while循环写99乘法表
for循环写99乘法表
笔记
1.while循环
index = 1 #索引定义 1为起始值
while index <= 100: #输出的是布尔值(ture false)
print("hello,world!") #循环体
index += 1
while 表达式:
循环体
(通过条件的不断变化,从而判断循环体是否执行)
while-------True:循环体
用break/continue来跳出循环
2.break:
index = 1
while index <= 10: 输出结果:
if index == 5: 1
break 2
print(index) 3
index += 1 4
Break----立即结束循环
3.Continue
index = 1
while index <= 10: 输出结果:
if index == 5: 1
continue 2
print(index) 3
index += 1 4
Continue:终止本次循环,提前开始下一次循环
index = 1
sum_ = 0
while index <= 100:
sum_ += index 此为求1-100的和
index += 1
print(sum_)
i = int(input("请输入层数"))
index = 1 请输入层数:5
while index <= i: *
j = 1 **
while j<= index: ***
print ("*",end="") ****
j += 1 *****
print()
index += 1
4.for循环
for 临时变量 in 数据容器:
代码块
list_ = [1,2,3,4]
index = 0
while index < len(list_):
print(list_[index])
index += 1
set_ ={1,2,3}
for i in set_:
print(i)
for循环不仅可以迭代无序的容器,也可以迭代有序的容器
layer = int(input("请输入打印层数:")) 请输入打印层数:3
for i in range(1,layer+1): *
a = layer - i **
for j in range(1,a+1): ***
print(" ",end="")
for j in range(1,i+1):
print("*",end="")
print()