Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 128 - 2612_Digimind
P. 128

    To demonstrate the use of all the assignment operators.

                          Program6.py
                       File  Edit  Format    Run   Options   Window    Help

                       # Assign values to variables
                       a = 5
                       y = 3
                       # Assignment (=)
                       x = a
                       print("Assignment: x =", x)
                       # Addition assignment (+=)
                       x += a
                       print("Addition Assignment (+=): x =", x)
                       # Subtraction assignment (-=)
                       x -= y
                       print("Subtraction Assignment (-=): x =", x)
                       # Multiplication assignment (*=)
                       x *= y
                       print("Multiplication Assignment (*=): x =", x)
                       # Division assignment (/=)
                       x /= y
                       print("Division Assignment (/=): x =", x)
                       # Modulus assignment (%=)
                       x %= y
                       print("Modulus Assignment (%=): x =", x)
                       # Floor division assignment (//=)
                       x //= y
                       print("Floor Division Assignment (//=): x =", x)
                       # Exponentiation assignment (**=)
                       x **= y
                       print("Exponentiation Assignment (**=): x =", x)

                          Output

                        Assignment: x = 5
                        Addition Assignment (+=): x = 10
                        Subtraction Assignment (-=): x = 7
                        Multiplication Assignment (*=): x = 21
                        Division Assignment (/=): x = 7.0
                        Modulus Assignment (%=): x = 1.0
                        Floor Division Assignment (//=): x = 0.0
                        Exponentiation Assignment (**=): x = 0.0

                      126   Premium Edition-VII

| 123 124 125 126 127 128 129 130 131 132 133 |