zoukankan      html  css  js  c++  java
  • Python编程 从入门到实践-5if语句上

    笔记出处(学习UP主视频记录) https://www.bilibili.com/video/av35698354?p=8

    5.1 一个简单示例

    5.2 条件测试

    cars = ['audi', 'bmw', 'subaru', 'toyota']
    for car in cars:
        if car == 'bmw':
            print (car.upper())
        else:
            print (car.title())

    Audi
    BMW
    Subaru
    Toyota

    5.2.1 检查是否相等

    car = 'bmw'
    print (car == 'bmw')

    True

    car = 'audi'
    print (car == 'bmw')

    False

    5.2.2 检查是否相等时不考虑大小写

    #两个大小写不同的值会被视为不相等
    car = 'Audi' print (car == 'audi')

    False

    car = 'Audi'
    print (car.lower() == 'audi')

    True

    5.2.3 检查是否不相等

    requested_toppong = 'mushrooms'
    
    if requested_toppong != 'anchovies':
        print ("Hold the anchovies!")

    Hold the anchovies!

    5.2.4 比较数字

    age = 18
    print (age == 18)

    True

    answer = 17
    if answer != 42:
        print ("That is not the correct answer. Please try again!")

    That is not the correct answer. Please try again!

    5.2.5 检查多个条件

    1. 使用and检查多个条件

    #检查是否2个人都不小于21岁
    age_0 = 22
    age_1 = 18
    
    print (age_0 >= 21 and age_1 >= 21)

    False

    #检查是否2个人都不小于21岁
    age_0 = 22
    age_1 = 18
    
    print (age_0 >= 21 and age_1 >= 21)
    
    age_1 = 22
    print (age_0 >= 21 and age_1 >= 21)

    False
    True

    #检查是否2个人都不小于21岁
    age_0 = 22
    age_1 = 18
    
    print ((age_0 >= 21) and (age_1 >= 21))

    False

    2. 使用or检查多个条件

    #只要至少有1个条件满足,就能通过测试
    #2个测试都没有通过时,表达式结果为False
    age_0 = 22
    age_1 = 18
    
    print ((age_0 >= 21) or (age_1 >= 21))

    True

    age_0 = 22
    age_1 = 18
    
    age_0 = 18
    print ((age_0 >= 21) or (age_1 >= 21))

    False

    5.2.6 检查特定值是否包含在列表中

    requested_toppings = ['mushrooms', 'onions', 'pineapple']
    print ('mushrooms' in requested_toppings)

    True

    requested_toppings = ['mushrooms', 'onions', 'pineapple']
    print ('pepperoni' in requested_toppings)

    False

    5.2.7 检查特定值是否不包含在列表中

    banned_users = ['andrew', 'carolina', 'david']
    user = 'marie'
    if user not in banned_users:
        print (user.title() + ". you can post a response if you wish.")

    Marie. you can post a response if you wish.

    5.2.8 布尔表达式

    True False

    Caesar卢尚宇

    2020年3月12日

  • 相关阅读:
    从坐标系图中理解“空间变换”
    Normal Map中的值, Tangent Space, 求算 Tangent 与 Binormal 与 TBN Matrix
    A Personal Understanding to Matrix Transformation in Graphics
    [转] 双数组前缀树
    [转] Hive简介
    [转] 远程访问服务器Jupyter Notebook的两种方法
    [转] LSTM中的pack和pad
    [转] Beam Search
    [转] linux提示符过长tips
    [转] batch normalization相关
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12468959.html
Copyright © 2011-2022 走看看