Class 11:Python Assignment operators

Assignment operators

Assignment operators are used in Python to assign values to variables.
a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable “a” on the left.

 

There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5.

Assignment operators in Python

Operator

Example

Equivalent to

=

x = 10

x = 10

+=

x += 10

x = x + 10

-=

x -= 10

x = x – 10

*=

x *= 10

x = x * 10

/=

x /= 10

x = x / 10

%=

x %= 10

x = x % 10

//=

x //= 10

x = x // 10

**=

x **= 10

x = x ** 10

Example:

a=10
print(a)
a=a+10
print(a)
a=10
a+=10
print(a)

Output:

10
20
20
>>>

Example:

a=20
print(a)
a=a-10
print(a)
a=20
a-=10
print(a)

Output:

20
10
10
>>>

Example:

a=10
print(a)
a=a*10 
print(a) 
a=10 
a*=10
print(a)

Output:

10
100
100
>>>

Example:

a=20
print(a)
a=a/10
print(a)
a=20
a/=10
print(a)

Output:

20
2.0
2.0
>>>

Example:

#1
a=10
print(a) #output:10
#2
a=a%10
print(a)#output:0
#3
a=10
a%=10
print(a)#output:0
#4
a=5
print(a)#output:5
#5
a=a/2
print(a)#output:2.5
#6
a=5
a=a//2
print(a)#output:2
#7
a=5
a=a/2
print(a)#output:2.5

Output:

10
0
0
5
2.5
2
2.5
>>>