zoukankan      html  css  js  c++  java
  • Python分支结构你真的搞定了吗?

    分支结构

    分支结构能够让计算机像人一样进行思考,应对不同的场景做出不同的回应。

    Python中不支持switch语法,目前仅支持if/else形式,但是在Python3.10的测试版本中,貌似支持了switch语法,这里不做例举。

    if

    多条if语句出现会逐行进行判断,条件为真则执行其下的代码块,条件为假则略过:

    if 条件判断:
        逻辑代码...
    if 条件判断:
        逻辑代码...
    if 条件判断:
        逻辑代码...
    

    示例演示:

    userAge = int(input("请输入你的年龄:"))
    if userAge < 18:
        print("少年")
    if userAge >= 18 and userAge < 30:
        print("青年")
    if userAge >= 30 and userAge < 60:
        print("中年")
    if userAge >= 60 and userAge < 80:
        print("老年")
    if userAge >=80:
        print("尚能饭矣")
    

    if/else

    if代表如果怎样就怎样,else代表否则怎样就怎样。

    一组if/else只会执行其中的一个。

    以下是Python中if/else语法:

    if 条件判断:
        逻辑代码...
    else:
        逻辑代码...
    

    示例演示:

    userinput = input("输入任意字符,判断是否为数字串:")
    if userinput.isdigit():
        print("是数字串")
    else:
        print("不是数字串")
    

    if/elif/else

    多条if会按顺序依次执行,对每一条if语句都进行判定,而如果使用elif则只会从多条逻辑判定中取出最先为True的进行执行,后续的判定将不会被执行。

    也就是说,if/elif/else三者只会执行一个。

    语法使用如下:

    if 条件判断:
        逻辑代码...
    elif 条件判断:  # 如果条件成立,剩下同级别下的elif与else都不将再继续执行。
        逻辑代码...
    elif 条件判断:
        逻辑代码...
    else:
        逻辑代码
    

    示例演示:

    userAge = int(input(">>>"))
    if userAge < 18:
        print("少年")
    elif userAge < 30:
        print("青年")
    elif userAge < 60:
        print("中年")
    elif userAge < 80:
        print("老年")
    else:
        print("尚能饭矣")
    

    三元表达式

    普通形式

    如果只是一个简单if/else判定,我们可以将代码写在一行,语法如下:

    <on_true> if <condition> else <on_false>
    

    示例演示:

    age = int(input(">>>")) 
    result = "成年" if age >= 18 else "未成年"
    print(result)
    

    其他形式

    上面的三元表达式是最常见的一种,除此之外再介绍几种不常见的。

    第二种,这种有一个BUG,不能区分0或者Fasle:

    <condition> and <on_true> or <on_false>
    

    如下所示,如果1大于0就返回0,否则返回False,但是第二种的返回的结果永远是False:

    result = 1 > 0 and 0 or False
    print(result)
    
    # False
    

    如果使用第一种,就不会有这样的问题:

    result = 0 if 1 > 0 else False
    print(result)
    
    # False
    

    第三种,语法如下:

    (<on_false>, <on_true>)[condition]
    

    示例如下:

    age = int(input(">>>"))
    result = ("未成年", "已成年") [age >= 18]
    print(result)
    

    第四种,语法如下:

    {True: <on_true>, False: <on_false>}[<condition>]
    

    示例如下:

    age = int(input(">>>"))
    result = {True: "已成年", False: "未成年"}[age >= 18]
    print(result)
    

    Python语法糖

    链式比较

    在Python中的判定支持一种链式比较,下面是常规的比较:

    print(3 > 2 and 2 > 1) 
    print(3 > 1 and 1 > 2) 
    
    # True
    # False
    

    通过链式比较进行简写:

    print(3 > 2 > 1) 
    print(3 > 1 > 2) 
    
    # True
    # False
    

    获得布尔值

    判定用户输入的是否为数字串,如果为数字串result变量为True,否则为False。

    很多情况下,初学者可能会写出下面这种代码:

    userInput = input(">>>")
    result = None
    if userInput.isdigit():
        result = True
    else:
        result = False
    print(result)
    

    其实一行代码就可以搞定:

    userInput = input(">>>")
    result = bool(userInput.isdigit())
    print(result)
    

    或者你也可以使用另一种方式:

    userInput = input(">>>")
    result = userInput.isdigit() or False
    print(result)
    
  • 相关阅读:
    282. Expression Add Operators
    281. Zigzag Iterator
    280. Wiggle Sort
    How Not to Crash #2: Mutation Exceptions 可变异常
    Moving Swiftly
    How to Use updateConstraints
    Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中
    Where-To-Put-The-Auto-Layout-Code
    iOS five years[转]
    ResponderChain note
  • 原文地址:https://www.cnblogs.com/Yunya-Cnblogs/p/14771286.html
Copyright © 2011-2022 走看看