Assignment Operators in Python Language

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.

  1. 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

  1. Assignment with addition

See Example

>>> x = 56

>>> x += 56

>>> print(x)

112

Here x + = 56 means x = x + 56

  1. 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.

  1. Assignment with multiplication

>>> a = 4

>>> a *= 5   # a = a * 5

>>> print(a)

20

  1. Assignment with Division

>>> df = 45

>>> df /= 5   # df = df / 5

>>> print(df)

9.0

Leave a Comment