在Python中使用成员运算符的示例

yipeiwu_com6年前Python基础

下表列出了所有Python语言支持的成员运算符。

2015513122253479.jpg (586×176)

 例如:

试试下面的例子就明白了所有的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

相关文章

Python判断对象是否相等及eq函数的讲解

摘要 什么是python对象的标识 python对象相等的判断 自定义python对象相等的条件 python对象的标识 python对象标识就是python对象自身的要...

用Python画小女孩放风筝的示例

用Python画小女孩放风筝的示例

我就废话不多说了,直接上代码吧! # coding:utf-8 2import turtle as t 3import random 4# 画心 5def xin(): 6...

python实现微信防撤回神器

本文实例为大家分享了python实现微信防撤回神器的具体代码,供大家参考,具体内容如下 手写辛苦,希望给赞 #!/usr/local/bin/python3 # coding=utf...

详谈pandas中agg函数和apply函数的区别

在利用python进行数据分析 这本书中其实没有明确表明这两个函数的却别,而是说apply更一般化. 其实在这本书的第九章‘数组及运算和转换'点到了两者的一点点区别:agg是用来聚合运算...

Python实现LRU算法的2种方法

LRU:least recently used,最近最少使用算法。它的使用场景是:在有限的空间中存储对象时,当空间满时,会按一定的原则删除原有的对象,常用的原则(算法)有LRU,FIFO...