📜  Python成员和身份运算符

📅  最后修改于: 2022-05-13 01:55:31.395000             🧑  作者: Mango

Python成员和身份运算符

在本文中,我们将介绍Python成员资格和身份运算符。

会员运营商

成员资格运算符运算符用于验证值的成员资格的运算符。它测试序列中的成员资格,例如字符串、列表或元组。

  • in运算符: 'in'运算符用于检查序列中是否存在值。如果它在指定的序列中找到一个变量,则评估为 true,否则为 false。
Python3
# Python program to illustrate
# Finding common member in list
# using 'in' operator
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")


Python3
# Python program to illustrate
# Finding common member in list
# without  using 'in' operator
 
#  Define a function() that takes two lists
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")


Python3
# Python program to illustrate
# not 'in' operator
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")


Python3
# Python program to illustrate the use
# of 'is' identity operator
x = 5
if (type(x) is int):
    print("true")
else:
    print("false")


Python3
# Python program to illustrate the
# use of 'is not' identity operator
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")


输出:

not overlapping

没有使用 in运算符的相同示例:

Python3

# Python program to illustrate
# Finding common member in list
# without  using 'in' operator
 
#  Define a function() that takes two lists
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。

Python3

# Python program to illustrate
# not 'in' operator
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

身份运算符

在Python中,身份运算符用于确定一个值是否属于某个类或类型。它们通常用于确定某个变量包含的数据类型。
有不同的身份运算符,例如

  • 'is'运算符-如果运算符两侧的变量指向同一对象,则计算结果为 true,否则计算结果为 false。

Python3

# Python program to illustrate the use
# of 'is' identity operator
x = 5
if (type(x) is int):
    print("true")
else:
    print("false")

输出:

true
  • 'is not'运算符–如果运算符两侧的变量指向不同的对象,则计算结果为 false,否则计算结果为 true。

Python3

# Python program to illustrate the
# use of 'is not' identity operator
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