CONTROL STATEMENTS =================== PYTHON PROGRAMS ================ STATEMENT BY STATEMENT EXECUTION IS APPLICABLE FOR PYTHON PROGRAMS THIS IS CALLED AS "SEQUENTIAL EXECUTION". WHY CONTROL STATEMENTS ====================== 1) TO MAKE EXECUTE THE SELECTED BLOCK OF STATEMENTS ONLY 2) TO MAKE EXECUTE SELECTED THE BLOCK OF CODE FOR SEVERAL NUMBER OF TIMES REPEATEDLY. 3) TO PAUSE/STOP EXECUTING THE BLOCK OF CODE TYPES OF CONTROL STATEMENTS ============================ 3-TYPES: 1) CONDITIONAL STATEMENTS 2) ITERATIVE STATEMENTS 3) TRANSFER STATEMENTS 1) CONDITIONAL STATEMENTS ========================= ==> ALSO CALLED AS "SELECTION STATEMENTS" ==> TO MAKE EXECUTE ONLY SELECTED BLOCK OF CODE IN A PROGRAM, CONDITIONAL STATEMENTS ARE USED. ==> THREE TYPES OF CONDITIONAL STATEMENTS: 1) IF STATEMENT 2) IF ELSE STATEMENT 3) NESETED IF ELSE STATEMENT 4) IF ELIF ELSE STATEMENT 1) IF STATEMENT =============== if is a keyword we can use if keyword to define the if statement. Syntax: if condition: statement-1 statement-2 statement-3 statement-4 Drawback: ========= 1) if a condition is "True". the program can execute with sequential execution process. 2) if a condition is "False" if block statements cannot execute. number = int(input("Enter a number:")) if number < 0: # -7 number = -number # negative number is changing to positive number 7 print("The Number after the sign change is = ",number) # 7 print("The Given number = ",number) # 7 ============================= n = False if n: print("Hi") print("Hello") print("Good Morning") ======================================================== 2) IF ELSE STATEMENT ==================== here: else ===> keyword Syntax: if condition: statement-1 statement-2 else: statement-3 statement-4 if condition == "True" ==> only if block statements can execute if condition == "False" ==> only the else block statements can execute. # WAP TO CHECK WHETHER THE GIVEN NUMBER IS EVEN NUMBER OR ODD NUMBER. num = int(input("Enter a number:")) # even number ==> number / 2: remainder == 0 # odd number ==> number / 2: remainder == 1 if num % 2 == 0: print(num,"is an even number.") else: print(num,"is an odd number.") =========================================================== Nested if else: ============== Syntax: if condition1: if condition2: statement-1 statement-2 else: statement-3 statement-4 else: if condition3: statement-5 statement-6 else: statement-7 statement-8 # WAP TO FIND THE BIGGEST NUMBER AMONG THREE INTEGERS. th1 = int(input("Enter a value:")) # 7 th2 = int(input("Enter a value:")) # 9 th3 = int(input("Entre a value:")) # 97 if th1 > th2: if th1 > th3: biggest = th1 else: biggest = th3 else: if th2 > th3: biggest = th2 else: biggest = th3 print("The Biggest number = ",biggest) ============================================= 4) if elif else ladder ===================== Syntax: if condition1: statements elif condition2: statements elif condition3: statements else: statements s1 = int(input("Enter marks:")) s2 = int(input("Enter marks:")) s3 = int(input("Enter marks:")) s4 = int(input("Entre marks:")) total = s1 + s2 + s3 + s4 percentage = total // 4 if percentage >= 85: grade = 'A' elif percentage >= 70 and percentage < 85: grade = 'B' elif percentage >= 55 and percentage < 70: grade = 'C' elif percentage >= 40 and percentage < 55: grade = 'D' else: grade = 'F' print("The Total Marks of the student = ",total) print("The Percentage of the student = ",percentage) print("The Grade of the student = ",grade)