美文网首页
2024-01-28_Python的if语句

2024-01-28_Python的if语句

作者: 微笑碧落 | 来源:发表于2024-02-02 16:03 被阅读0次

    1. Python的if语句基本语法

    • else语句可以省略
    age = 12 
    if age < 4: 
      price = 0 
    elif age < 18: 
      price = 5 
    elif age < 65: 
      price = 10
    print("Your admission cost is $" + str(price) + ".")
    
    • 支持 5 < age <20
    • 支持not in 和 !=判断
    • and和or为快速and和快速or。假如or语句的第一个判断返回为True,则不执行第二个判断语句
    if 5 < age <20:
      pass
    
    banned_users = ['andrew', 'carolina', 'david']
    user = 'marie'
    if user not in banned_users:
        print(user.title() + ", you can post a response if you wish.")
    
    car = 'subaru' 
    print("Is car == 'subaru'? I predict True.") 
    print(car == 'subaru') 
    print("\nIs car != 'audi'? I predict False.") 
    print(car != 'audi')
    
    • if语句没有变量作用域,也就是if语句下的变量可以在外部直接使用
    age = 3
    if 5 < age < 18:
        price = 3
    else:
        price = 8
    print(f"Your admission cost is ${price}.")
    

    相关文章

      网友评论

          本文标题:2024-01-28_Python的if语句

          本文链接:https://www.haomeiwen.com/subject/lhpiodtx.html