1. while循环
#循环输入1-10的值
i = 1
while i <=10:
print(i,end=" ") # end=" " 取消换行符
i +=1 #累加条件
1 2 3 4 5 6 7 8 9 10
#循环输出10-1的值
i = 10
while i >=1:
print(i,end=" ")
i -=1
print()
10 9 8 7 6 5 4 3 2 1
#循环输出1-10的奇数
i= 1
while i <= 10:
if i%2 == 1 :
print(i ,end=" ")
i += 2
print()
1 3 5 7 9
#累加1-10的值
i = 1
res = 0 #初始化一个存放累加结果的变量
while i <= 10:
res += i #将每次循环的i累加到res变量中
i += 1
print("累加结果: ",res)
累加结果: 55
2. for … in循环
# for ... in 循环的使用
#b遍历字符串
for c in "zhangsan":
print(c,end = " ")
print()
#遍历列表
for c in [10,20,30]:
print(c,end=" ")
print()
#遍历元组
for c in (30,40,50):
print(c,end=" ")
print()
#遍历字典
stu = <