>>> # fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
... print(b)
... a, b = b, a+b
...
1
1
2
3
5
8
the first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. on the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. the right-hand side expressions are evaluated from the left to the righ
第一行包含多个赋值:变量a和b同时获得新值0和1。在最后一行中,再次使用,表明在任何赋值发生之前首先对右边的表达式进行求值。右边表达式由左到右进行计算。
the while loop executes as long as the condition (here: b >> i = 256*256
>>> print('the value of i is', i)
the value of i is 65536
the keyword argument end can be used to avoid the newline after the output, or end the output with a different string:关键字参数结束可以用来避免输出后的换行符,或者用不同的字符串结束输出:
>>> a, b = 0, 1
>>> while b < 1000:
... print(b, end=',')
... a, b = b, a+b
...
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
footnotes:注意:
since ** has higher precedence than -, -3**2 will be interpreted as -(3**2) and thus result in -9. to avoid this and get 9, you can use (-3)**2.
由于**比-a具有更高的优先级,-*** 2将被解释为-(3**2),从而导致-9。为了避免这个问题,得到9,你可以使用(-3)** 2。
unlike other languages, special characters such as \n have the same meaning with both single ('...') and double (...) quotes. the only difference between the two is that within single quotes you don’t need to escape (but you have to escape \') and vice versa.不像其他语言,特殊字符如\与双单引号(‘...’)和双引号(“....”)有相同的作用,唯一的不同是在双单引号时您不需要避免双引号,但是要避免\',反之亦然。