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 = 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 result of the above instance:
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
|