CONTROL STATEMENTS ==================== normal execution a = 100 b = 200 c = a + b print(c) # 300 ==================== selection execution a = 100 b = 200 if a > b: print("a is bigger") else: print("b is bigger") ==================== repeated execution i = 1 print(i) i <= 100: i = i + 1 print(i) =============================== ==> three types of control statements: 1) conditional statements/selection statements 2) loop statements/iterative statements 3) transfer statements def fun(): pass 1) conditional statements/selection statements ==================================== ==> when we want to execute the selected block of code only, conditional statements used. ==> three conditional statements: 1) if statement 2) if-else statement 3) if-elif-else statement here: if, else and elif ==> keywords 1) if statement =========== Syntax: if test_condition: statement-1 statement-2 test_condition ==> relational operators, boolean values, integer, string (any value) # WAP TO CHANGE THE SIGN OF THE NUMBER IF IT IS NEGATIVE. number = int(input("Enter a value:")) # number < 0 ==> negative # number > 0 ==> positive if number < 0: number = -number print("The number after the sign change = ",number) print("The number which is defined is = ",number) ========================= # if condition with integer value: if 1: print("Hi") print("Hello") print("Good night") print("End of code.") =============================== # if condition with integer value: if False: print("Hi") print("Hello") print("Good night") print("End of code.") ======================== if '': print("Hi") print("Hello") print("Good night") print("End of code.") =================================================== 2) if-else statement =============== Syntax: if condition: statement-1 statement-2 else: statement-3 statement-4 # WAP TO FIND THE GIVEN NUMBER IS EVEN NUMBER OR ODD NUMBER. num = int(input("Enter a value:")) # 96 # even ==> num / 2, remainder == 0 ==> num % 2 == 0 # odd ==> num % 2 != 0 if num % 2 == 0: print(num,"is an even number.") else: print(num,"is an odd number.") =================================== # WAP TO TAKE FOUR INTEGER VALUES AND CHECK WHETHER THE TAKE FOUR INTEGERS FORMS A SQUARE OR NOT. i1 = int(input("Enter an integer:")) i2 = int(input("Enter an integer:")) i3 = int(input("Enter an integer:")) i4 = int(input("Enter an integer:")) # if i1 == i2 and i1 == i3 and i1 == i4: if i1 == i2 == i3 == i4: print("Square is possible with these given inputs.") else: print("Square is not possible with these given inputs.") ========================================================= Assignments: =========== USING IF-ELSE & CONDITIONAL OPERATOR ===================================== 1) WAP TO CHECK WHETHER THE GIVEN INTEGER IS POSTIVE OR NEGATIVE. 2) WAP TO FIND THE BIGGEST NUMBER AMONG TWO NUMBERS. 3) WAP TO FIND THE SUM OF TWO INTEGERS WHEN FIRST IS SMALLER THAN SECOND OTHERWISE FIND ITS DIFFERENCE. ======================================================= 3) if-elif-else statement ================== Syntax: if condition1: block of code elif condition2: block of code elif condition3: block of code else: block of code # WAP TO FIND THE TYPE OF TRIANGLE. # EQUILATERAL ==> ALL THREE SIDES ARE EQUAL # ISOSCELES ==> ANY OF TWO SIDES ARE EQUAL # SCALENE ==> ALL THREE SIDES ARE NOT EQUAL s1 = int(input("Enter a side:")) s2 = int(input("Enter a side:")) s3 = int(input("Enter a side:")) if s1 == s2 == s3: print("It is Equilateral Triangle.") elif s1 == s2 or s1 == s3 or s2 == s3: print("It is Isosceles triangle.") else: print("Scalene Triangle.") ============================================= # WAP TO ACCEPT 5 SUBJECT MARKS FOR THE STUDENT AND CALCULATE GRADE OF THE STUDENT. """ PERCENTAGE >= 85 ==> 'A+' PERCENTAGE ==> BETWEEN 75 AND 85 ==> 'A' BETWEEN 65 AND 75 ==> 'B' BETWEEN 50 AND 65 ==> 'C' BETWEEN 35 AND 50 ==> 'D' BELOW 35 ==> FAIL """ sub1 = int(input("Enter marks for subject:")) sub2 = int(input("Enter marks for subject:")) sub3 = int(input("Enter marks for subject:")) sub4 = int(input("Enter marks for subject:")) sub5 = int(input("Enter marks for subject:")) total_marks = sub1 + sub2 + sub3 + sub4 + sub5 percentage = total_marks // 5 print("The Student's total Score = ", total_marks) print("The Student's Percentage = ", percentage) if percentage >= 85: grade = 'A+' elif 75 <= percentage < 85: grade = 'A' elif 65 <= percentage < 75: grade = 'B' elif 50 <= percentage < 65: grade = 'C' elif 35 <= percentage < 50: grade = 'D' else: grade = 'Fail' print("The Grade of the student = ", grade)