## Python Assignment Operators

In this chapter we will be learning about Python assignment operators and some compound assignment operators supported in Python

## Python Assignment operators :

Python Assignment operator is used for assigning the value of right operand to the left operand.

#!/usr/bin/python num1 = 10 num2 = 20.20 num3 = 'A' print ("Line 1 - Value of num1 : ", num1) print ("Line 2 - Value of num2 : ", num2) print ("Line 3 - Value of num3 : ", num3)

**Output :**

>>> Line 1 - Value of num1 : 10 Line 2 - Value of num2 : 20.2 Line 3 - Value of num3 : A >>>

## Python Compound Assignment Operators

Consider the value of num1 = 10 and num2 = 5

Operator | Explanation |
---|---|

+= | Add right operand to left and assigns addition to the left operand |

-= | Subtract right operand from left and assigns subtraction to the left operand |

*= | Multiply right operand to left and assigns multiplication to the left operand |

/= | Divide right operand by left and assigns division to the left operand |

%= | Divide right operand by left and assigns remainder of division to the left operand |

//= | Divide right operand by left and assigns results of division to the left operand by removing digits after the decimal point |

**= | Perform exponential calculation of two operands and then assign result to left operand. |

In these kind of compound assignment operators we are also performing indirect python arithmetic operations.

## Example:

#!/usr/bin/python num1 = 10 num2 = 5 res = num1 + num2 res += num1 print ("Line 1 - Result of + is ", res) res *= num1 print ("Line 2 - Result of * is ", res) res /= num1 print ("Line 3 - Result of / is ", res) res //= num1 print ("Line 4 - Result of // is ", res) res **= num1 print ("Line 5 - Result of ** is ", res) res %= num1 print ("Line 6 - Result of % is ", res) res -= num1 print ("Line 7 - Result of - is ", res)

**Output :**

>>> Line 1 - Result of + is 25 Line 2 - Result of * is 250 Line 3 - Result of / is 25.0 Line 4 - Result of // is 2.0 Line 5 - Result of ** is 1024.0 Line 6 - Result of % is 4.0 Line 7 - Result of - is -6.0 >>>