Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 143 - CodePilot V5.0 C6
P. 143

    Program 5 To demonstrate the use of all the logical operators.

                         Program5.py
                      File  Edit  Format   Run    Options  Window     Help

                      # Assign values to variables                                         In case of the OR operator,
                      x = 5                                                                  the second condition is
                      y = 3                                                                  checked only if the first
                      # Logical NOT (not)                                                            is False.
                      print("not (x > y):", not (x > y))
                      # Logical AND (and)                                                   Output
                      print("x > 3 and y < 4:", x > 3 and y < 4)
                                                                                         not (x > y): False
                      # Logical OR (or)
                                                                                         x > 3 and y < 4: True
                      print("x > 3 or y > 4:", x > 3 or y > 4)
                                                                                         x > 3 or y > 4: True

                     ASSIGNMENT OPERATORS

                     Assignment operators are used to assign the value of the right expression to the left operand.
                     The assignment operators are described in the following table:
                                                                                                             Example
                      Operator         Name                              Description
                                                                                                           (x = 5, y = 3)
                           =      Assignment        Assigns the value of the operand on the right side to      x = y
                                                    the left side operand.
                          +=      Addition          Adds  the  right  operand  to  the  left operand  and      x += y
                                  assignment        assigns  the  result  to  the  left operand.  x += y  is
                                                    equivalent to x = x + y.

                          -=      Subtraction       Subtracts the right operand  from the left operand         x -= y
                                  assignment        and assigns the result to the left operand. x -= y is
                                                    equivalent to x = x - y.
                          *=      Multiplication    Multiplies  the  right operand  with the  left operand     x*=y
                                  assignment        and assigns the result to the left operand. x *= y is
                                                    equivalent to x = x * y.
                          /=      Division          Divides the left operand by the right operand              x /= y
                                  assignment        and assigns the result to the left operand. x /= y is
                                                    equivalent to x = x / y.
                          %=      Modulus           Takes the modulus of the left operand by the right         x %= y
                                  assignment        operand and assigns the result to the left operand. x
                                                    %= y is equivalent to x = x % y.
                          //=     Floor division    Performs floor division on the operands and assigns        x //= y
                                  assignment        the result to the left operand. x //= y is equivalent to
                                                    x = x // y.
                         **=      Exponentiation  Raises  the  left operand  to  the  power  of the  right    x **= y
                                  assignment        operand and assigns the result to the left operand. x
                                                    **= y is equivalent to x = x ** y.

                                                                                                                      141
                                                                                                  Python–Start to Code

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