while循环
while循环语法格式:
while 条件:
代码块...
示例:
count = 0
while count < 10:
print("count:",count)
count += 1
print("循环结束!")
for循环
for循环语法格式:
for 变量 in 序列:
代码块
示例:
# for循环遍历元组
tuple = (1, 3, 5, 6, 7, 9)
for i in tuple:
print(i)
# for循环遍历列表
list = [12, 43, 5, 109.4, 'aab']
for ele in list:
print(ele)
# for循环遍历数组
mydict = {'chinese':90 ,'Math':88, 'English':89}
for key, value in mydict.items():
print(key,':',value)
嵌套循环
示例
# 99 乘法表
while n <= 9:
for m in range(n, 10):
print("%d * %d = %2d" % (n, m, n * m), end=" ")
print("")
n = n + 1
控制循环结构
- break:结束一个循环,跳出循环体。
- continue:结束当次循环,开始下一次循环。
- return:返回数据(变量、常量、表达式),结束函数或方法