在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

相关文章

flask利用flask-wtf验证上传的文件的方法

利用flask-wtf验证上传的文件 定义验证表单类的时候,对文件类型的字段,需要采用FileField这个类型,即wtforms.FileField。 验证器需要从flask...

python和pyqt实现360的CLable控件

 复制代码 代码如下: #!/usr/bin/python  #-*-coding:utf-8-*- from PyQt4.QtGui import *fr...

python3 实现的对象与json相互转换操作示例

本文实例讲述了python3 实现的对象与json相互转换操作。分享给大家供大家参考,具体如下: 1. python主要有三种数据类型:字典、列表、元组,其分别由花括号,中括号,小括号表...

Python多叉树的构造及取出节点数据(treelib)的方法

项目: 基于Pymysql的专家随机抽取系统 引入库函数: >>> import treelib >>> from treelib import...

python 日期排序的实例代码

问题1:如果日期中有千年以前的情况(没法用格式化函数),如('2010-11-23','1989-3-7','2010-1-5','978-12-1','2010-2-4')参照方法1...