if-elif-else
>>> if x < 0: x = 0 print('Negative changed to zero') elif x == 0: print('Zero') elif x == 1: print('Single') else: print('More')
迭代式for语句
a = ['cat', 'window', 'defenestrate'] >>> for x in a: print(x, len(x))
range函数
>>> for i in range(5): print(i) 0 1 2 3 4``range(10)`` 生成了一个包含10个值的链表,它用链表的索引值填充了这个长度为 10的列表,所生成的链表中不包括范围中的结束值。也可以让range操作从另一个数值开始,或者可以指定一个不同的步进值(甚至是负数,有时这也被称为 “步长”):: range(5, 10) 5 through 9 range(0, 10, 3) 0, 3, 6, 9 range(-10, -100, -30) -10, -40, -70
while语句
while True: pass #pass语句 # Busy-wait for keyboard interrupt (Ctrl+C)
lambda语句
>>> def make_incrementor(n): return lambda x: x + n>>> f = make_incrementor(42)>>> f(0)42>>> f(1)43
函数文档
>>> def my_function(): """Do nothing, but document it....... No, really, it doesn't do anything.... """ pass>>> print(my_function.__doc__) Do nothing, but document it. No, really, it doesn't do anything