Python Arithmetic Operators Example
Python Arithmetic Operators Example
Here is example to show how arithmetic operation can be performed :-
#!/usr/bin/python
a = 21
b = 10
c = 0
c = a + b
print “Line 1 – Value of c is “, c
c = a – b
print “Line 2 – Value of c is “, c
c = a * b
print “Line 3 – Value of c is “, c
c = a / b
print “Line 4 – Value of c is “, c
c = a % b
print “Line 5 – Value of c is “, c
a = 2
b = 3
c = a**b
print “Line 6 – Value of c is “, c
a = 10
b = 5
c = a//b
print “Line 7 – Value of c is “, c
Output:-
Line 1 – Value of c is 31
Line 2 – Value of c is 11
Line 3 – Value of c is 210
Line 4 – Value of c is 2
Line 5 – Value of c is 1
Line 6 – Value of c is 8
Line 7 – Value of c is 2
Lets see below another example:-
Line | Code | Meaning |
1 | total = 9 | total is assigned the value of 9. |
2 | total += 10 | total is assigned the value of itself plus 10. |
This is the “shortcut” operator for addition. It is easier to type than “total = total + 10”. | ||
total now has the value of 19. | ||
3 | total -= 10 | total is assigned the value of itself minus 10. |
This is the shortcut operator for subtraction. | ||
total now has the value of 9. | ||
4 | total *= 3 | total is assigned the value of itself multiplied by 3. |
This is the shortcut operator for multiplication. | ||
total now has the value of 27. | ||
5 | total /= 2 | total is assigned the value of itself divided by 2. |
This is the shortcut operator for division. | ||
total now has the value of 13, not 13.5, because / returns the integer portion of the division. | ||
6 | total = 27 | |
7 | from __future__ import division | |
8 | total /= 2 | total is assigned the value of itself divided by 2, preceded by an import from the __future__ module. |
What this line says is that in a future release of Python (Python 3000, to be exact), division will include the fractional portion of the result. | ||
total is now 13.5 | ||
9 | total = ’9’ | total is assigned the value of the character ’9’. |
This ’9’ is not an integer, but a string data type. Even though it looks the same as the integer 9, the program cannot use it for arithmetic calculations. | ||
10 | total += 10 | total, with a string value of ’9’ is assigned the value of itself plus the integer 10. |
This causes a compiler error, because you cannot add a string and a number | ||
11 | total = int(total) | total is converted to an integer. |
This only works if the string can convert to an integer. | ||
total now has the value of the integer 9. | ||
12 | total += .5 | total is assigned the value of itself plus a floating point number. |
The datatype of total now becomes float in order to accommodate the floating point total now has the value of 9.5 | ||
13 | total -= .5 | total is assigned the value of itself minus a floating point number. |
The datatype of total is still a float, even though its decimal fraction is 0. total now has a value of 9.0 | ||
14 | total = int(total) | total is converted to an integer. |
total now has the value of 9. |