在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的Flask框架及Nginx实现静态文件访问限制功能

Nginx配置 Ngnix,一个高性能的web服务器,毫无疑问它是当下的宠儿。卓越的性能,灵活可扩展,在服务器领域里攻城拔寨,征战天下。 静态文件对于大多数website是不可或缺的一部...

将python代码和注释分离的方法

python的注释方式和C语言、C++、java有所不同 python语言中,使用‘#' 来进行注释,其次还有使用 三个引号来进行注释 本文的程序将把 python 中 使用‘#' 号...

Python并发编程协程(Coroutine)之Gevent详解

Python并发编程协程(Coroutine)之Gevent详解

Gevent官网文档地址:http://www.gevent.org/contents.html 基本概念 我们通常所说的协程Coroutine其实是corporateroutine的缩...

python 函数的缺省参数使用注意事项分析

本文实例讲述了python 函数的缺省参数使用注意事项。分享给大家供大家参考,具体如下: python的函数支持4种形式的参数:分别是必选参数、 缺省参数、 可变长参数、关键字参数;而且...

Python根据服务获取端口号的方法

根据服务获取端口号 首先需要下载一个psutil库 然后根据服务名找到PID 找到PID之后,通过pid获取端口号 # -*- encoding=utf8 -*- import ps...