This article mainly describes the Python arithmetic operator examples of the relevant information, the need for friends can refer to the following
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 = 21b = 10c = 0 c = A + bprint "line 1-value of C are", c C = a-bprint "line 2-value of C is", c C = A * bprint "line 3-value of C are", c C = a/bprint "line 4-value of C are", c C = a% bprint "line 5-value o F c is ", c a = 2b = 3c = A**bprint" line 6-value of C are ", c a = 10b = 5c = A//bprint" line 7-value of C is ", C
The result of the above example output:
Line 1-value of C are 31Line 2-value of C is 11Line 3-value of C are 210Line 4-value of C are 2Line 5-value of C is 1Line 6-value of C is 8Line 7-value of C is 2