Python成员和标识操作符详细指南

成员运算符
成员标识运算符是用于验证值的成员标识的运算符。它测试序列中的成员标识, 例如字符串, 列表或元组。
在in运算符中:” in” 运算符用于检查一个值是否存在于序列中。如果找到指定序列中的变量, 则评估为true, 否则为false。

# 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" )

输出如下:
not overlapping

没有使用in运算符的相同示例:
# Python program to illustrate # Finding common member in list # withoutusing '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。
# 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 ispresent in given list" )if ( y in list ): print ( "y is present in given list" ) else : print ( "y is NOT present in given list" )

【Python成员和标识操作符详细指南】标识运算符
在Python中, 用于确定值是特定类还是类型。它们通常用于确定某个变量包含的数据类型。
有不同的标识运算符, 例如
“ is” 运算符–
如果运算符两侧的变量指向同一对象, 则计算为true, 否则为false。
# 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。
# Python program to illustrate the # use of 'is not' identity operator x = 5.2 if ( type (x) is not int ): print ( "true" ) else : print ( "false" )

输出如下:
true

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

    推荐阅读