1.条件语句
if语句
Python中if语句的一般形式如下所示:
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
如果 “condition_1” 为 True 将执行 “statement_block_1” 块语句
如果 “condition_1” 为False,将判断 “condition_2”
如果"condition_2"为 True 将执行 “statement_block_2” 块语句
如果 “condition_2” 为False,将执行"statement_block_3"块语句
注:
- Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else。
- 每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。
例1
temp=input("不妨猜一下我心里想的是哪个数字:")
guess=int(temp)
if guess==8:
print("你是我肚子里的蛔虫吗")
print("猜对了也没有奖励")
elif guess>8:
print("大了")
else:
print("小了")
print("游戏结束")
例2
temp=input("不妨猜一下我心里想的是哪个数字:")
guess=int(temp)
if guess==8:
print("你是我肚子里的蛔虫吗")
print("猜对了也没有奖励")
else:
if guess>8:
print("大了")
else:
print("小了")
print("游戏结束")
如例1、例2所示,elif等同于else if,但是elif不需缩进。
2.循环语句
while循环
Python 中 while 语句的一般形式:
while 判断条件:
语句
注:
- 判断条件后需要加冒号;
- 缩进;
- python中没有do…while循环。
例3
temp=input("不妨猜一下我心里想的数字是几:")
guess=int(temp)
while guess!=8:
if guess>8
print("大了")
temp=input("请重新输入:")
guess=int(temp)
else:
print("小了")
temp=input("请重新输入:")
guess=int(temp)
print("你是我肚子里的蛔虫吗")
print("猜中了也没奖励")
print("游戏结束")
例4: 1到100求和
n = 100
sum = 0
counter = 1
while counter <= n:
sum = sum + counter
counter += 1
#print("1 到 %d 之和为: %d" % (n,sum)) #why
print("1到100的和是:"+str(sum))
while 循环使用 else 语句
在 while … else 在条件语句为 false 时执行 else 的语句块。
语法格式如下:
while <expr>:
<statement(s)>
else:
<additional_statement(s)>
例5
count = 0
while count < 5:
print (count, " 小于 5")
count = count + 1
else:
print (count, " 大于或等于 5")
输出结果为:
注:无限循环可以使用 CTRL+C 来中断循环。
for语句
for循环的一般格式如下:
for <variable> in <sequence>:
<statements>
else:
<statements>
例6:用for循环语句遍历列表
languages = ["C", "C++", "Perl", "Python"]
for x in languages:
print(x)
输出结果为:
例7:用break语句跳出当前循环体
sites = ["Baidu", "Google","Runoob","Taobao"]
for site in sites:
if site == "Runoob":
print("菜鸟教程!")
break
print("循环数据 " + site)
else:
print("没有循环数据!")
print("完成循环!")
执行脚本后,在循环到 "Runoob"时会跳出循环体。
range()函数
- 遍历数字序列
for i in range(5):
print(i)
输出为0、1、2、3、4。
- 也可以使用range指定区间的值:
for i in range(5,9) :
print(i)
输出为5、6、7、8。
注意:端点值的取法。
- 也可以使range以指定数字开始并指定不同的增量(甚至可以是负数,有时这也叫做’步长’):
for i in range(0, 10, 3) :
print(i)
输出结果为:0、3、6、9。
- 结合range()和len()函数以遍历一个序列的索引:
a= ['Google', 'Baidu', 'Runoob', 'Taobao', 'QQ']
for i in range(len(a)):
print(i, a[i])
输出结果为:
5.使用range()函数来创建一个列表:
a=list(range(5))
print(a)
输出为:[0, 1, 2, 3, 4]。
break语句
break 语句可以跳出 for 和 while 的循环体。
- while语句中使用break:
n = 5
while n > 0:
n -= 1
if n == 2:
break
print(n)
print('循环结束。')
- for语句中使用break:
for letter in 'ncepu':
if letter == 'p':
break
print ('当前字母为 :', letter)
输出e后跳出循环。
continue语句
continue 语句被用来告诉 Python 跳过当前循环块中的剩余语句,然后继续进行下一轮循环。
- while 中 使用 continue:
n = 5
while n > 0:
n -= 1
if n == 2:
continue
print(n)
print('循环结束。')
输出为:
- for语句中使用continue:
for letter in "ncepu":
if letter == "e": # 字母为 e 时跳过输出
continue
print ('当前字母 :', letter)
输出结果没有e。
循环语句中的else语句
用于查询质数的循环例子:
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(n, '等于', x, '*', n//x)
break
else:
# 循环中没有找到元素
print(n, ' 是质数')
pass语句
Python中pass是空语句,是为了保持程序结构的完整性。
pass 不做任何事情,一般用做占位语句。
for letter in 'ncepu':
if letter == 'e':
pass
print('执行 pass 块')
print('当前字母 :', letter)
print("Good bye!")
输出:
当前字母 : n
当前字母 : c
执行 pass 块
当前字母 : e
当前字母 : p
当前字母 : u
Good bye!
参考文献
https://www.runoob.com/python3/python3-loop.html
https://www.bilibili.com/video/av4050443/?p=7