数字计算
- type
通过type()函数确定任何数据的类型
type(3)
type(3.14)
type(type)
type(print)
- python内置数值操作
- 类型转换和舍入
x = 5.0 * 2 python会强制将2变成2.0与5相乘,因为将float数转化成int数相乘会导致精度损失,而int转float则不会
int(3.5)
float(4)
float(int(3.3))
int(float(3.2))
四舍五入使用round()函数
round(3.5)
round(3.499)
round(4.0)
- 使用math库
#导入数学模块
import math
#打印pi
print(math.pi)
#打印自然数e
print(math.e)
- 阶乘
range的四种用法
for i in range(10):
print(i)
for i in range(5,15):
print(i)
for i in range(5,15,2):
print(i)
for i in range(15,5,-1):
print(i)
# n!
def main():
n = eval(input("Please input a number: "))
fact = 1
for i in range(n,1,-1):
fact = fact*i
print("The factorial of ",n," is :",fact)
main()