This article mainly introduces the relevant information of Pythonarithmetic operatorsdetailed examples. Friends in need can refer to
Python arithmetic operators
The following assumes that variable a is 10 and variable b is 20:
Operator | Description | Instance |
---|---|---|
+ | Add - Add two objects Adding | a + b outputs the result 30 |
- | minus-gets a negative number or one number minus another number | a - b outputs the result-10 |
* | Multiplication - Multiply two numbers or return a string repeated several times | a * b Output result 200 |
/ | divided - x divided by y | b / a Output result 2 |
% | Modulo - Returns the remainder of division | b % a Output result 0 |
** | Power - Returns the y power of Integer | Part9//2 Output result 4, 9.0//2.0 Output result 4.0 |
##The following | example demonstration The operation 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 Copy after login | The output result of the above example: 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 Copy after login |
The above is the detailed content of Detailed explanation of examples of arithmetic operators in Python. For more information, please follow other related articles on the PHP Chinese website!