zoukankan      html  css  js  c++  java
  • 条件、逻辑运算符、成员运算与身份运算

    '''
    什么是条件?什么可以当做条件?为何要要用条件?
    第一大类:显式布尔值
    条件可以是:比较运算符
    age = 18
    print(age > 16) # 条件判断之后会得到一个布尔值

    条件可以是:True、False
    is_beautiful=True
    print(is_beautiful)


    第二大类:隐式布尔值,所有的值都可以当成条件去用
    其中0、None、空(空字符串、空列表、空字典)=》代表的布尔值为False,其余都为真
    '''
    '''
    一:not、and、or的基本使用
    not:就是把紧跟其后的那个条件结果取反
    ps:not与紧跟其后的那个条件是一个不可分割的整体

    and:逻辑与,and用来链接左右两个条件,两个条件同时为True,最终结果才为真
    print(True and 10 > 3)

    or:逻辑或,or用来链接左右两个条件,两个条件但凡有一个为True,最终结果就为True,
    两个条件都为False的情况下,最终结果才为False

    二:优先级not>and>or
    ps:
    如果单独就只是一串and链接,或者说单独就只是一串or链接,按照从左到右的顺讯依次运算即可(偷懒原则)
    如果是混用,则需要考虑优先级了

    res=3>4 and not 4>3 or 1==3 and 'x' == 'x' or 3 >3
    print(res)

    # False False False
    res=(3>4 and (not 4>3)) or (1==3 and 'x' == 'x') or 3 >3
    print(res)

    res=3>4 and ((not 4>3) or 1==3) and ('x' == 'x' or 3 >3)
    print(res)
    '''
    '''
    1、成员运算符
    print("egon" in "hello egon") # 判断一个字符串是否存在于一个大字符串中
    print("e" in "hello egon") # 判断一个字符串是否存在于一个大字符串中

    print(111 in [111,222,33]) # 判断元素是否存在于列表

    判断key是否存在于字典
    print(111 in {"k1":111,'k2':222})
    print("k1" in {"k1":111,'k2':222})

    not in
    print("egon" not in "hello egon") # 推荐使用
    print(not "egon" in "hello egon") # 逻辑同上,但语义不明确,不推荐

    2、身份运算符
    is # 判断的是id是否相等
    '''
  • 相关阅读:
    VIJOS1476 旅行规划(树形Dp + DFS暴力乱搞)
    神奇的图片
    How to locate elements/ Object locators for Android devices
    ZT: How to install appium with node.js
    How to get the appPackage and appActivity
    How to enable auto-complete for python in powershell
    Node.js
    Steps to develop an iterative algorithm
    Iterative Algorithm
    FSM
  • 原文地址:https://www.cnblogs.com/0B0S/p/12426497.html
Copyright © 2011-2022 走看看