for、while循环

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值