PYTHON ASSIGNMENT OPERATORS
INTRODUCTION:
PYTHON ASSIGNMENT OPERATORS perform an operation and assign the result.
ASSIGN OPERATOR (=):
ASSIGN OPERATOR assign a value to a variable. It can be used with any data type.
# Assignment operator example
a = 9
b = 2.5
c = 'Hello World'
d = True
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\nValue of c -> ", c)
print("\nValue of d -> ", d)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of b -> 2.5
Value of c -> Hello World
Value of d -> True
ADD AND ASSIGN OPERATOR (+=):
ADD AND ASSIGN OPERATORS add two values and assign the result to the variable in the left.
ADDING TWO DIFFERENT NUMBERS:
# Assignment operator example
a = 9
b = 2.5
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
b += a
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of b -> 2.5
Value of a -> 9
Value of b -> 11.5
ADDING SAME NUMBER:
# Assignment operator example
a = 9
print("\nValue of a -> ", a)
a += a
print("\nValue of a -> ", a)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of a -> 18
SUBTRACT AND ASSIGN OPERATOR (-=):
SUBTRACT AND ASSIGN OPERATORS subtract two values and assign the result to the variable in the left.
SUBTRACTING TWO DIFFERENT NUMBERS:
# Assignment operator example
a = 9
b = 5
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
a -= b
print("\nValue of a -= b -> ", a)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of b -> 5
Value of a -= b -> 4
SUBTRACTING SAME NUMBER FROM ITSELF:
# Assignment operator example
a = 9
print("\nValue of a -> ", a)
a -= a
print("\nValue of a -= a -> ", a)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of a -= a -> 0
MULTIPLY AND ASSIGN OPERATOR (*=):
MULTIPLY AND ASSIGN OPERATORS multiply two values and assign the result to the variable in the left.
MULTIPLYING TWO DIFFERENT NUMBERS:
# Assignment operator example
a = 9
b = 5
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
a *= b
print("\nValue of a *= b -> ", a)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of b -> 5
Value of a *= b -> 45
MULTIPLYING SAME NUMBER TO ITSELF:
# Assignment operator example
a = 9
print("\nValue of a -> ", a)
a *= a
print("\nValue of a *= a -> ", a)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of a *= a -> 81
DIVIDE AND ASSIGN OPERATOR (/=):
DIVIDE AND ASSIGN OPERATORS divide two values and assign the result to the variable in the left.
DIVIDING TWO DIFFERENT NUMBERS:
# Assignment operator example
a = 9
b = 5
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
a /= b
print("\nValue of a /= b -> ", a)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of b -> 5
Value of a /= b -> 1.8
DIVIDING SAME NUMBER:
# Assignment operator example
a = 9
print("\nValue of a -> ", a)
a /= a
print("\nValue of a /= a -> ", a)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of a /= a -> 1.0
FLOOR DEVIDE AND ASSIGN OPERATOR (//=):
FLOOR DIVIDE AND ASSIGN OPERATORS divide two values and assign the quotient part to the variable in the left.
FLOOR DEVIDING TWO DIFFERENT NUMBERS:
# Assignment operator example
a = 9
b = 5
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
a //= b
print("\nValue of a //= b -> ", a)
a = 9
b //= a
print("\nValue of b //= a -> ", b)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of b -> 5
Value of a //= b -> 1
Value of b //= a -> 0
FLOOR DEVIDING SAME NUMBER:
a = 9
print("\nValue of a -> ", a)
a //= a
print("\nValue of a //= a -> ", a)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of a //= a -> 1
MODULUS AND ASSIGN OPERATOR (%=):
MODULUS AND ASSIGN OPERATORS divide two values and assign the remainder part to the variable in the left.
MODULUS OPERATION OF TWO DIFFERENT NUMBERS:
# Assignment operator example
a = 9
b = 5
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
a %= b
print("\nValue of a %= b -> ", a)
PYTHON ASSIGNMENT OPERATORS : Output
Value of a -> 9
Value of b -> 5
Value of a %= b -> 4
MODULUS OPERATION OF SAME NUMBERS:
# Assignment operator example
a = 9
print("\nValue of a -> ", a)
a %= a
print("\nValue of a %= a -> ", a)
PYTHON ASSIGNMENT OPERATORS : Output
RELATED TOPICS:
Pingback: PYTHON LOGICAL OPERATORS - Sayantan's Blog On Python Programming
Pingback: PYTHON MEMBERSHIP OPERATORS - Sayantan's Blog On Python Programming
Pingback: PYTHON ARITHMETIC OPERATORS - Sayantan's Blog On Python Programming