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

yipeiwu_com5年前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 获取 Linux 系统信息的代码

哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相同的代码在CPython 3 (3.3)上是不工作的,以及提供一份...

python概率计算器实例分析

本文实例讲述了python概率计算器实现方法。分享给大家供大家参考。具体实现方法如下: from random import randrange #randrange form ra...

python实现对图片进行旋转,放缩,裁剪的功能

先说明下,我这是对某个目录下的图片名称进行操作,该目录下的图片名称为1.jpg,2.jpg。。。。。这样类似的图片名。 1.旋转 # -*-coding:utf-8-*- from...

python中的五种异常处理机制介绍

从几年前开始学习编程直到现在,一直对程序中的异常处理怀有恐惧和排斥心理。之所以这样,是因为不了解。这次攻python,首先把自己最畏惧和最不熟悉的几块内容列出来,里面就有「异常处理」这一...

python 使用正则表达式按照多个空格分割字符的实例

python 使用正则表达式按照多个空格分割字符的实例

程序代码如下 import os import re os.system("nmap -sP 192.168.3.0/24") //扫描IP mac = os.popen("cat...