古城堡 发表于 2018-8-12 13:55:46

Python中的简单计算

Python中的简单计算
(1)基本的加减乘除>>> 2 + 24>>> 50 - 5*620>>> (50 - 5*6) / 45.0>>> 8 / 51.6  (2)除法总是会返回一个浮点数,想要返回整数,需要用“//”来表示(floor division),另外,可以用“%”进行取余操作
>>> 17 / 3# classic division returns a float5.666666666666667>>>>>> 17 // 3# floor division discards the fractional part5>>> 17 % 3# the % operator returns the remainder of the division2  (3)幂运算可以使用“**”来进行
>>> 5 ** 2# 5 squared25>>> 2 ** 7# 2 to the power of 7128  需要注意的是,
  >>> -3**2 #it will be interpretedas -(3**2)
  -9
  如果需要得到正确的结果,需要下面的写法
  >>> (-3)**2
  9
  除了float和int类型的数据之外,还支持其他类型的数字,例如Decimal和Fraction,当然python也支持复杂的数据格式,一般都是以j或者J作为后缀,例如3+5j
页: [1]
查看完整版本: Python中的简单计算