Python之條件判斷與循環
1. Python之if語句
score = 75
if score >= 60:
print 'passed'
2. Python之if-else
score = 55
if score >= 60:
print 'passed'
else:
print 'failed'
3. Python之if-elif-elsese
score = 85
if score >= 90:
print 'excellent'
elif score >= 80:
print 'good'
elif score >= 60:
print 'passed'
else:
print 'failed'
5. Python之while循環
L = [75, 92, 59, 68]
sum = 0.0
for x in L:
sum = sum + x
print sum / 4
6. Python之break退出循環
sum = 0
x = 1
while x < 100:
sum = sum + x
x = x + 2
print sum
7. Python之continue繼續循環
sum = 0
x = 1
n = 1
while True:
if n > 20:
break
sum = sum + x
x = x * 2
n = n + 1
print sum
8. Python之continue繼續循環判斷
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break
if x % 2 == 0:
continue
sum = sum + x
print sum
for x in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
for y in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
if x < y:
print x * 10 + y
8. Python之continue繼續循環文章請關注PHP中文網!