Home>Article>Backend Development> How to calculate in python
Python calculations mainly use Python operators. Let’s take a simple example: 4 5 = 9. In the example, 4 and 5 are called operands, and " " is called the operator.
The Python language supports a lot of operators. Here we only introduce the arithmetic operators used in calculations. (Recommended learning:Python video tutorial)
The following hypothetical variables: a=10, b=20:
Operator |
Description |
Instance |
Multiply- Multiply two numbers or return a string repeated several times |
a * b The output result is 200 |
|
Divide - 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 | #Round and divide - Return the integer part of the quotient (rounded down)
>>> 9//2 | 4
|
-5 |
# Example: #!/usr/bin/python# -*- coding: UTF-8 -*- a = 21b = 10c = 0 c = a + bprint "1 - c 的值为:", c c = a - bprint "2 - c 的值为:", c c = a * bprint "3 - c 的值为:", c c = a / bprint "4 - c 的值为:", c c = a % bprint "5 - c 的值为:", c # 修改变量 a 、b 、ca = 2b = 3c = a**b print "6 - c 的值为:", c a = 10b = 5c = a//b print "7 - c 的值为:", c The output result of the above example: 1 - c 的值为: 31 2 - c 的值为: 11 3 - c 的值为: 210 4 - c 的值为: 2 5 - c 的值为: 1 6 - c 的值为: 8 7 - c 的值为: 2For more Python-related technical articles, please visit the | Python Tutorial
The above is the detailed content of How to calculate in python. For more information, please follow other related articles on the PHP Chinese website!