在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中的Function定义方法第1/2页

下面就先定义一个函数: 复制代码 代码如下:def foo(): print('function') foo() 在上述代码中,定义了一个名为foo的函数,这个函数没有参数。最后一行代码...

Python简单计算文件MD5值的方法示例

本文实例讲述了Python简单计算文件MD5值的方法。分享给大家供大家参考,具体如下: 一 代码 import sys import hashlib import os.path f...

Python中decorator使用实例

在我以前介绍 Python 2.4 特性的Blog中已经介绍过了decorator了,不过,那时是照猫画虎,现在再仔细描述一下它的使用。 关于decorator的详细介绍在 Python...

Django框架组成结构、基本概念与文件功能分析

本文实例讲述了Django框架组成结构、基本概念与文件功能。分享给大家供大家参考,具体如下: django遵循MVC架构: 管理工具(management):一套内置的创建站点、迁移数据...

浅谈Matplotlib简介和pyplot的简单使用——文本标注和箭头

浅谈Matplotlib简介和pyplot的简单使用——文本标注和箭头

 在使用pyplot画图的时候,有时会需要在图上标注一些文字,如果曲线靠的比较近,最好还能用箭头指出标注文字和曲线的对应关系。这里就介绍文字标注和箭头的使用。 添加标注使用py...