Python的成员操作符在一个序列测试成员,如字符串,列表或元组。有两个成员操作符如下面所解释:
运算符 | 描述 |
例子
|
---|---|---|
in |
如果在指定的顺序找到则计算结果为true,否则变量结果值为false
|
x in y,这里在一个1的结果,如果 x 是序列 y 的成员
|
not in |
如果不能在指定的顺序找到则计算结果为true,否则变量为 false
|
x ont in y,这里在一个1的结果,如果 x 不是序列 y 成员 |
示例
#!/usr/bin/python3 a = 10 b = 20 list = [1, 2, 3, 4, 5 ] if ( a in list ): print ("Line 1 - a is available in the given list") else: print ("Line 1 - a is not available in the given list") if ( b not in list ): print ("Line 2 - b is not available in the given list") else: print ("Line 2 - b is available in the given list") c=b/a if ( c in list ): print ("Line 3 - a is available in the given list") else: print ("Line 3 - a is not available in the given list")
当你执行上面的程序它产生以下结果:
Line 1 - a is not available in the given list Line 2 - b is not available in the given list Line 3 - a is available in the given list