Python从0到1——循环和判断结构
其他常用操作见:https://blog.youkuaiyun.com/qq_33302004/article/details/112859327
目录
1.常规 while 循环
temp = 0
while temp < 10:
print(temp)
temp += 1
2.使用set进行while循环
temps = {'123', '456', '789'}
print(type(temps),temps)
while temps:
temp = temps.pop() # set 是pop第一个,但是set本身的顺序是不一定的
print(temp)
3.使用list进行while循环
temps = ['123', '456', '789']
print(type(temps),temps)
print(temps[0],temps[2])
while temps:
temp = temps.pop() # list 是pop最后一个元素
print(temp)
4.使用list进行for循环
temps = ['123', '456', '789']
for i in temps:
print(i)
5.使用range进行for循环
range()可以理解为下标索引
temp = 10
for i in range(temp):
print(i)
6.使用dict进行for循环
temp = {'math':98, 'chinese':99, 'english':100}
for key in temp.keys():
print(key, temp[key])
7.循环和判断结合
temp = [1,2,3,4,5,6]
for i in temp:
if(i%2 == 0):
print(i)
elif(i%5 == 0):
break
else:
continue