Partial preview of the text
Download Python programming Made Easy step-by-step Notes for school students and college students and more Study notes Computer science in PDF only on Docsity!
PYTHON PROGRAMMING lil YEAR/II SEM MRCET Output: Hello MRCET Hello CSE Hello SIR Hello MADAM #Program to find area of a circle using function use single return value functor argument. pi=3.14 def areaOfCircle(r): return pi*r*r r=int(input("Enter radius of circle")) print(areaOfCircle(r)) Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/ful .py Enter radius of circle 3 28.259999999999998 #Program to write sum different product and using arguments with return value function. def calculete(a,b): total=a+b diff=a-b prod=a*b div=a/b mod=a%b 31 PYTHON PROGRAMMING lil YEAR/I1 SEM return total,diff,prod,div,mod a=int(input("Enter a value")) b=int(input("Enter b value")) #function call s,d,p,q,m = calculete(a,b) print("Sum= ",s,"diff= ",d,"mul= ",p,"div= ",q,"mod= ",m) #print("diff= ",d) #print("mul= ",p) #print("div= ",q) #print("mod= ",m) Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/ful .py Enter a value 5 Enter b value 6 Sum= 11 diff= -1 mul= 30 div= 0.8333333333333334 mod= 5 #program to find biggest of two numbers using functions. def biggest(a,b): if a>b: return a else : return b a=int(input("Enter a value")) b=int(input(""Enter b value")) #function call big= biggest(a,b) print("big number= ",big) Output: 32 MRCET 37/142 é PYTHON PROGRAMMING lil YEAR/II SEM MRCET else: return "fail" a=int(input("Enter one subject marks")) print(result(a)) Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/ful .py Enter one subject marks 35 fail #Write a program to display mrecet cse dept 10 times on the screen. (while ey, 29/142.) & def usingFunctions(): count =0 while count<10: print("mrcet cse dept" count) count=count+1 usingFunctions() Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/ful .py mrcet cse dept 0 mrcet cse dept | mrcet cse dept 2 mrcet cse dept 3 mrcet cse dept 4 mrcet cse dept 5 mrcet cse dept 6 mrcet cse dept 7 mrcet cse dept 8 mrcet cse dept 9 34 PYTHON PROGRAMMING lll YEAR/II SEM MRCET UNIT — I CONTROL FLOW, LOOPS Conditionals: Boolean values and operators, conditional (if), alternative (if-else), chained conditional (if-elif-else); Iteration: while, for, break, continue. Control Flow, Loops: Boolean Values and Operators: A boolean expression is an expression that is either true or false. The following examples use the operator ==, which compares two operands and produces True if they are equal and False otherwise: 40/142 >>> 5 == True >>> 5 == False True and False are special values that belong to the type bool; they are not strings: >>> type(True) >>> type(False) The == operator is one of the relational operators; the others are: x != y # x is not equal to y x > y #x is greater than y x < y # x is less than y X >= y #x 1s greater than or equal to y x <= y # xX 1s less than or equal to y Note: All expressions involving relational and logical operators will evaluate to either true or false 35 PYTHON PROGRAMMING ill YEAR/I SEM MRCET C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/if] .py 3 is greater done -l ais smaller Finish a=10 if a>9: print("A is Greater than 9") Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/if2.py A is Greater than 9 Alternative if (If-Else): An else statement can be combined with an if statement. An else statement contains the block of code (false block) that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at most only one else Statement following if. Syntax of if - else : if test expression: Body of if stmts else: Body of else stmts If - else Flowchart : 37 PYTHON PROGRAMMING I YEAR/II SEM MRCET False Test Expression | True | Body of if | Body of else E | Fig: Operation of if — else statement Example of if - else: a=int(input(‘enter the number')) if a>5: print("a is greater") else: print("a is smaller than the input given") Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/ifelse.py enter the number 2 a is smaller than the input given a=10 b=20 if a>b: print("A is Greater than B") else: print("B is Greater than A") Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/if2.py B is Greater than A 38 PYTHON PROGRAMMING I] YEAR/II SEM MRCET print("a is greater") elif b>c: print("b is greater") else: print("c is greater") Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/ifelse.py enter the number5 enter the number2 enter the number9 a is greater >>> C:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/ifelse.py enter the number2 enter the number5 enter the number9 c is greater var = 100 if var == 200: print("1 - Got a true expression value") print(var) elif var == 150: print("2 - Got a true expression value") print(var) elif var == 100: print("3 - Got a true expression value") print(var) else: print("4 - Got a false expression value") print(var) print("Good bye!") Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/ifelif.py 3 - Got a true expression value 100 Good bye! 40 PYTHON PROGRAMMING lil] YEAR/I1 SEM MRCET Iteration: A loop statement allows us to execute a statement or group of statements multiple times as long as the condition is true. Repeated execution of a set of statements with the help of loops is called iteration. Loops statements are used when we need to run same code again and again, each time with a different value. Statements: In Python Iteration (Loops) statements are of three types: 1. While Loop 2. For Loop 3. Nested For Loops While loop: e Loops are either infinite or conditional. Python while loop keeps reiterating a block of code defined inside it until the desired condition is met. e The while loop contains a boolean expression and the code inside the loop is repeatedly executed as long as the boolean expression is true. e The statements that are executed inside while can be a single line of code or a block of multiple statements. Syntax: while(expression): Statement(s) Flowchart: | while loop false Is condition true? true Body of while loop Execute statements i exit while loop 41 PYTHON PROGRAMMING lil YEAR/II SEM MRCET j=l while i<=3: print("MRCET",end="_ ") while j<=1: print("CSE DEPT" ,end="") j=j+1 i=i+1 print() Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/wh3.py MRCET CSE DEPT MRCET MRCET = J while (i < 10): print (1) i=i+l Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/wh4.py l 2 3 4 5 6 7 8 9 QD. werner nnn nn nnn nn nnn nn ne enn nee n+ 2+ ----- }: b= while (a<10): print (‘Iteration’,a) a=a+l b=b+1 43 PYTHON PROGRAMMING Ill YEAR/II SEM MRCET if (b == 4): break print (‘While loop terminated’) Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/whS.py Iteration 1 Iteration 2 Iteration 3 While loop terminated count = 0 while (count < 9): print("The count is:", count) count = count + 1 print("Good bye!") Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/wh.py = The count is: The count is: The count is: The count is: The count is: The count is: The count is: The count is: The count is: Good bye! on nNM fF WN eK © For loop: Python for loop is used for repeated execution of a group of statements for the desired number of times. It iterates over the items of lists, tuples, strings, the dictionaries and other iterable objects | Syntax: for var in sequence: Statement(s) A sequence of values assigned to var in each iteration Holds the value of item in sequence in each iteration 44 PYTHON PROGRAMMING ll] YEAR/II SEM Iterating over a list: #list of items list — ['M','R','C','E’,'T'] i= 1 #Iterating over the list for item in list: print (‘college ',1,' is ',item) i=i+l Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/lis.py college 1 is M college 2 is R college 3 is C college 4 is E college 5 is T Iterating over a Tuple: tuple = (2,3,5,7) print (‘These are the first four prime numbers ') #Iterating over the tuple for a in tuple: print (a) Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/fr3.py These are the first four prime numbers 2 3 5 7 Iterating over a dictionary: #creating a dictionary college = {"ces":"block1","it":"block2","ece":"block3" } #Iterating over the dictionary to print keys print (‘Keys are:') 46 MRCET 91/142 é PYTHON PROGRAMMING lil YEAR/I1 SEM MRCET for keys in college: print (keys) #Iterating over the dictionary to print values print (‘Values are:') for blocks in college.values(): print(blocks) Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/dic.py Keys are: ces it ece Values are: block] block2 block3 Iterating over a String: #declare a string to iterate over college = 'MRCET' #Iterating over the string for name in college: print (name) Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/strr.py 4Hmaxe Nested For loop: When one Loop defined within another Loop is called Nested Loops. Syntax: for val in sequence: for val in sequence: 47 PYTHON PROGRAMMING lil YEAR/I1 SEM MRCET Break and continue: In Python, break and continue statements can alter the flow of a normal loop. Sometimes we wish to terminate the current iteration or even the whole loop without checking test expression. The break and continue statements are used in these cases. Break: The break statement terminates the loop containing it and control of the program flows to the statement immediately after the body of the loop. If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop. Flowchart: Enter loop > test expression of loop Exit Loop v Remaining body of loop The following shows the working of break statement in for and while loop: for var in sequence: # code inside for loop If condition: break (if break condition satisfies it jumps to outside loop) # code inside for loop # code outside for loop 49 PYTHON PROGRAMMING Il YEAR/II SEM MRCET while test expression # code inside while loop If condition: break (if break condition satisfies it jumps to outside loop) # code inside while loop # code outside while loop Example: for val in "MRCET COLLEGE": if val=="": break print(val) print("The end") # Program to display all the elements before number 88 for num in [11, 9, 88, 10, 90, 3, 19]: print(num) if(num==88): print("The number 88 is found") print(Terminating the loop") break Output: 1] 9 88 The number 8&8 is found 50