循环
重复执行某一个固定的动作或者任务
for语句
基础语法:
for 临时变量(可以是多个,用逗号隔开) in iterable(可迭代元素):
某一件事情
某几件事情
再次强调:python中iterable元素:bytes str tuple list set dict
for输出不同数据类型
输入:for i in b'123':
print(i) #输出其对应Ascii码值
结果:49
50
51
输入:for i in {1: 2, 2: 3, 3: 4}:
print(i) #字典默认结果为输出他的key值
结果:1
2
3
输入:for i in {1: 2, 2: 3, 3: 4}.values():
print(i) #通过语句后指定输出键值可以改变输出
结果:2
3
4
输入:for i in {1: 2, 2: 3, 3: 4}.items():
print(i) #通过末尾指定可以以元组形式分别输出。
结果:(1, 2)
(2, 3)
(3, 4)
输入:for i, j in {1: 2, 2: 3, 3: 4}.items():
print(i, j) #指定输出多个变量
结果:1 2
2 3
3 4
嵌套语句
输入:color_list = ["red", "blue", "black"]
size_list = ["S", "M", "L"]
for i in color_list:
for j in size_list:
print(i, j)
结果: #可以看出执行一次外循环需要执行三次内循环。
red S
red M
red L
blue S
blue M
blue L
black S
black M
black L
打印99乘法表
输入:for i in range(1,10):
for j in range(1,i+1):
print(i, '*', j, "=", i*j, end="\t" )
print() #换行输出
结果:
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
break和cintinue语句
break语句
输入:for i in range(10):
if i > 5: 当满足结果时退出循环
break
print(i)
结果:
0
1
2
3
4
5
continue语句
输入:
for i in range(10):
if i == 5: #当满足条件时跳过该循环进入下一个循环
continue
print(i)
结果:
0
1
2
3
4
6
7
8
9
enumerate命令
序列下标和值对应列举出来。
输入:for index, value in enumerate([1, 2, 3]):
print(index, value)
结果:0 1
1 2
2 3
rang范围命令
明确一点:
若 range(a, b) 则范围为:a<= num <b 且 都是整数(左开右闭)
输入:for s in range(10): #默认start=0,step = 1
print(s)
结果:
0
1
2
3
4
5
6
7
8
9
输入:for r in range(1, 10): #start=1, stop=10, 默认step=1
print(r)
结果:
1
2
3
4
5
6
7
8
9
输入:for b in range(1, 10, 1): #start==1,stop=10,step=1
print(b)
结果:1
2
3
4
5
6
7
8
9
求0-100之间所有数之和
输入:sum_data = 0
for i in range(1, 101): #start=1,stop=101, step=1
sum_data += i
print(sum_data)
结果: 5050
while语句
只要条件满足,就不断循环,条件不满足时退出循环
基础语句
输入:num = 0
while num <= 4:
print(num)
num += 1
结果:1
2
3
4
嵌套语句
color_list = ["red", "blue", "black"]
size_list = ["S", "M", "L"]
print(len(color_list))
print(len(size_list))
i = 0
while i < len(color_list):
j = 0 #注意内部循环结束后内部循环需要重置才能进行下一次内部循环
while j < len(size_list):
print(color_list[i], size_list[j])
j += 1
i += 1
结果:
red S
red M
red L
blue S
blue M
blue L
black S
black M
black L
打印99乘法表
输入:
i = 1
while i < 10:
j = 1
while j <= i:
print(i, '*', j, "=", i*j, end="\t" ) #这里\t为制表符
j += 1
print()
i += 1
结果:
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81