Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 101 - 2629_Devagiri_C-6
P. 101

    Program 6 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

                                                                                                                      99
                                                                                                  Python–Start to Code

| 96 97 98 99 100 101 102 103 104 105 106 |