• Python——流程控制


    1. i f if if判断

    语句格式如下:

    if 表达式:
    	语句1
    	语句2
    
    • 1
    • 2
    • 3

    注意❗️:“语句1”、“语句2”前有一个缩进(相当于4个空格)不能省略。 i f if if判断语句是根据它旁边表达式的真假来决定要不要执行下面的语句。也就是 i f if if右边表达式的值只有 T r u e True True F a l s e False False两种。

    a = 1
    b = 2
    c = 0
    if a < b:
        print("you are right!")
    #you are right!
    if a > b:
        print("you are wrong!")
    #
    if a:
    	print("yes");
    #yes
    if c:
    	print("no");
    #
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    对于上面的代码,由于 a < b a < b a<b是正确的,所以它的值是 T r u e True True,执行该 i f if if判断下面的语句。而 a > b a > b a>b是错误的,所以它的值是 F a l s e False False,不会执行该 i f if if判断下面的语句。

    此外还要说明一种情况👉:当表达式是一个空的数据结构时,它也是 F a l s e False False值,例如👇:

    a = [ ]
    b = ( )
    c = { }
    if a:
        print("yes")
    #
    if b:
        print("yes")
    #
    if c:
        print("yes")
    #
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ❗️:上面的空列表、空元组和空字典都被判断为 F a l s e False False,但是只要这些数据结构中有一个元素,就会被判断为 T r u e True True

    e l s e else else语句

    e l s e else else跟在 i f if if后,只要 i f if if的表达式判断为 F a l s e False False,就执行后面的 e l s e else else语句。❗️❗️❗️: e l s e else else不能单独出现,只能跟在 i f if if后。例如👇:

    a = 1
    b = 2
    if a > b:
        print("a > b")
    #
    else:
        print("a < b")
    #a < b
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    e l i f elif elif语句

    单独一个 e l s e else else有时候也许不能满足要求,这时就可以用 e l i f elif elif e l i f elif elif可以有多个。 e l i f elif elif e l s e else else都是 i f if if的可选项。且 e l i f elif elif也不能单独出现,只能跟在 i f if if后面,当既有 e l i f elif elif也有 e l s e else else时, e l s e else else要只能在最后,不能插到 e l i f elif elif前面去。例如👇:

    a = 1
    b = 2
    if a > b:
        print("a > b")
    #
    elif a < b:
        print("a < b")
    #a < b
    else:
        print("a == b")
    #
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2. 循环

    w h i l e while while循环

    它的语法格式👇:

    while 表达式:
    	语句1
    	语句2
    
    • 1
    • 2
    • 3

    它的意思就是只要表达式的值为 T r u e True True,就循环执行 w h i l e while while里面的语句。直到表达式的值为 F a l s e False False就退出循环。

    a = 1;
    while a <= 5:
        print(a)
        a += 1
    #1
    #2
    #3
    #4
    #5
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    ❗️:在使用循环的过程中,要避免死循环。什么是死循环❓死循环就是 w h i l e while while旁边的表达式一直为 T u r e Ture Ture,不会停止。
    例如👇:

    a = 1;
    while a > 0:
        print(a)
        a += 1
    
    • 1
    • 2
    • 3
    • 4

    上面的循环就是一个死循环,因为循环内 a a a一直在加1,而它本身又是大于0的,所以循环就结束不了,最终可能导致系统资源被耗尽。

    f o r for for循环

    语法👇:

    for 变量 in 序列:
    	语句
    
    • 1
    • 2

    f o r for for循环跟 w h i l e while while循环有点不一样, w h i l e while while循环是判断通过表达式的是值否为 T r u e True True来选择是否执行其内的语句。而 f o r for for循环则是用 x x x去遍历序列中所有元素,遍历完后就结束循环,也就是说 f o r for for循环是规定了循环次数的。

    for x in {1,2,3}:
        print(x + 1)
    #2
    #3
    #4
    
    • 1
    • 2
    • 3
    • 4
    • 5

    也可以用 r a n g e range range函数来表示循环的次数。

    for x in range(3):
        print(x)
    #0
    #1
    #2
    for x in range(3):
        print(1)
    #1
    #1
    #1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    r a n g e range range函数若只有一个参数,参数为元素的个数,默认从0开始。💨同时也可以有两个或三个参数。两个参数的情况就是一个开始位置到结束位置,但不包括结束位置。还有一个就是增长步进,默认为1。

    b r e a k break break

    其作用是立即退出循环体,直接结束循环,例如👇:

    for x in range(4):
        if x == 2:
            break
        print(x)
    #0
    #1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    c o n t i n u e continue continue

    其作用是跳过当前循环而执行下一次循环,例如👇:

    for x in range(1,6):
        if x == 3:
            continue
        print(x)
    #1
    #2
    #4
    #5
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3. 其它

    p a s s pass pass

    在python中, p a s s pass pass是一个空语句,其作用是保持程序结构的完整性。 p a s s pass pass不做任何操作,一般用做占位语句,例如👇:

    for x in range (1,4):
        if x > 2:
            pass
        print(x)
    #1
    #2
    #3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    可以看出来 p a s s pass pass语句没有任何影响。

    循环语句中的 e l s e else else

    e l s e else else w h i l e while while
    w h i l e while while的条件表达式的值为 F a l s e False False时就会去执行 e l s e else else,例如👇:

    x = 0
    while x <= 2:
        print(x)
        x += 1
    else:
        print("end")
    #0
    #1
    #2
    #end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ❗️:如果循环中有 b r e a k break break则不会执行 e l s e else else,下同。

    e l s e else else f o r for for

    for x in range(3):
        print(x)
    else:
        print(x)
    #0
    #1
    #2
    #2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    宋浩高等数学笔记(三)微分中值定理
    生成式预训练语言模型能否视作闭卷问答的知识库?
    移动宣传舞台车设计及运动仿真(lunwen+开题报告+初稿+cad图纸)
    【自用14.2】C++俄罗斯方块
    Stream API
    MySQL_01_概述
    同步推送?苹果计划本月推出 iOS17和iPadOS17,你的手机支持吗?
    玩转webpack(02):webpack基础使用
    【2022秋招面经】——NLP
    为开发GPT-5,OpenAI向微软寻求新融资
  • 原文地址:https://blog.csdn.net/weixin_62917800/article/details/126345317