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') ... elif x == 1: ... print('Single') ... else: ... print('More') ...
for 敍述
>>> # Measure some strings: ... words = ['cat', 'window', 'defenestrate'] >>> for w in words: ... print(w, len(w)) ... cat 3 window 6 defenestrate 12
>>> for w in words[:]: # Loop over a slice copy of the entire list. ... if len(w) > 6: ... words.insert(0, w) ... >>> words ['defenestrate', 'cat', 'window', 'defenestrate']
Range (n) 產生0~n-1序列
>>> for i in range(5): ... print(i) ... 0 1 2 3 4
a = ['Mary', 'had', 'a', 'little', 'lamb'] for i in range(len(a)): print(i, a[i]) 0 Mary 1 had 2 a 3 little 5 lamb
while 敍述
a = ['Mary', 'had', 'a', 'little', 'lamb'] i=0while i < len(a):print(i, a[i])i+=10 Mary 1 had 2 a 3 little 4 lamb
沒有留言 :
張貼留言