Assume variable a holds 10 and variable b holds 20, then:
Operator | Description | Example |
---|---|---|
= | Assigns values from right side operands to left side operand | c = a + b assigns value of a + b into c |
+= Add AND | It adds the right operand to the left operand and assigns the result to the left operand | c += a is equivalent to c = c + a |
-= Subtract AND | It subtracts the right operand from the left operand and assigns the result to the left operand | c -= a is equivalent to c = c - a |
*= Multiply AND | It multiplies the right operand with the left operand and assigns the result to the left operand | c *= a is equivalent to c = c * a |
/= Divide AND | It divides the left operand with the right operand and assigns the result to the left operand | c /= a is equivalent to c = c / a |
%= Modulus AND | It takes modulus using two operands and assigns the result to the left operand | c %= a is equivalent to c = c % a |
**= Exponent AND | Performs exponential (power) calculation on operators and assign value to the left operand | c **= a is equivalent to c = c ** a |
//= Floor Division | It performs floor division on operators and assigns value to the left operand | c //= a is equivalent to c = c // a |
Assume variable a holds 10 and variable b holds 20, then:
#!/usr/bin/python a = 21 b = 10 c = 0 c = a + b print "Line 1 - Value of c is ", c c += a print "Line 2 - Value of c is ", c c *= a print "Line 3 - Value of c is ", c c /= a print "Line 4 - Value of c is ", c c = 2 c %= a print "Line 5 - Value of c is ", c c **= a print "Line 6 - Value of c is ", c c //= a print "Line 7 - Value of c is ", c
When you execute the above program, it produces the following result:
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 99864
Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.