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)
Operator | Description | Example |
---|---|---|
in | It returns True, if a variable/value found in the sequence. | 10 in list1 |
not in | It 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中的成员运算符