Python教程之成员和身份运算符的用法详解
作者:海拥 时间:2021-04-19 11:36:56
成员运算符
Python 提供了两个成员运算符来检查或验证值的成员资格。它测试序列中的成员资格,例如字符串、列表或元组。
in 运算符
'in' 运算符用于检查序列中是否存在字符/子字符串/元素。如果在序列中找到指定元素,则评估为 True,否则为 False。例如,
'G' in 'GeeksforGeeks' # 检查字符串中的“G”
True
'g' in 'GeeksforGeeks' # 检查字符串中的“g”,因为 Python 区分大小写,返回 False
False
'Geeks' in ['Geeks', 'For','Geeks'] # 检查字符串列表中的“Geeks”
True
10 in [10000,1000,100,10] # 检查整数列表中的 10
True
dict1={1:'Geeks',2:'For',3:'Geeks'} # 检查字典键中的 3
3 in dict1
True
# Python 程序说明使用“in”运算符在列表中查找常见成员
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9]
for item in list1:
if item in list2:
print("overlapping")
else:
print("not overlapping")
输出
not overlapping
not overlapping
not overlapping
not overlapping
not overlapping
没有使用 in 运算符的相同示例:
# 说明在不使用“in”运算符的情况下在列表中查找常见成员的 Python 程序
# 定义一个接受两个列表的函数()
def overlapping(list1, list2):
c = 0
d = 0
for i in list1:
c += 1
for i in list2:
d += 1
for i in range(0, c):
for j in range(0, d):
if(list1[i] == list2[j]):
return 1
return 0
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9]
if(overlapping(list1, list2)):
print("overlapping")
else:
print("not overlapping")
输出
not overlapping
'not in' 运算符
如果在指定序列中没有找到变量,则评估为 true,否则评估为 false。
# Python 程序来说明 not 'in' 运算符
x = 24
y = 20
list = [10, 20, 30, 40, 50]
if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if (y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
复制代码
输出:
x is NOT present in given list
y is present in given list
身份运算符
如果两个对象实际上具有相同的数据类型并共享相同的内存位置,则使用标识运算符来比较对象。
有不同的身份运算符,例如
'is' 运算符
如果运算符两侧的变量指向同一对象,则计算结果为 True,否则计算结果为 false。
# Python程序说明'is'恒等运算符的使用
x = 5
y = 5
print(x is y)
id(x)
id(y)
输出:
True
140704586672032
140704586672032
在给定的示例中,变量 x 和 y 都分配了值 5,并且都共享相同的内存位置,这就是返回 True 的原因。
'is not' 运算符
如果运算符两侧的变量指向不同的对象,则计算结果为 false,否则计算结果为 true。
# Python程序说明'is not'恒等运算符的使用
x = 5
if (type(x) is not int):
print("true")
else:
print("false")
# Prints True
x = 5.6
if (type(x) is not int):
print("true")
else:
print("false")
输出:
False
True
来源:https://juejin.cn/post/7145486202659930126
标签:Python,成员,身份,运算符
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
悟道WEB标准:统一思想,遵循标准
2009-10-11 16:38:00
Asp实现伪静态的方法
2007-09-29 21:27:00
Python数组变形的几种实现方法
2021-08-20 09:30:47
![](https://img.aspxhome.com/file/2023/7/85387_0s.png)
Python绘制堆叠柱状图的实例
2022-01-04 06:14:55
![](https://img.aspxhome.com/file/2023/8/118598_0s.jpg)
ThinkPHP 3.2.3实现页面静态化功能的方法详解
2023-11-23 13:12:53
快速解决 MySQL中与浮点比较有关的问题
2008-11-27 16:28:00
解决python虚拟环境切换无效的问题
2023-02-01 14:37:50
![](https://img.aspxhome.com/file/2023/0/77860_0s.jpg)
使用Spry轻松将XML数据显示到HTML页
2007-11-16 16:44:00
![](https://img.aspxhome.com/file/UploadPic/200711/16/2007111616510974s.jpg)
JavaScript实现年历效果
2023-09-10 10:53:26
![](https://img.aspxhome.com/file/2023/0/56280_0s.jpg)
Python中星号的五种用法小结
2022-12-06 16:17:23
Python 3.x 新特性及10大变化
2023-02-05 09:17:36
用MySQL做站点时如何记录未知错误的发生
2010-09-30 14:11:00
四行Python3代码实现图片添加美颜效果
2021-01-25 10:29:30
![](https://img.aspxhome.com/file/2023/2/108062_0s.jpg)
Django中实现一个高性能计数器(Counter)实例
2023-11-16 03:53:48
从Python的源码来解析Python下的freeblock
2023-07-26 20:44:39
![](https://img.aspxhome.com/file/2023/7/61217_0s.png)
Python实现基本数据结构中栈的操作示例
2021-09-04 10:29:23
![](https://img.aspxhome.com/file/2023/4/121614_0s.png)
python判断字符串或者集合是否为空的实例
2021-08-03 04:08:58
解决python 3 urllib 没有 urlencode 属性的问题
2022-03-31 12:42:44
PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)实例详解
2023-09-10 08:37:27
![](https://img.aspxhome.com/file/2023/3/553831_0s.png)
python 多进程通信模块的简单实现
2021-06-13 08:21:32