首页 > 其他 > 详细

3. 成员运算符和身份运算符

时间:2020-06-02 17:45:19      阅读:48      评论:0      收藏:0      [点我收藏+]

一:成员运算符

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

3. 成员运算符和身份运算符

原文:https://www.cnblogs.com/j-chao/p/13031607.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!