python控制流
时间:2010-09-09 来源:biti-leaf
当我们使用任何一门语言编写程序时,有时会想改变语句流的执行顺序,比如根据时间打印“早上好”或者“晚上好”。在Python中有三种控制流语句——if、for和while。如果你熟悉一门编程语言如C、VB等,那么,这部分内容将会变的非常容易。
1.if语句
if语句用来检验一个条件, 如果条件为真,我们运行一块语句(称为 if-块 ), 否则 我们处理另外一块语句(称为 else-块 )。 else从句是可选的。
使用if语句
if expression:#冒号不能少
statement
statement#注意:相同的语句块缩进要相同
elif expression:
statement
statement
elif expression:
statement
statement
...
else:
statement
statement
other statement
例 使用if语句
#!/usr/bin/python
# Filename: if.py
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.' # New block starts here
print "(but you do not win any prizes!)" # New block ends here
elif guess < number:
print 'No, it is a little higher than that' # Another block
else:
print 'No, it is a little lower than that'
print 'Done'
# This last statement is always executed, after the if statement is executed
输出
$ python if.py
Enter an integer : 50
No, it is a little lower than that
Done
$ python if.py
Enter an integer : 22
No, it is a little higher than that
Done
$ python if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
2.while语句
只要在一个条件为真的情况下,while语句允许你重复执行一块语句。while语句是所谓循环语句的一个例子。while语句有一个可选的else从句。
使用while语句
while expression:
statement
statement
...
statement
else:
statement
other statement
例 使用while语句
#!/usr/bin/python
# Filename: while.py
number = 23
running = True
while running:
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.'
running = False # this causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that'
else:
print 'No, it is a little lower than that'
else:
print 'The while loop is over.'
print 'Done'
输出
$ python while.py
Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done
3.for循环
for..in是另外一个循环语句,它在一序列的对象上递归即逐一使用队列中的每个项目。python的for循环和VB的for循环类似。
使用for语句
for var in ...
statement
statement
else:
statement
例 使用for语句
#!/usr/bin/python
# Filename: for.py
for i in range(1, 5):
print i
else:
print 'The for loop is over'
输出
$ python for.py
1
2
3
4
The for loop is over
注:range函数其实有3个参数,第一个参数是起点(包含),第二个参数是终点(不含),第三个参数是步长,默认为1。
4.break语句
break语句是用来终止循环语句的,即哪怕循环条件不是False或序列还没有被完全递归,也停止执行循环语句。
注:如果你从for或while循环中终止,任何对应的循环else块将不执行。
使用break语句
例6.4 使用break语句
#!/usr/bin/python
# Filename: break.py
while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
print 'Length of the string is', len(s)
print 'Done'
(源文件:code/break.py)
输出
$ python break.py
Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something : use Python!
Length of the string is 12
Enter something : quit
Done
5.continue语句
continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后继续进行下一轮循环。
使用continue语句
例6.5 使用continue语句
#!/usr/bin/python
# Filename: continue.py
while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
continue
print 'Input is of sufficient length'
# Do other kinds of processing here...
1.if语句
if语句用来检验一个条件, 如果条件为真,我们运行一块语句(称为 if-块 ), 否则 我们处理另外一块语句(称为 else-块 )。 else从句是可选的。
使用if语句
if expression:#冒号不能少
statement
statement#注意:相同的语句块缩进要相同
elif expression:
statement
statement
elif expression:
statement
statement
...
else:
statement
statement
other statement
例 使用if语句
#!/usr/bin/python
# Filename: if.py
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.' # New block starts here
print "(but you do not win any prizes!)" # New block ends here
elif guess < number:
print 'No, it is a little higher than that' # Another block
else:
print 'No, it is a little lower than that'
print 'Done'
# This last statement is always executed, after the if statement is executed
输出
$ python if.py
Enter an integer : 50
No, it is a little lower than that
Done
$ python if.py
Enter an integer : 22
No, it is a little higher than that
Done
$ python if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
2.while语句
只要在一个条件为真的情况下,while语句允许你重复执行一块语句。while语句是所谓循环语句的一个例子。while语句有一个可选的else从句。
使用while语句
while expression:
statement
statement
...
statement
else:
statement
other statement
例 使用while语句
#!/usr/bin/python
# Filename: while.py
number = 23
running = True
while running:
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.'
running = False # this causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that'
else:
print 'No, it is a little lower than that'
else:
print 'The while loop is over.'
print 'Done'
输出
$ python while.py
Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done
3.for循环
for..in是另外一个循环语句,它在一序列的对象上递归即逐一使用队列中的每个项目。python的for循环和VB的for循环类似。
使用for语句
for var in ...
statement
statement
else:
statement
例 使用for语句
#!/usr/bin/python
# Filename: for.py
for i in range(1, 5):
print i
else:
print 'The for loop is over'
输出
$ python for.py
1
2
3
4
The for loop is over
注:range函数其实有3个参数,第一个参数是起点(包含),第二个参数是终点(不含),第三个参数是步长,默认为1。
4.break语句
break语句是用来终止循环语句的,即哪怕循环条件不是False或序列还没有被完全递归,也停止执行循环语句。
注:如果你从for或while循环中终止,任何对应的循环else块将不执行。
使用break语句
例6.4 使用break语句
#!/usr/bin/python
# Filename: break.py
while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
print 'Length of the string is', len(s)
print 'Done'
(源文件:code/break.py)
输出
$ python break.py
Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something : use Python!
Length of the string is 12
Enter something : quit
Done
5.continue语句
continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后继续进行下一轮循环。
使用continue语句
例6.5 使用continue语句
#!/usr/bin/python
# Filename: continue.py
while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
continue
print 'Input is of sufficient length'
# Do other kinds of processing here...
相关阅读 更多 +