(三)《A Byte of Python》——控制流
2018-01-17 13:05
387 查看
改变程序工作顺序,并依据不同情况去完成不同工作。
1. If语句
if语句用以检查条件:如果条件为真(True),我们将运行一块语句(称作 if-block或if块),否则 我们将运行另一块语句(称作else-block或else 块)。else从句是可选的。
number = 5
guess = int(input('Enter an integer : '))
if guess == number:
# 新块从这里开始
print('Congratulations, you guessed it.')
# 新块在这里结束
elif guess < number:
# 另一代码块
print('No, it is a little higher than that')
# 你可以在此做任何你希望在该代码块内进行的事情
else:
print('No, it is a little lower than that')
# 你必须通过猜测一个大于(>) 设置数的数字来到达这里。
print('Done')
input()返回的数据类型是str,str不能直接和整数比较,必须先把str转换成整数。
2. while语句
能够让你在条件为真的前提下重复执行某块语句。while语句是 循环(Looping) 语句的一种,同样可以拥有else子句作为可选选项。
number = 5
running = True
while running:
guess = int(input('Enter an integer : '))
if guess == number:
print('Congratulations, you guessed it.')
# 这将导致 while 循环中止
running = False
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')
3. for循环
for...in 语句是另一种循环语句,其特点是会在一系列对象上进行迭代(Iterates) ,意即它会遍历序列中的每一个项目。
for i in range(1, 5): #在C++中则需写为for(int i=0;i<5;i++)
print(i)
else:
print('The for loop is over')
4. break语句
用以中断(Break) 循环语句,也就是中止循环语句的执行,即使循环条件没有变更为False,或队列中的项目尚未完全迭代依旧进行中断 。如果你的 中断 了一个for或 while循环,任何相应循环中的else块都将不会被执行。
while True:
s = input('Enter something : ')
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')
跳过当前循环块中的剩余语句,并继续该循环的下一次迭代。
while True:
s = input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
print('Too small')
continue
print('Input is of sufficient length')
1. If语句
if语句用以检查条件:如果条件为真(True),我们将运行一块语句(称作 if-block或if块),否则 我们将运行另一块语句(称作else-block或else 块)。else从句是可选的。
number = 5
guess = int(input('Enter an integer : '))
if guess == number:
# 新块从这里开始
print('Congratulations, you guessed it.')
# 新块在这里结束
elif guess < number:
# 另一代码块
print('No, it is a little higher than that')
# 你可以在此做任何你希望在该代码块内进行的事情
else:
print('No, it is a little lower than that')
# 你必须通过猜测一个大于(>) 设置数的数字来到达这里。
print('Done')
$ python if.py Enter an integer : 9 No, it is a little lower than that Done $ python if.py Enter an integer : 2 No, it is a little higher than that Done $ python if.py Enter an integer : 5 Congratulations, you guessed it. Doneelif 语句实际上将两个相连的if else-if else语句合并成一句if-elif-else语句。这能够使程序更加简便,并且可以减少所需要的缩进量 。需要注意的是,Python中不存在switch语句。
input()返回的数据类型是str,str不能直接和整数比较,必须先把str转换成整数。
2. while语句
能够让你在条件为真的前提下重复执行某块语句。while语句是 循环(Looping) 语句的一种,同样可以拥有else子句作为可选选项。
number = 5
running = True
while running:
guess = int(input('Enter an integer : '))
if guess == number:
print('Congratulations, you guessed it.')
# 这将导致 while 循环中止
running = False
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 : 9 No, it is a little lower than that. Enter an integer : 2 No, it is a little higher than that. Enter an integer : 5 Congratulations, you guessed it. The while loop is over. Done与之前的if程序相比,while能够允许用户持续猜测直至他猜中为止——而无需在每次猜测时都要重新运行程序。
3. for循环
for...in 语句是另一种循环语句,其特点是会在一系列对象上进行迭代(Iterates) ,意即它会遍历序列中的每一个项目。
for i in range(1, 5): #在C++中则需写为for(int i=0;i<5;i++)
print(i)
else:
print('The for loop is over')
$ python for.py 1 2 3 4 The for loop is overrange函数为左闭右开的,默认步长为1,因此只会输出从1-4的4个数字。若为range(1,5,2) ,则会输出[1,3]。range()每次只会生成一个数字,如果你希望获得完整的数字列表,要在使用range()时调用list() 。
4. break语句
用以中断(Break) 循环语句,也就是中止循环语句的执行,即使循环条件没有变更为False,或队列中的项目尚未完全迭代依旧进行中断 。如果你的 中断 了一个for或 while循环,任何相应循环中的else块都将不会被执行。
while True:
s = input('Enter something : ')
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')
$ python break.py Enter something : Programming is fun Length of the string is 18 Enter something : quit Done
n = 1 while n <= 100: if n > 10: # 当n = 11时,条件满足,执行break语句 break # break语句会结束当前循环 print(n) n = n + 1 print('END')5. continue语句
跳过当前循环块中的剩余语句,并继续该循环的下一次迭代。
while True:
s = input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
print('Too small')
continue
print('Input is of sufficient length')
$ python continue.py Enter something : a Too small Enter something : 123 Input is of sufficient length Enter something : quit
n = 0 while n < 10: n = n + 1 if n % 2 == 0: # 如果n是偶数,执行continue语句 continue # continue语句会直接继续下一轮循环,后续的print()语句不会执行 print(n)
相关文章推荐
- A Byte of Python 笔记(4)控制流:if、for、while、break、continue
- A Byte of Python (1)安装和运行
- 【A ByteOf Python2.7】第13章 异常
- Byte-of-python笔记代码3:Object.py
- 【A ByteOf Python2.7】第4章 基本概念
- A Byte of Python 笔记(3)运算符和表达式
- A Byte of Python 简明Python教程 读书笔记
- 《A Byte of Python》笔记
- Byte of Python笔记(一)
- 【A ByteOf Python2.7】第5章 运算符与表达式
- A Byte of Python 笔记(6)模块
- a byte of python
- (二)《A Byte of Python》 ——运算符与表达式
- 【A ByteOf Python2.7】第2章 安装Python
- 【A ByteOf Python2.7】第6章 控制流
- A Byte of Python -- Functions
- 《A Byte of Python》读书笔记
- (六)《A Byte of Python》——数据结构
- A Byte of Python(简明Python教程) for Python 3.0 下载
- Byte of Python笔记(二)