zoukankan      html  css  js  c++  java
  • 3. 成员运算符和身份运算符

    一:成员运算符

    1. in

    • 判断一个字符或者字符串是否存在于一个大字符串中
    print("eogn" in "hello eogn")
    print("e" in "hello eogn")
    
    True
    True
    
    • 判断元素是否存在于列表
    print(1 in [1,2,3])
    print('x' in ['x','y','z'])
    
    True
    True
    
    • 判断key是否存在于字典
    print('k1' in {'k1':111,'k2':222})
    print(111 in {'k1':111,'k2':222})
    
    True
    False
    

    2. not in

    • 判断一个字符或者字符串是否存在于一个大字符串中
    print("eogn" not in "hello eogn")
    print("e" not in "hello eogn")
    
    False
    False
    
    • 判断元素是否存在于列表
    print(1 not in [1,2,3])
    print('x' not in ['x','y','z'])
    
    False
    False
    
    • 判断key是否存在于字典
    print('k1' not in {'k1':111,'k2':222})
    print(111 not in {'k1':111,'k2':222})
    
    False
    True
    

    二:身份运算符

    1. is

    • 判断的是id是否相等

    • 如果引用的是同一个对象则返回 True,否则返回 False

    x = 1
    y = 1
    print(x is y)
    
    True
    x = 1
    y = 2
    print(x is y)
    
    False
    

    2. not is

    • 是判断两个标识符是不是引用自不同对象

    • 如果引用的不是同一个对象则返回结果 True,否则返回 False

    x = 1
    y = 1
    print(x is not y)
    
    False
    x = 1
    y = 2
    print(x is not y)
    
    True
    

    三:短路运算

    偷懒原则,偷懒到哪个位置,就把当前位置的值返回

    10>3 and 10 and None and 10 //返回None

    10>3 and 10 and None and 10 or 10 > 3 and 1 == 1 //返回True

  • 相关阅读:
    iframe
    daterangepicker 时间区间选择
    刷新父窗口
    echars
    原生http请求封装
    css布局方式总结
    js获取http请求响应头信息
    js事件循环机制 (Event Loop)
    http协议的状态码
    javaScript函数节流与函数防抖
  • 原文地址:https://www.cnblogs.com/j-chao/p/13031607.html
Copyright © 2011-2022 走看看