谁能解释一下代码的最后一行是如何工作的。 python中使用的语言,我是python的新手?
Could anyone please explain how the last line of the code is working. The language used in python, I am new to python?
我无法理解最后一行是如何打印斐波那契数列的。如果有人能按顺序解释代码,那将是一个很大的帮助。
a , b = 0 , 1
while b < 10:
print(b, end = ' ')
a, b = b, a + b
a, b = b, a + b
正在打包一个元组 b, a + b
并在同一行将其解包 a, b =
。
The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:
x, y, z = t
This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences
循环开始
a = 0 , b=1 ; b < 10
所以 a, b = b, a+b 是 a =1 ,b = 1
现在 a= 1, b= 1 ; b< 10
所以 a, b = b, a+b 为 1, 2
现在a = 1, b = 2; b < 10
所以 a, b = b, a+b 是 2, 3
现在a = 2, b = 3; b < 10
所以 a, b = b, a+b 是 3, 5
现在a = 3, b = 5; b < 10
所以 a, b = b, a+b 是 5, 8
现在a = 5, b = 8; (b > 10)
所以 a, b = b, a+b 是 8, 13 ; b>10(循环中断)
ans 是 1 1 2 3 5 8
我无法理解最后一行是如何打印斐波那契数列的。如果有人能按顺序解释代码,那将是一个很大的帮助。
a , b = 0 , 1
while b < 10:
print(b, end = ' ')
a, b = b, a + b
a, b = b, a + b
正在打包一个元组 b, a + b
并在同一行将其解包 a, b =
。
The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:
x, y, z = t
This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences
循环开始
a = 0 , b=1 ; b < 10
所以 a, b = b, a+b 是 a =1 ,b = 1
现在 a= 1, b= 1 ; b< 10
所以 a, b = b, a+b 为 1, 2
现在a = 1, b = 2; b < 10
所以 a, b = b, a+b 是 2, 3
现在a = 2, b = 3; b < 10
所以 a, b = b, a+b 是 3, 5
现在a = 3, b = 5; b < 10
所以 a, b = b, a+b 是 5, 8
现在a = 5, b = 8; (b > 10)
所以 a, b = b, a+b 是 8, 13 ; b>10(循环中断)
ans 是 1 1 2 3 5 8