Operators in Python

3. Assignment Operators

The assignment operator is used to assign a specific value to a variable or an operand.

The equal to (=) operator is used to assign a value to an operand directly, if we use any arithmetic operator along with the equal to operator then it will perform the arithmetic operation on the given variable and then assign the resulting value to that variable itself.

Example: x=2
                  print(x)   Output: 2
                 x+=2
                 print(x)    Output: 4

Operator

Description

Example(s)

=

Assigns the right operand to the left operand

X=12

+=

Adds the right operand to the left operand and then assigns to the left operand

X=3

X+=2   (X=X+2)

-=

Subtracts the right operand from left operand and then the result is assigned to the left operand

X=3

X-=2      (X=X-2)

*=

Multiplies the right operand with left operand and then the result is assigned to the left operand

X=3

X*=4     (X=X*4)

/=

Divides the left operand with right operand and then the result (with decimals) is assigned to the left operand

X=3

X/=4     (X=X/4)

%=

Divides the left operand with right operand and then the remainder is assigned to the left operand

X=3

X%=4     (X=X%4)

//=

Divides the left operand with right operand and then the result (without decimals) is assigned to the left operand

X=3

X//=4     (X=X//4)

**=

Exponent – The left operand raised to the right operand will be calculated and result is assigned to left operand

X=3

X**=4     (X=X**4)


The following video deals with the examples of Assignment operators in Python.

"Assignment Operators in Python" by Chakradhar Dwivedi, prototype is licensed under CC BY-SA 3.0