在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字符串拼接的几种方法整理

Python字符串拼接的几种方法整理

Python字符串拼接的几种方法整理 第一种 通过加号(+)的形式 print('第一种方式通过加号形式连接 :' + 'love'+'Python' + '\n') 第二种 通...

python实现K近邻回归,采用等权重和不等权重的方法

如下所示: from sklearn.datasets import load_boston boston = load_boston() from sklearn.cros...

python动态监控日志内容的示例

日志文件一般是按天产生,则通过在程序中判断文件的产生日期与当前时间,更换监控的日志文件程序只是简单的示例一下,监控test1.log 10秒,转向监控test2.log 程序监控使用是l...

使用python编写udp协议的ping程序方法

服务器端 import random from socket import * serverSocket = socket(AF_INET, SOCK_DGRAM)#建立udp协...

python版本单链表实现代码

今天看了一下数据结构的书,发现其实数据结构没有几种,线性表,数组,字符串,队列和栈,等等,其实是一回事,然后就是树结构,图结构。数据结构的理论并不难,主要是要自己写一下这些数据结构以及对...