Page 125 - Computing E-Book Grade 7
P. 125

The City School  2021-2022




             5.11.  Conditional Statements in Python



             Conditional statement is a set of rules performed if a certain condition is met.

             IF Statement


             IF statement is a programming conditional statement that, if proved true, performs a

             function or displays information. An if statement in python is written by using the IF
             keyword. The sample code below shows the if statement in python.                                        Programming the Computer-Python



             Sample Code



             # This program compares two numbers
             using if
             a = 33

             b = 200

             # if block
             if b > a:
                 print(“b is greater than a”)




             In this example, we use two variables, a and b, which are used as part of the if
             statement to test whether b is greater than a. As a is 33, and b is 200, we know that
             200 is greater than 33, and so we print to screen that “b is greater than a”.

             Indentation is necessary; if we do not use the indentation as mentioned in example,

             python will give an error.


             ELIF Statement

             The ELIF keyword is Python’s way of saying if the previous conditions were not true,

             then try this condition. The sample code below demonstrates Elif statement.
























                                                                                                                125
   120   121   122   123   124   125   126   127   128   129   130