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/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
|
The result of the above example 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
|
Python arithmetic operators