Assignment Operators


  • This program demonstrates the use of assignment operators in Python. The program starts by asking the user to input two numbers, 'a' and 'b', and assigns them to the variables a and b respectively. The program then prints out the values of a and b, which have been assigned by the user.
  • The program then uses the assignment operators to perform various mathematical operations on the variables a and b. The += operator is used to add the value of b to a and assigns the result to a. Similarly, the -= operator is used to subtract the value of b from a, the *= operator is used to multiply a by b, the /= operator is used to divide a by b, the %= operator is used to find the remainder of a when divided by b, the //= operator is used to perform floor division on a and b, and the **= operator is used to raise a to the power of b.
  • The program then prints out the result of each operation, using the assignment operator, showing how the value of the variable a has been modified by each operation.

Source Code

a   = int(input("Enter Number 1 : "))
b   = int(input("Enter Number 2 : "))

print("Assignment Operators Example ")

print("Number 1 =",a,"\nNumber 2 =",b)
a += b
print("Assignment Addition Example (+=) : =",a)
a -= b
print("Assignment Subtraction Example (-=) : =",a)
a *= b
print("Assignment Multiplication Example (*=) : =",a)
a /= b
print("Assignment Division Example (/=) : =",a)
a %= b
print("Assignment Modulo Example (%=) : =",a)
a //= b
print("Assignment Floor Division Example (//=) : =",a)
a **= b
print("Assignment Exponent Example (**=) : =",a)
To download raw file Click Here

Output

Enter Number 1 : 20
Enter Number 2 : 5

Assignment Operators Example

Number 1 = 20
Number 2 = 5
Assignment Addition Example (+=) : = 25
Assignment Subtraction Example (-=) : = 20
Assignment Multiplication Example (*=) : = 100
Assignment Division Example (/=) : = 20.0
Assignment Modulo Example (%=) : = 0.0
Assignment Floor Division Example (//=) : = 0.0
Assignment Exponent Example (**=) : = 0.0