Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 99 - 2629_Devagiri_C-6
P. 99

    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

                                                                                                                      97
                                                                                                  Python–Start to Code

| 94 95 96 97 98 99 100 101 102 103 104 |