Here are the Assignment Operators in Python Language
Assignment Operators are used to assign values to variables. In simple words, putting value in a variable. These values can be of any data type.
- Equal to Operator – ‘=’
Equal to operator is used to assign any value to a variable.
Example –
>>> x = 5
>>> name = ‘Mohit’
Here value of 5 (integer) is assigned to variable x
Value of Mohit (String type) is assigned to variable name.
Note – the value to be assigned (defined) should be written on right hand side of = sign.
Here x = 5 is correct but 5 = x is incorrect.
See an example:
>>> a = 65
>>> b = a
>>> print(b)
65
>>> u = 65
>>> u = v #Will raise Error
- Assignment with addition
See Example
>>> x = 56
>>> x += 56
>>> print(x)
112
Here x + = 56 means x = x + 56
- Assignment with Subtraction
>>> g = 58
>>> g -= 8 # g = g-8 | g = 58 – 8
>>> print(g)
50
We are assigning a value of g – 8 to g itself.
- Assignment with multiplication
>>> a = 4
>>> a *= 5 # a = a * 5
>>> print(a)
20
- Assignment with Division
>>> df = 45
>>> df /= 5 # df = df / 5
>>> print(df)
9.0