1. 程式人生 > >【python】入門指南:控制語句

【python】入門指南:控制語句

pan else pre 循環 clas python continue break for

條件控制

if,if-else,if-elseif-else

#!/bin/python

a = test
if a == test:
    print(a is %s  %(a))
else:
    print(a is not test)

if a == test:
    print(a is test)

a = other
if a == test:
    print(a is test)
elif a == test1:
    print(a is test1)
else:
    print
(a is not test or test1)

輸出結果:

a is test
a is test
a is not test or test1

for循環控制

for循環中,加入控制流程:for-continue(繼續循環),for-break(跳出循環)

#!/bin/python

for i in range(1, 3): 
    print(i)

for i in [1, 2, 3]: 
    print(i)

for i in range(0, 100):
    if i <= 20: 
        continue
else: if i >= 25: break else: print(i)

輸出結果:

1
2
1
2
3
21
22
23
24

while循環控制

#!/bin/python

i = 0 
while i <= 20: 
    if i < 10: 
        i += 1
        continue;
    elif i >= 10 and i <= 15: 
        i += 1
    elif
i == 16: break else: i += 1 print(i)

輸出結果:

11
12
13
14
15
16

【python】入門指南:控制語句