Python arithmetic operators
The following assumes that the variable A is 10 and the variable B is 20:
| operator |
Description |
Example |
| + |
Add-Two objects added |
A + B output result 30 |
| - |
Minus-get negative numbers or one number minus the other |
-B Output Result-10 |
| * |
Multiply by two number or return a string that is repeated several times |
A * b output result 200 |
| / |
Except-X divided by Y |
B/A Output Results 2 |
| % |
Modulo-Returns the remainder of the division |
B% A output result 0 |
| ** |
Power-Returns the Y power of X |
A**b Output Results 20 |
| // |
Divide-Returns the integer part of the quotient |
9//2 output result 4, 9.0//2.0 output 4.0 |
The following example shows the operation of all the arithmetic operators of Python:
#!/usr/bin/pythona =21b =10c =0c =a +bprint"Line 1 - Value of c is ", c c =a -bprint"Line 2 - Value of c is ", cc =a *bprint"Line 3 - Value of c is ", cc =a /bprint"Line 4 - Value of c is ", cc =a %bprint "Line 5 - Value of c is ", ca =2b =3c =a**bprint"Line 6 - Value of c is ", ca =10b =5c =a//bprint"Line 7 - Value of c is ", c |
The result of the above example output:
Line 1-Value of c is31Line 2-Value of c is11Line 3- Value of c is210Line 4-Value of c is2Line 5-Value of c is1Line 6-Value of c is8Line 7-Value of c is2
|
Python arithmetic operators