Page 41 - Code & Click - 7
P. 41

Relational Operators
            Relational  operators  are  used  to  compare  two  values  or  expressions.  They  are  also  known  as
            Comparison operators. The result of these operators is either True or False.

                                                                                        Example
             Operator        Name                       Description                                      Output
                                                                                      (X = 9, Y = 6)

                                        If the values compared are equal, returns
                 ==         Equal to                                                      X == Y          False
                                        True, else returns False.

                 !=      Not equal to   If  the  values  compared  are  unequal,          X != Y          True
                                        returns True, else returns False.
                            Greater     If  the  first  value  is  greater  than  the
                  >                                                                       X > Y           True
                              than      second, returns True, else returns False.

                  <        Less than    If the first value is less than the second,       X < Y           False
                                        returns True, else returns False.
                            Greater     If the first value is greater than or equal
                 >=         than or     to the second, returns True, else returns         X >= Y          True
                            equal to    False.
                          Less than or  If the first value is less than or equal to the
                 <=                                                                       X <= Y          False
                            equal to    second, returns True, else returns False.
            Logical Operators
            Logical operators are used to combine two or more expressions containing relational operators.
            These operators evaluate to either True or False. These are used to test conditions and control the
            flow of a program based on the result of the condition.
                                                                                        Example
             Operator                          Description                                                Output
                                                                                      (X = 9, Y = 6)

                        If both expressions evaluate to True, returns True, else
                and     returns False.                                              (X > 5) and (Y > 4)    True

                        If any one of the expressions evaluates to True, returns
                 or                                                                 (X > 5) or (Y > 10)    True
                        True, else returns False.

                        If expression evaluates to True, returns False, and vice-
                not                                                                     not(X > Y)         False
                        versa (reverses the result of the expression).
                                                                                                 Experiential Learning
              Lab Activity 5
             Program to illustrate the use of Relational and Logical operators.
             1. Type the code as shown in the image in the Python Editor.












             2. Press the F5 key to execute the code. Sample output of the program is given.


                                                                                                                 39
   36   37   38   39   40   41   42   43   44   45   46