Page 111 - TCS ICT Book 8
P. 111

The City School  2021-2022




             In this example a is equal to b, so the first condition is not true, but the elif condition is

             true, so we print to screen that “a and b are equal”.

             Else Statement

             The else keyword catches anything which isn’t caught by the preceding conditions. The
             sampe code below shows else statement.


             Sample code:
             # This program compares two numbers using else statements

             a = 33

             b = 200                                                                                                Programming Robots & Single-Board Computers
             # if block
             if b > a:

                print(“b is greater than a”)

             # elif block
             elif b == a:
                print(“a and b are equal”)

             # else block

             else:
                print(“a is greater than b”)


             In this example a is greater than b, so the first condition is not true, also the elif
             condition is not true, so we go to the else condition and print to screen that “a is

             greater than b”. We can also use the else without using elif.

             5.8.  Conditional and Logical Operators


             Conditional operators refine the statement you’re testing for. For instance,  you can
             specify the statement whether it’s greater than, less than, and a whole lot more.


                          Operator                                Meaning

                      =                   Equal too
                      >                   Greater than

                      <                   Less than
                      >=                  More than or equal to

                      <=                  Less than or equal to
                      <>                  Not equal to









                                                                                                                111
   106   107   108   109   110   111   112   113   114   115   116