资源链接:【Python学习笔记】第五章 程序流程控制.pdf 295KB
Python的设计理念很简单,刚好够用。多分支结通过
if-elif-else
实现
if语句有三种结构:
if
if-else
if-elif-else
if
结构用法如下:
if 条件: # 以英文冒号结尾
语句组 # 缩进(Python中推荐使用4个空格)
示例代码:
# coding = utf-8
# 代码文件: ch_5_1_1.py
mark = int(input('Please enter an integer form 0 to 100: ')) # input()函数获得用户输入字符串, int() 函数将字符串转为整数。
if mark >= 86:
print('Nice work!')
if mark < 50 :
print('You need work hard!')
if (mark >= 50 ) and (mark <86):
print('Your mark is fine, you can do better!')
--------------------------------------------------------------------------------------
# 运行输出文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_1_1.py =============
Please enter an integer form 1 to 100: 55 # 输入整数,程序正常输出。
Your mark is fine, you can do better!
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_1_1.py =============
Please enter an integer form 1 to 100: 95 # 输入整数,程序正常输出。
Nice work!
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_1_1.py =============
Please enter an integer form 1 to 100: 85.6 # 输入小数,不能转化为整数,程序运行失败。
Traceback (most recent call last):
File "/Users/fukang/Downloads/FInal/timetable.py", line 1, in <module>
mark = int(input('Please enter an integer form 1 to 100: '))
ValueError: invalid literal for int() with base 10: '85.6'
if-else
结构用法如下:
if 条件:
语句组1
else: # 以英文冒号结尾
语句组2 # 缩进(Python中推荐使用4个空格)
示例代码:
# coding = utf-8
# 代码文件: ch_5_1_2.py
mark = int(input('Please enter an integer form 0 to 100: '))
if mark >= 50:
if mark >= 86:
print('Nice work!')
else:
print('Your mark is fine, you can do better!')
else:
print('You need work hard!')
------------------------------------------------------------------------------
# 运行输出文件:
============== RESTART: /Users/不二最帅/Downloads/FInal/ch_5_1_2.py=============
Please enter an integer form 1 to 100: 95
Nice work!
============== RESTART: /Users/不二最帅/Downloads/FInal/ch_5_1_2.py =============
Please enter an integer form 1 to 100: 40
You need work hard!
============== RESTART: /不二最帅/fukang/Downloads/FInal/ch_5_1_2.py =============
Please enter an integer form 1 to 100: 77
Your mark is fine, you can do better!
if-elif-else
结构的语法如下:
if 条件 1:
语句组 1
elif 条件 2:
语句组 2
elif 条件 3:
语句组 3
。。。
elif 提条件 n:
语句组 n
else:
语句组 n+1
示例代码:
# coding = utf-8
# 代码文件: ch_5_1_3.py
mark = int(input('Please enter an integer form 0 to 100: '))
if mark >= 90:
grade = 'A'
elif mark >= 80:
grade = 'B'
elif mark >= 70:
grade = 'C'
elif mark >= 60:
gradee = 'D'
else:
grade = 'F'
print('Grade = ' + grade)
-----------------------------------------------------------------------------
# 运行输出文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_1_3.py =============
Please enter an integer form 0 to 100: 17
Grade = F
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_1_3.py =============
Please enter an integer form 0 to 100: 77
Grade = C
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_1_3.py =============
Please enter an integer form 0 to 100: 88
Grade = B
Python 支持两种循环语句:
while
和for
在 Python 中只有
while
语句,没有do-while
语句。while
语句先判断循环条件,若为True
则执行循环体。
while
句的语法:
while 循环条件: # 以英文冒号结尾
循环体语句 # 注意缩进,4个空格
1. 一个简单的代码展示:
# coding = utf-8
# 代码文件: ch_5_2_1.py
i = 0
while i * i < 1000:
i += 1
print('i = ' + str(i))
print('i * i = ' +str(i * i))
--------------------------------------------------------------------------
# 通过Python指令运行文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_2_1.py =============
i = 32
i * i = 1024
2. 第二个示例代码:
# coding = utf-8
# 代码文件: ch_5_2_1.py
i = 0
while i * i < 10:
i += 1
print(str( i ) + ' * ' + str( i ) + ' = ' , i * i )
else:
print('While Over!')
--------------------------------------------------------------------------
# 通过Python指令运行文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_2_1.py =============
1 * 1 = 1
2 * 2 = 4
3 * 3 = 9
4 * 4 = 16
While Over!
3. 修改示例代码:
# coding = utf-8
# 代码文件: ch_5_2_1.pyi = 0
i = 0
while i * i < 10:
i += 1
if i ==3:
break
print(str(i) + ' + ' + str(i) + ' = ' + str(i * i))
else:
print('While Over!')
--------------------------------------------------------------------------
# 通过Python指令运行文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_2_1.py =============
1 + 1 = 1
2 + 2 = 4
Python 基于简单的设计理念,只有一种
for-in
语句,它可以遍历任意可迭代对象中的元素。
可迭代对象包括字符串、 列表、 元组、 集合和字典等
for
语句的一般格式如下:
for 变量 in 可迭代对象: # 以英文冒号结尾
循环体语句组 # 注意缩进,4个空格
1. 示例代码:
# coding = utf-8
# 代码文件: ch_5_2_1.pyi = 0
print('-----字符串-----')
for i in ' Hello': # 迭代字符串
print(i)
# 声明整数列表
number = [ 521 , 520 , 1314 , 99 , 999]
print('-----整数列表-----')
for i in number: # 迭代整数列表
print(i)
# 通过Python指令运行文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_2_1.py =============
-----字符串-----
H
e
l
l
o
-----整数列表-----
521
520
1314
99
999
2. 示例代码:
range(5)
函数返回整数序列,他的取值大于 0 且小于 5, 一共5个整数。
# coding = utf-8
# 代码文件: ch_5_2_1.pyi = 0
for i in range(5):
print(i)
else:
print('For Over!')
# 通过Python指令运行文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_2_1.py =============
0
1
2
3
4
For Over!
3. 修改示例代码:
# coding = utf-8
# 代码文件: ch_5_2_1.pyi = 0
for i in range(5):
if i == 3:
break
print(i)
else:
print('For Over!')
# 通过Python指令运行文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_2_1.py =============
0
1
2
跳转语句能够改变程序的执行顺序,包括
break
、continue
和return
。break
和continue
用于循环体中,return
用于函数中。
break
语句用于强行退出循环体,不再执行循环体剩余的语句。
示例代码:
# coding = utf-8
# 代码文件: ch_5_3_1.py
for i in range(5):
if i == 3: # 若满足条件i==3,break结束循环
break
print(i)
# 通过Python指令运行文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_3_1.py =============
0
1
2
continue
语句用于借宿本次循环,跳过循环体中尚未执行的语句,接着进行终止条件的判断,以决定是否继续循环。
示例代码:
# coding = utf-8
# 代码文件: ch_5_3_2.py
for i in range(5):
if i == 3:
continue
print(i)
# 当条件i ==3,时,执行continue语句,continue语句会终止本次循环,循环体中的continue不再执行,接着下次循环
# 通过Python指令运行文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_3_2.py =============
0
1
2
4
通过
while
循环计算出水仙花数。Tips:水仙花数是一个三位数,三位数各位的立方纸盒等于三位数本身。
参考代码:
# coding = utf-8
# 代码文件: ch_5_4.py
i = 0 ; r = 0 ; t = 0
7
while i <1000:
r = i // 100
s = (i - r *100) // 10
t = i - r * 100 - s * 10
if i == (r **3 + s ** 3 + t ** 3):
print('i = ' + str(i))
i += 1
# 通过Python指令运行文件:
============== RESTART: /Users/starkk/Downloads/FInal/ch_5_4.py =============
i = 0
i = 1
i = 153
i = 370
i = 371
i = 407
本章难点较多,需要多看看示例,多动动手。
u will be fine