python中的成员运算符_Python中的成员运算符

本文详细介绍了Python中的成员运算符,包括'in'和'not in'的使用方法,以及如何用它们来检查值是否存在于字符串、列表、元组、集合或字典等序列中。通过实例演示了运算符的返回值为True或False的具体情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python中的成员运算符

Membership Operators are the operators, which are used to check whether a value/variable exists in the sequence like string, list, tuples, sets, dictionary or not.

成员运算符是用于检查诸如字符串,列表,元组,集合,字典之类的序列中是否存在值/变量的运算符。

These operator returns either True or False, if a value/variable found in the list, its returns True otherwise it returns False.

这些运算符返回True或False ,如果在列表中找到一个值/变量,则返回True,否则返回False 。

Python会员运算符 (Python Membership Operators)

OperatorDescriptionExample
inIt returns True, if a variable/value found in the sequence.10 in list1
not inIt returns True, if a variable/value does not found in the sequence.10 not in list1
操作员 描述
如果在序列中找到变量/值,则返回True 。 清单1中的10
不在 如果在序列中未找到变量/值,则返回True 。 10不在清单1中

Example:

例:

# Python example of "in" and "not in" Operators

# declare a list and a string
str1 = "Hello world"
list1 = [10, 20, 30, 40, 50]

# Check 'w' (capital exists in the str1 or not 
if 'w' in str1:
	print "Yes! w found in ", str1
else:
	print "No! w does not  found in " , str1

# check 'X' (capital) exists in the str1 or not
if 'X' not in str1:
	print "yes! X does not exist in ", str1
else:
	print "No!  X exists in ", str1

# check 30 exists in the list1 or not 
if 30 in list1:
	print "Yes! 30 found in ", list1
else:
	print "No! 30 does not found in ", list1

# check 90 exists in the list1 or not
if 90 not in list1:
	print "Yes! 90 does not exist in ", list1
else:
	print "No! 90 exists in ", list1

Output

输出量

    Yes! w found in  Hello world
    yes! X does not exist in  Hello world
    Yes! 30 found in  [10, 20, 30, 40, 50]
    Yes! 90 does not exist in  [10, 20, 30, 40, 50]


翻译自: https://www.includehelp.com/python/membership-operators.aspx

python中的成员运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值