美文网首页python
03 逻辑运算符

03 逻辑运算符

作者: 菩提树下参悟 | 来源:发表于2023-04-12 08:29 被阅读0次

    Python逻辑运算符(得到True(真)/False(假)):and、or、not
    and:通过and连接的两个条件,必须同时成立,整体成立。例如:print(1>2 and 4>2) F
    Or:通过or连接的两个条件,有一个条件成立,整体都成立。例如:print(1>2 or 4>2) T
    Not:被not修饰的条件,如果原条件成立,反向整体不成立,(对立面)。例如:print(not 4<2) T

    ingeter1 = 123
    ingeter2 = 456
    string1 = 'abc'
    string2 = 'defg'
    # print(ingeter1 < ingeter2)
    # print(ingeter1 > ingeter2)
    # print(string1 < string2)
    # print(string1 > string2)
    ubool  = ingeter1 < ingeter2 and string1 < string2
    print(ubool)
    ubool = ingeter1 < ingeter2 and string1 > string2
    print(ubool)
    ubool  = ingeter1 < ingeter2 or string1 < string2
    print(ubool)
    ubool = ingeter1 < ingeter2 or string1 > string2
    print(ubool)
    ubool = not string1 < string2
    print(ubool)
    ubool = not string1 > string2
    print(ubool)
    
    输出:
    True
    False
    True
    True
    False
    True
    

    字符串是可以比较大小的

    成员判断(得到True(真)/False(假)):in,not in
    判断一个成员是否在某个集合内,
    IN:print(‘a’ in ‘abc’) T / print(‘3’ in ‘abc’) F
    Not:print(‘3’ not in ‘abc’) T

    ingeter1 = 123
    ingeter2 = 456
    string1 = 'abc'
    string2 = 'abd'
    string3 = 'abc'
    print('a' in string1)
    print('3 'in string1) # 不能print(3 in string1),不同类型不能比较
    print('3' not in string1)
    print(string1 < string2)
    print(string1 > string2)
    print(string1 == string3)
    
    
    输出:
    True
    False
    True
    True
    False
    True
    

    相关文章

      网友评论

        本文标题:03 逻辑运算符

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