Python学习之路(1)——序列解包
序列解包:Python中一次给多个变量赋多个值
基本方法就是一次性将一个元组赋值给多个变量
1 #序列解包 2 value = 1, 2, 3 3 print(type(value)) 4 print(value) 5 6 x, y, z = value 7 print(x, y, z) 8 print(x) 9 print(y) 10 print(z) 11 print(type(x))
举一个例子,著名的斐波纳契数列
1 #斐波纳契数列 2 def fibonacci(count): 3 a, b = 0, 1 4 list = [] 5 for i in range(count): 6 if i == 0: 7 list.append(a) 8 else: 9 list.append(b) 10 a, b = b, a+b 11 return list 12 13 print(fibonacci(0)) 14 15 print(fibonacci(1)) 16 17 print(fibonacci(2)) 18 19 print(fibonacci(5)) 20 21 print(fibonacci(10))
浙公网安备 33010602011771号