if-elif-else事例

score = 85
if score>=90:
    print 'excellent'
elif score<90 and score>=80:
    print 'good'
elif score<80 and score>=60:
    print 'passed'
else:
    print 'failed'

for循环事例

计算平均数

L = [75, 92, 59, 68]
sum = 0.0
for x in L:
    sum = sum + x
print sum / 4

while事例

利用while循环计算100以内奇数的和

sum = 0
x = 1
while x<100:
    sum = sum + x
    x = x+2
print sum

break 事例

利用 while True 无限循环配合 break 语句,计算 1 + 2 + 4 + 8 + 16 + ... 的前20项的和。

sum = 0
x = 1
n = 1
while True:
    if n > 20:
        break
    sum = sum +x
    x = x * 2
    n = n + 1
print sum

coutinue 事例

对已有的计算 0 - 100 的while循环进行改造,通过增加 continue 语句,使得只计算奇数的和:

sum = 0
x = 1
while True:
    if x > 100:
        break
    if x % 2 == 0:
        continue
    sum = sum + x
    x = x + 2
print sum

双重循环

对100以内的两位数,请使用一个两重循环打印出所有十位数数字比个位数数字小的数,例如,23(2 < 3)。

for x in [1,2,3,4,5,6,7,8,9,10]:
    for y in [0,1,2,3,4,5,6,7,8,9]:
        if x < y:
            print x * 10 +y