python 中的数值运算
1、使用内置的type函数查看变量的类型。
spam_amount = 0
print(type(spam_amount))
print(type(1.2))

2、常用的数值运算符
a + b :表示a加上b
a - b :表示a减b
a * b : 表示a乘以b
a / b : a除以b
a // b :a除以b取整
a % b :a除以b取余
a ** b :a的b次方
-a :负a

3、一个例子:
hat_height_cm = 25
my_height_cm = 190
# How tall am I, in meters, when wearing my hat?
total_height_meters = hat_height_cm + my_height_cm / 100
print("Height in meters =", total_height_meters, "?")

4、最大最小函数max,min
print(min(1, 2, 3))
print(max(1, 2, 3))

5、去绝对值函数abs
print(abs(32))
print(abs(-32))

6、类型强制转换:
print(float(10))
print(int(3.33))
# They can even be called on strings!
print(int('807') + 1)

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:87
阅读量:80
阅读量:48
阅读量:174
阅读量:112