zoukankan      html  css  js  c++  java
  • Python————逻辑运算符

    了解:
    一、优先级:not > and > or
    1、not与紧跟其后的那个条件是不可分割的
    2、如果条件语句全部由纯and、或纯or链接,按照从左到右的顺序依次计算即可

    print(True and 10 > 3 and not 4 < 3 and 1 == 1)
    print(False or 10 < 3 or not 4 < 3 or 1 == 1)

    3、对于既有and又有or链接的语句,以and为中心把左右两个条件用
    括号括起来

    res=(10 == 9 and 0 < 3) or ('' == 'egon' and 0> 3) or not True or ('egon' == 'dsb' and 333 > 100) or 10 > 4
    print(res)

    2、短路运算=>偷懒原则
    所有的数据类型的值都自带布尔值,所以值可以直接被当成条件使用
    0、None、空三种值对应的布尔值为False,其余全为True

    if 0:
    print('ok')
    else:
    print('=====>')


    if 3 and []:
    print('真')
    else:
    print('假')


    and运算会返回当前计算位置的值
    res=0 and 123
    res=111 and 123
    print(res)


    if 111 and 123:
    print('ok')
    else:
    print('no')

    x=''
    if x:
    print('不为空')
    else:
    print("为空")


    print(1 or 0) # 1
    print(0 and 2 or 1) # 1
    print(0 and 2 or 1 or 4) # 1

  • 相关阅读:
    Thinking in Java
    Interview Common Sample Codes
    Longest Common Substring
    Mac键盘按键符号
    ElasticSearch
    Variables and Arithmetic Expression
    Associative Containers
    Container Adaptors
    string Type
    初识 tk.mybatis.mapper 通用mapper
  • 原文地址:https://www.cnblogs.com/x945669/p/12360372.html
Copyright © 2011-2022 走看看