Python成員運算符示例
下表列出了所有Python語言支持的成員運算符。
運算符 | 描述 | 示例 |
---|---|---|
in | 計算結果為true,如果它在指定變量找到,否則false。 | x在y中,在這裡產生一個1,如果x是序列y的成員。 |
not in | 計算結果為true,如果它不找到在指定的變量順序,否則為false。 | x不y中,這裡不產生一個1,如果x是序列y的成員。 |
例如:
試試下麵的例子就明白了所有的Python編程語言提供會員運算符:
#!/usr/bin/python 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" a = 2 if ( a 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