在字典中循环时,关键字和对应的值可以使用items()方法同时解读出来: knights = {'gallahad': 'the pure', 'robin': 'the brave'} for k, v in knights.items(): ... print(k, v) ... gallahad the pure robin the ...
我们可以创建一个用来生成指定边界的斐波那契数列的函数: def fib(n): # write Fibonacci series up to n ... """Print a Fibonacci series up to n.""" ... a, b = 0, 1 ... while a n: ... print(a, end=' ') ...
也许最有名的是if语句。例如: x = int(input("Please enter an integer: ")) Please enter an integer: 42 if x 0: ... x = 0 ... print('Negative changed to zero') ... elif x == 0: ... print('Zero') ... ...