Skip to main content

Python Assignment Operators

Operators are used to do action or process on variables and operands. The operator usually represents  as symbol for doing an action. Python has different type of operators like Arithmetic, Assignment, Relational / Comparison , Logical, Bitwise, Identity and Membership operators. Every operators are using  to do different kind of operations.

In this tutorial we will learn about  Python Assignment operators

Python Assignment Operators

Python Assignment Operators are used to assign the values to the variables.

 
Operator Description Syntax Example
= assigns the value from  one operand to another operand or mathematical operation

result = var1

result = var1 (Arithmetic Symbol) var2

a = 5

c = a + b

+= Adds and assigns that value to the first operand var1 += var2 a += b is used for a = a + b
-= subtracts and assigns that value to the first operand var1 -= var2 a -= b is used for a = a - b
*= Multiplies and assigns that value to the first operand var1 *= var2 a *= b is used for a = a * b
/= Divides and assigns that value to the first operand var1 /= var2 a /= b is used for a = a / b
%= Modulus - Returns the remainder of first operand divided by second operand and assigns to first operand var1 %= var2 a %= b is used for a = a % b
**= Exponentiation - Returns the first operand power of second operand and assigns to first operand var1 **= var2 a **= b is used for a = a ** b
//= Floor Division - Divides the values of two operands and returns the integer value and assigns to first operand var1 //= var2 a //= b is used for a = a // b

 

Code Example:

>>> var1 = 2
>>> var2 = 3
>>> var3 = var1 + var2
>>> print('Assign value => ' + str(var1) + ' + ' +  str(var2) + ' = ' + str(var3))
Assign value => 2 + 3 = 5
>>> var1 = 2
>>> var2 = var3 = 3
>>> var2 += var1
>>> print('Addition and Assign => ' + str(var3) + ' += ' +  str(var1) + ' = ',(var2))
Addition and Assign => 3 += 2 =  5
>>> var1 = 3
>>> var2 = var3 = 5
>>> var2 -= var1
>>> print('Subtraction and Assign => ' + str(var3) + ' -= ' + str(var1) + ' = ' + str(var2))
Subtraction and Assign => 5 -= 3 = 2
>>> var1 = 5
>>> var2 = var3 = 6
>>> var2 *= var1
>>> print('Multiplication and Assign => ' + str(var3) + ' *= ' + str(var1) + ' = ' + str(var2))
Multiplication and Assign => 6 *= 5 = 30
>>> var1 = 6
>>> var2 = var3 = 30
>>> var2 /= var1
>>> print('Division and Assign => ' + str(var3) + ' /= ' + str(var1) + ' = ' + str(var2))
Division and Assign => 30 /= 6 = 5.0
>>> var1 = 2
>>> var2 = var3 = 3
>>> var2 %= var1
>>> print('Modulus and Assign => ' + str(var3) + ' %= ' + str(var1) + ' = ' + str(var2))
Modulus and Assign => 3 %= 2 = 1
>>> var1 = 2
>>> var2 = var3 = 2
>>> var2 **= var1
>>> print('Exponentiation and Assign => ' + str(var3) + ' **= ' + str(var1) + ' = ' + str(var2))
Exponentiation and Assign => 2 **= 2 = 4
>>> var1 = 2
>>> var2 = var3 = 5
>>> var2 //= var1
>>> print('Floor Division and Assign => ' + str(var3) + ' //= ' + str(var1) + ' = ' + str(var2))
Floor Division and Assign => 5 //= 2 = 2
>>>