Posts

Showing posts from April, 2025

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 ...

While Loop

''' While Loop: The 'while' loop as the name suggests, execute statements while the condition is True. As soon as the condition becomes False, the interpreter comes out of the while loop. ''' for i in range(3):     print(i) #output = 0 #         1 #         2 i = 0 while(i<3):     print(i)     i = i + 1 #output = 0 #         1 #         2 x = 2 while(x <= 10):     print(x)     x = x + 2 print("Done with the loop") #output = 2 #         4 #         6 #         8 #         10 #         Done with the loop y =int(input("Enter the number: ")) while(y <= 38):     y = int(input("Enter the number: "))     print(y) print("Done with the loop") #Enter the number: 10 #10 #Enter the number: 38 #38 #En...

For Loops

''' Loops in Python: Sometimes we want to execute a group of statements a certain number of times. This can be done using loops. Loops are used to execute a block of code repeatedly, either a fixed number of time. Based on this, loops are further classified into following types; for loop, while loop, nested loop. ''' ''' For-loop: A for-loop in Python is used for iterating over sequences such as lists, tuples, strings, dictionaries, and even ranges. It simplifies respective tasks and enhances the code efficiency. ''' name = "Sindhu" for i in name:     print(i) #output = S #         i #         n #         d #         h #         u #Here "i" denotes every character of the string "Sindhu" name1 = "Shilpa" for i in name1:     print(i)     if(i == "p"):         print("This is special") #output = S #     ...

Match case Statements

''' A match case statement compares a given variable's value to different shapes, also referred to as the pattern. The main data is to keep on comparing the variable with all the present patterns until it fits into one. match-case is very similar to if-else statements, but they do have some differences. The main difference is that, if-statements are used for general conditions, but match-case is used for specific conditions i.e. discrete values and patterns. if-statements could be lengthy for coarse conditions, but match-case is more efficient and shorter. if-statements support logical operators like and, or but match-case does not. ''' x = int(input("Enter the value of x: ")) # x is the variable to match. match x:     #if x is 0     case 0:         print("x is zero")     #case with if-condition     case 4 if x % 2 == 0:         print("x % 2 == 0 and case is 4")  #Empty case with if-condition   ...

if-else Statements

''' if-else Statements: Sometimes the programmer needs to check the evaluation of certain expression (s), whether the expression(s) evaluate True or False. An if-else statement in Python is a way to make decisions in our code. It allows our program to choose between two or more paths based on a certain condition. ''' a = int(input("Enter your age: ")) print("Your age is:" ,a) if(a>18):     print("You can drive")     print("Yes") else:     print("You cannot drive")     print("No") # #output = Enter your age: 23 #           Your age is : 23 #           You can drive #           Yes '''Conditional Operators: < , > , <= , >= , == , != ''' print(a<18)    #Enter your age: 18   output = False print(a>18)                         # output = False print(a<=18)...

String Methods

''' Strings are immutable. String methods work on our existing string and produce another string as per our requirement, but do not change our existing string. ''' name = "Sindhu" print(len(name))           #output = 6 #1.upper() print(name.upper())        #output = SINDHU #It converts the string into an upper case. #2.lower() print(name.lower())        #output = sindhu #It converts the string into a lower case. #3.rstrip() name1 = "Sindhu!!!" print(name1.rstrip("!"))   #output = Sindhu name2 = "Shilpa??" print(name2.rstrip("?"))   #output = Shilpa name3 = "@Prasad" print(name3.rstrip("@"))   #output = @Prasad #It removes only the trailing characters. #It does not remove the leading characters. #4.replace() print(name.replace("Sindhu" , "Asish"))     #output = Asish name4 = "Sindhu @ Sindhu" print(name4.replace("Sindhu" , "Asish"))    #output =...