Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 142 - CodePilot V5.0 C6
P. 142

    Example
                        Operator        Name                      Description                                   Output
                                                                                              (x = 5, y = 3)
                            <       Less than       Returns True if the left operand is less      x < y          False
                                                    than the right operand.
                            >=      Greater  than  Returns True if the left operand  is          x >= y          True
                                    or equal to     greater than or equal to the right.
                            <=      Less than or    Returns True if the left operand is less     x <= y          False
                                    equal to        than or equal to the right operand.

                       Program 4     To demonstrate the use of all relational operators.

                          Program4.py

                       File  Edit  Format   Run    Options   Window    Help

                       # Assign values to variables
                                                                                           Write a  Python program to
                       x = 5
                                                                                            calculate the value of the
                       y = 3
                                                                                           following expression if x = 8,
                       # Equal to (==)
                                                                                                y = 4 and z = 10:
                       print("x == y:", x == y)
                                                                                                 x(x+y)*(z−y)//x
                       # Not equal to (!=)
                       print("x != y:", x != y)
                       # Greater than (>)
                       print("x > y:", x > y)                                              Output

                       # Less than (<)                                                  x == y: False
                       print("x < y:", x < y)                                           x != y: True

                       # Greater than or equal to (>=)                                  x > y: True
                       print("x >= y:", x >= y)                                         x < y: False
                       # Less than or equal to (<=)                                     x >= y: True
                       print("x <= y:", x <= y)                                         x <= y: False

                      LOGICAL OPERATORS
                      Logical operators are used  to  combine  conditional statements  and return a  Boolean  value

                      (True or False) based on the conditions provided.
                      Python supports the following logical operators:

                                                                                                   Example
                       Operator       Name                        Description                                     Output
                                                                                                 (x = 5, y = 3)
                       not         not           Reverses the  result, returns True if the         not(x > y)      False
                                                 condition is False or vice versa.
                       and         and           Returns True if both conditions are True.       x > 3 and y < 4   True
                       or          or            Returns True if at least one condition is True.  x > 3 or y > 4   True

                      140
                            CodePilot (V5.0)-VI

         5

| 137 138 139 140 141 142 143 144 145 146 147 |