• 循环语句


    while循环
    aa = 100
    
    bb = 0
    cc = 1
    while cc <= aa:
        bb = bb + cc
        cc += 1
    print("1 到 %d 之和为:%d" % (aa, bb))
    
    n = 100
    
    sum = 0
    counter = 1
    while counter <= n:
        sum = sum + counter
        counter += 1
    
    print("1 到 %d 之和为: %d" % (n, sum))
    
    无限循环
    a = 1
    while a ==1:
        b = int(input("请输入一个数字>>>"))
        print("你输入的数字是>>>",b)
    
    print("good bye!")
    
    while 循环使用else语句
    count = 0
    while count < 5:
        print(count,"小于5")
        count = count + 1
    else:
        print(count,"大于5")
    
    range函数
    for i in range(10):
        print("i",i)
    
    for s in range(5,10):
        print("s:",s)
    
    break和continue语句及循环else子句
    for wo in "long":
        if wo == "li":
            break
        print("当前的字母是>>>",wo)
    
    a = 100
    while a > 0:
        print("当前变量为>>>",a)
        a = a - 5
        if a == 4:
            break
    print("good bye !")
    
    continue语句用来告诉python跳过当前循环语句块中的语句进行下一个循环
    for wo in "long":
        if wo == "o":
            continue
        print("当前的字母是>>>",wo)
    
    a = 100
    while a > 0:
        a = a -5
        if a == 0:
            continue
        print("当前的变量为>>>",a)
    
    循环语句可以有else子句,它在穷尽列表(以for循环)或条件变为false(以while循环)导致循环终止时被执行
    但循环被break终止时不执行
    for n in range(2,10):
        for x in range(2,n):
            if n % x == 0:
                print(n,"等于",x,"*", n //x )
                break
        else:
            print(n,"是质数")
    
    pass 语句是空语句,是为了保持程序结构的完整性
    for wo in "long":
        if wo == "o":
            pass
            print("执行pass快")
        print("当前字母>>>",wo)
  • 相关阅读:
    POJ 1751 Highways (kruskal)
    POJ 2031 Building a Space Station
    UVA 624
    POJ 1502 MPI Maelstrom (Dijkstra)
    POJ 3259 Wormholes(SPFA判负环)
    HZAU 1199 Little Red Riding Hood(水DP)
    HZAU 1205 Sequence Number(最大值前后缀 +双指针 + 二分)
    HZAU 1209 Deadline (hash 贪心 水题不水)
    STL完整版整理
    set集合完整版整理
  • 原文地址:https://www.cnblogs.com/lichenglong521/p/9541228.html
Copyright © 2020-2023  润新知