Python Arithmetic Operators, python Arithmetic
Python Arithmetic Operators
Assume that variable a is 10 and variable B is 20:
| Operator |
Description |
Instance |
| + |
Add-add two objects |
A + B output result 30 |
| - |
Minus-get a negative number or a number minus another number |
A-B output result-10 |
| * |
Multiply by two numbers or return a string that has been repeated for several times. |
A * B output 200 |
| / |
Divide By-x by y |
B/a output result 2 |
| % |
Modulo-return the remainder of the Division |
B % a output result 0 |
| ** |
Power-returns the y Power of x. |
A ** B output result 20 |
| // |
Returns the integer portion of the operator. |
9 // 2 output result 4, 9.0 // 2.0 output result 4.0 |
The following example demonstrates the operations of all Arithmetic Operators in Python:
#!/usr/bin/python a = 21b = 10c = 0 c = a + bprint "Line 1 - Value of c is ", c c = a - bprint "Line 2 - Value of c is ", c c = a * bprint "Line 3 - Value of c is ", c c = a / bprint "Line 4 - Value of c is ", c c = a % bprint "Line 5 - Value of c is ", c a = 2b = 3c = a**bprint "Line 6 - Value of c is ", c a = 10b = 5c = a//bprint "Line 7 - Value of c is ", c |
Output result of the above instance:
Line 1 - Value of c is 31Line 2 - Value of c is 11Line 3 - Value of c is 210Line 4 - Value of c is 2Line 5 - Value of c is 1Line 6 - Value of c is 8Line 7 - Value of c is 2
|