整数和浮点数
Python支持对整数和浮点数直接进行四则混合运算,运算规则和数学上的四则运算规则完全一致。
基本的运算:
1 + 2 + 3 # ==> 6
4 * 5 - 6 # ==> 14
7.5 / 8 + 2.1 # ==> 3.0375
使用括号可以提升优先级,这和数学运算完全一致,注意只能使用小括号,但是括号可以嵌套很多层:
(1 + 2) * 3 # ==> 9
(2.2 + 3.3) / (1.5 * (9 - 0.3)) # ==> 0.42145593869731807
和数学运算不同的地方是,Python的整数运算结果仍然是整数,浮点数运算结果仍然是浮点数:
1 + 2 # ==> 整数 3
1.0 + 2.0 # ==> 浮点数 3.0
但是整数和浮点数混合运算的结果就变成浮点数了:
1 + 2.0 # ==> 浮点数 3.0
为什么要区分整数运算和浮点数运算呢?这是因为整数运算的结果永远是精确的,而浮点数运算的结果不一定精确,因为计算机内存再大,也无法精确表示出无限循环小数,比如 0.1
换成二进制表示就是无限循环小数。
那整数的除法运算遇到除不尽的时候,结果难道不是浮点数吗?我们来试一下:
11 / 4 # ==> 2
令很多初学者惊讶的是,Python的整数除法,即使除不尽,结果仍然是整数,余数直接被扔掉。不过,Python提供了一个求余的运算 % 可以计算余数:
11 % 4 # ==> 3
如果我们要计算 11 / 4 的精确结果,按照“整数和浮点数混合运算的结果是浮点数”的法则,把两个数中的一个变成浮点数再运算就没问题了:
11.0 / 4 # ==> 2.75
任务
请计算 2.5 + 10 / 4 ,并解释计算结果为什么不是期望的 5.0 ?
请修复上述运算,使得计算结果是 5.0
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAiYAAABdCAIAAAAe1jo6AAAILUlEQVR4nO2cT2saeRiA/RB72YMrHhbKInsoBEr/JBDSQ5UcG9pAIpQl0EuRWi+FsliI0EJ7SSBRcuhBel2aSw9KoYeWfor5KrOHcf5GnVFn3veNeR6eQ6LjONmV92Fmfrb02+9/ICIiClhSPwJERLwhkhxERBSS5CAiopAkBxERhSQ5iIgoJMlBREQhSQ4iIgpJchARUUiSg4iIQpIcREQUslSuVDEvSwAAM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c5iL7a094Eqybn2fnPH7+iLhCe+P+PyfKBq8vJZiQnvn34knnJib0qZcVa+KUc38Q6scQj3jdmFv5SzgonEMG7R9462bmpaZyy/ZzkLLBwICHJQYyoPe1NYOdfH0jcy0FRZydngbXRiDhH7WlvApKD1TL/xhpi8WpPexOQHKyWSQ5i8WpPexPYSQ5qSnIQi1Z72pug9OetmpbqnwBERDG1p70JSA4iooTa094EJAcRUULtaW8CkoOIKKH2tDcByUFElFB72puA5CAiSqg97U1AchARJdSe9iYgOYiIEmpPexOQHERECbWnvQlIDiKihNrT3gQkBxFRQu1pbwKSg4goofa0NwHJQUSUUHvam4DkICJKqD3tTUByEBEl1J72JvgfIhWdC1PWP5gAAAAASUVORK5CYII=" alt="" />
布尔类型
我们已经了解了Python支持布尔类型的数据,布尔类型只有True
和False
两种值,但是布尔类型有以下几种运算:
与运算:只有两个布尔值都为 True 时,计算结果才为 True。
True and True # ==> True
True and False # ==> False
False and True # ==> False
False and False # ==> False
或运算:只要有一个布尔值为 True,计算结果就是 True。
True or True # ==> True
True or False # ==> True
False or True # ==> True
False or False # ==> False
非运算:把True变为False,或者把False变为True:
not True # ==> False
not False # ==> True
布尔运算在计算机中用来做条件判断,根据计算结果为True或者False,计算机可以自动执行不同的后续代码。
在Python中,布尔类型还可以与其他数据类型做 and、or和not运算,请看下面的代码:
a = True
print a and 'a=T' or 'a=F'
计算结果不是布尔类型,而是字符串 'a=T',这是为什么呢?
因为Python把0
、空字符串''
和None
看成 False,其他数值和非空字符串都看成 True,所以:
True and 'a=T' 计算结果是 'a=T'
继续计算 'a=T' or 'a=F' 计算结果还是 'a=T'
要解释上述结果,又涉及到 and 和 or 运算的一条重要法则:短路计算。
1. 在计算 a and b
时,如果 a 是 False,则根据与运算法则,整个结果必定为 False,因此返回 a;如果 a 是 True,则整个计算结果必定取决与 b,因此返回 b。
2. 在计算 a or b
时,如果 a 是 True,则根据或运算法则,整个计算结果必定为 True,因此返回 a;如果 a 是 False,则整个计算结果必定取决于 b,因此返回 b。
所以Python解释器在做布尔运算时,只要能提前确定计算结果,它就不会往后算了,直接返回结果。
任务
请运行如下代码,并解释打印的结果:
a = 'python'
print 'hello,', a or 'world'
b = ''
print 'hello,', b or 'world'
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAm4AAAChCAIAAADx66QMAAASpElEQVR4nO2dTWsbdx6A9SH2sgc36BAIS6rDQi6rVU1LQGuHXAwxrBuJ0ITFe7HCWqFpzeLFkAqW6hIU/ELqU9CprrHBzUH29mTjT+EP0EvaBJ+1h9G8/DUvmtFvZn6S8jw8BGssjUdjZR7/Z0aawh/++AkiIiKObUF9CRAREadaUoqIiCiSlCIiIookpYiIiCJJKSIiokhSioiIKJKUIiIiiiSliIiIIkkpIiKiSFKKiIgokpQiIiKKJKWIiIgiC+/evfvtt9+sfx1+//339+/f//rrr3M3ipiWBQCAEPowzZBSUgoA+mi3AESQUlIKAPpotwBEkFJSCgD6aLcARJBSUgoA+mi3AESQUlIKAPpotwBEFN69e/fhw4fr6+sPHz6QUlIKACpotwBEFN6/f399ff3pp596a0pKSSkA5Il2C0BE4fr6+pNPPrlz586XX37p1JSUklIAyBPtFoCIQrFYXFhY+Oabb77//vvvvvvOqikpJaUAkCfaLQARha+//vqHH3746aef3r59+8svv/z888/X19eklJQCQJ5otwBEFK59MColpQCQM9otABG8GYaUAoA+6WzQk88qbAHSXbCMmJwlJKWkFAD0SW2bPtaswh6V4oJlxIQsISklpQCgT2rb9JlOqX95JmQJSSkpBQB9Utumk1INSCkpBQB9UtumFwr9kCOdET8ubAECp8sX23ns0EyGZuu9Gba6Ej1f/9zGW/6AZ0RKSSkAqJPaNt2cW8FTkaG7RdyMmB7zgYmWM3CBY/64pM837P5CSCkpBQB9Utmg90clJOwnhi2Af3rMB469nP3wHCZanqTThZBSUgoA+qSyQe/HTkj0oyKmh80/KRHziVjm+MuTdLoQzZSun/b7p8/i3rPf71/tLWrnkJQCQBakskHvz1BKA+ccf3mSThcyHSmdu1GcWz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