Posts

Operation on Tuples:

''' Manipulating Tuples: As tuples are immutable, if we want to add, remove, or change the tuples items, then first we must convert the tuple to a list, then perform operation on that list and convert it back to tuple. ''' #Example: countries = ("India", "Italy", "Spain", "Australia") x = list(countries) print(x)               #output = ['India', 'Italy', 'Spain', 'Australia'] x.append("Russia")     #add item print(x)               #output = ['India', 'Italy', 'Spain, 'Australia', 'Russia'] x.pop(3)               #remove item print(x)               #output = ['India', 'Italy', 'Spain', 'Russia'] x[2] = "England"       #change item print(x)               #output = ['India', 'Italy', 'England', 'Russia'] #We covert the tuple to a list, manipulating the items...

Tuple

''' Python Tuples: Tuple is a built-in data type used to store multiple items in a single variable. Tuple items are separated by commas and enclosed within round brackets(). Basic Characteristics of Tuple: 1.Ordered: The items have a defined order and that order will not change. 2.Immutable: We cannot change, add, or remove items after tuple is created. 3.Allow duplicates: Tuples can have items with the same values. 4.Can contain mixed types: Like integers, strings, list etc. ''' tup1 = (10, 20, 30, 40, 50) print(tup1)                     #output = (10, 20, 30, 40, 50) print(type(tup1))           #output = <class 'tuple'> tup2 = (100) print(type(tup2))           #output = <class 'int'> tup3 = (100, ) print(type(tup3))           #output = <class 'tuple'> print(type(tup3), tup3)     #output = <class 'tuple'>...

List Methods

''' List Methods: 1.sort() 2.reverse() 3.index() 4.count() 5.copy() 6.append() 7.insert() 8.extend() ''' #list.sort() #This method sorts the list in ascending order and the original list is updated. #Example: x = [24, 9, 10, 28, 22, 18] x.sort() print(x)                #output = [9, 10, 18, 22, 24, 28] x.sort(reverse = True) print(x)                #output = [28, 24, 22, 18, 10, 9] #reverse() #This method reverses the order of the list. #Example: l = [1, 2, 3, 4, 5, 6, 7] l.reverse() print(l)                #output = [7, 6, 5, 4, 3, 2, 1] #index() #This method returns the index of first occurrence of the list item. #Example: print(l.index(5))       #output = 4 print(x.index(22))      #output = 2 y = [1, 2, 3, 1, 2, 3]   print(y.index(1))       #output = 0 #If a particual element is more than one time in a...

Lists

''' 1. Lists are ordered collection of data items. 2. They store multiple items in a single variable. 3. List items are separated by commas and enclosed within square brackets [ ]. 4. Lists are mutable i.e. we can alter them after creation. ''' list = [1, 2, 3] print(list)             #output = [1, 2, 3] print(type(list))       #output = <class 'list'> marks = [50, 60, 80] print(marks)            #output = [50, 60, 80] print(type(marks))      #output = <class 'list'> print(marks[0])         #output = 50 print(marks[1])         #output = 60 print(marks[2])         #output = 80   #Different data types can come under a single list. data = [24, 2, 2002, "Sindhu", True] print(data)             #output = [24, 2, 2002, 'Sindhu', True] ''' list Index: Each item/element in...

Function Arguments and Return Statements

''' Function Argument and Return Statements: There are four types of arguments that we can provide in a function: 1.Default Arguments 2.Keyword Arguments 3.Variable-Length Arguments 4.Required Arguments ''' def average(a, b):     print("The average is", (a+b) /2)      average(4, 6)               #output = The average is 5.0 ''' Default Argument: This is a function parameter that has a default value assigned to it i.e. if the programmer does not provide a value for that parameter, the default value is used instead. ''' def average(a=10, b=20):     print("The average is", (a+b)/2) average( )                 #output = The average is 15.0 #Here the default values of a, b are used as we have not mentioned any specific values for the parameters. def average(a=10, b=20):     print("The average is", (a+b)/2) average(12, 22)            #o...

Functions

''' A function is a block of code that performs a specific task whenever it is called. They may help in making programs more organized, modular, and easier to maintain. Let's say we have to perform a block of 50 codes for many times. Then, we have to write or copy-paste the block of codes many times. To avoid such a thing, we will define and rename the block of 50 codes and use that renamed function with different variables as much as we want. This will lessen the amount of mistakes and make our code smaller. There are two types of functions: 1. Built-in Functions 2. User-defined Functions Built-in Functions: These functions are defined and pre-coded in Python. Examples: min(), max(), len(), sum(), type(), range(), dict(), list(), tuple(), set(), print(), etc. User-defined Functions: We can create functions to perform specific tasks as per our needs. ''' #Example: ''' Let's say we need to find geometric mean of two numbers. The most beginner-...

Break and Continue Statements

''' Break Statement: The break-statement in Python is used to skip a loop prematurely i.e. before the loop has finished all its iterations. We can use break inside a for or while loop while a certain condition is met and we want to stop the loop at that point. ''' for i in range(12):     if(i == 10):         break     print("5 x", i+1, "=", 5 * (i+1))      print("Loop ko chodkar nikal gaya") #output : 5 x 1 = 5 #         5 x 2 = 10 #         5 x 3 = 15 #         5 x 4 = 20 #         5 x 5 = 25 #         5 x 6 = 30 #         5 x 7 = 35 #         5 x 8 = 40 #         5 x 9 = 45 #         5 x 10 = 50 #         Loop ko chodkar nikal gaya ''' Continue Statement: It skips the rest of the loop ...