CONTROL STATEMENTS ==================== for: 1) to execute only the selected part of the program only 2) to make execute the selected part of the program/block of program repeatedly 3) during the execution of the program, to stop execution uncertainly ==> three types: 1) conditional statements/selection statements 2) loop statements/iterative statements 3) loop control statements/jump statements 1) conditional statements/selection statements ==================================== ==> three types: 1) if statement 2) if-else statements 3) if-elif-else statements ==> keywords: if, else, elif Note: ==== switch statement ==> not supposed to use. 1) if statement =========== Syntax: if test_condition: statement-1 statement-2 statement-3 test-codition: can be allowed to define with relational operators boolean values # WAP TO CHANGE THE SIGN OF THE NUMBER. number = int(input("Enter a value:")) # -7 if number < 0: number = -number print("The number after sign change = ", number) print("The number = ", number) ================================================ 2) if-else statements ================ Syntax: if test_condition: statement-1 statement-2 statement-3 else: statement-4 statement-5 statement-6 # WAP TO FIND WHETHER THE GIVEN NUMBER IS POSITIVE OR NEGATIVE. number = int(input("Enter a value:")) if number > 0: print(number, "is a positive number.") else: print(number, "is a negative number.") ===================================== # WAP TO CHECK WHETHER THE GIVEN FOUR INTEGERS WILL FORM A SQUARE OR NOT. side1 = int(input("Enter an integer:")) side2 = int(input("Enter an integer:")) side3 = int(input("Enter an integer:")) side4 = int(input("Enter an integer:")) # if side1 == side2 and side1 == side3 and side1 == side4: if side1 == side2 == side3 == side4: print("The given four side value will form a square.") else: print("The given four side values will not form a square.") ================================================= ASSIGNMENTS: ============= USING IF-ELSE: USING CONDITIONAL OPERATORS 1) WAP IN PYTHON TO CHECK WHETHER THE GIVEN NUMBER IS EVEN OR ODD NUMBER. 2) WAP IN PYTHON TO FIND THE BIGGEST NUMBER AMONG TWO NUMBERS. 3) WAP TO FIND THE SUM OF TWO NUMBERS WHEN FIRST NUMBER IS SMALLER THAN SECOND OTHERWISE FIND THE DIFFERENCE. ================================================== 3) if-elif-else statements ================== Syntax: if test_condition1: statement-1 statement-2 elif test_condition2: statement-3 statement-4 elif test_condition3: statement-5 statement-6 else: statement-7 statement-8 # WAP TO CHECK THE TYPE OF TRIANGLE. """ Equilateral ==> side1 == side2 == side3 Isosceles ==> side1 == side2 or side2 == side3 or side1 == side3 Scalene ==> side!= side2 != side3 """ side1 = int(input("Enter a side:")) side2 = int(input("Enter a side:")) side3 = int(input("Enter a side:")) if side1 == side2 == side3: print("This is Equilateral Triangle.") elif side1 == side2 or side1 == side3 or side2 == side3: print("This is Isosceles Triangle.") else: print("This is Scalene Triangle.") =================================== # WAP TO FIND THE SMALLEST NUMBER AMONG 5 INTEGERS. a = int(input("Enter a value:")) b = int(input("Enter a value:")) c = int(input("Enter a value:")) d = int(input("Enter a value:")) e = int(input("Enter a value:")) if a < b and a < c and a < d and a < e: smaller = a elif b < c and b < d and b < e: smaller = b elif c < d and c < e: smaller = c elif d < e: smaller = d else: smaller = e print("The Smallest number = ", smaller) ============================================== # WAP TO FIND THE GRADE OF THE STUDENT BY TAKING 5 SUBJECT MARKS """ PERCENTAGE >= 85 ==> A+ PERCENTAGE < 85 AND PERCENTAGE >= 75 ==> A PERCENTAGE < 75 AND PERCENTAGE >= 65 ==> B PERCENTAGE < 65 AND PERCENTAGE >= 55 ==> C PERCENTAGE < 55 AND PERCENTAGE >= 40 ==> D PERCENTAGE < 40 AND PERCENTAGE >= 33 ==> E PERCENTAGE < 33 ==> FAIL """ s1 = int(input("Enter a subject marks:")) s2 = int(input("Enter a subject marks:")) s3 = int(input("Enter a subject marks:")) s4 = int(input("Enter a subject marks:")) s5 = int(input("Enter a subject marks:")) total_marks = s1 + s2 + s3 + s4 + s5 percentage = total_marks // 5 # floor division ==> // # when we need a quotient in integer format ==> floor division # normal division ==> / # when we need a quotient in float format ==> normal division # modulo division ==> % # to get a remainder as an output ==> modulo division print("The Total score of the student = ", total_marks) print("The Percentage of the student = ", percentage) if percentage >= 85: grade = 'A+' elif 75 <= percentage < 85: grade = 'A' elif 65 <= percentage < 75: grade = 'B' elif 55 <= percentage < 65: grade = 'C' elif 40 <= percentage < 55: grade = 'D' elif 33 <= percentage < 40: grade = 'E' else: grade = 'Fail' print("The Grade of the student = ", grade)