在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框架Jinjia模板常用语法总结

Flask框架Jinjia模板常用语法总结

本文实例总结了Flask框架Jinjia模板常用语法。分享给大家供大家参考,具体如下: 1. 变量表示 {{ argv }} 2. 赋值操作 {% set links =...

使用Python做定时任务及时了解互联网动态

使用Python做定时任务及时了解互联网动态

前言 本人因为比较喜欢看漫画和动漫, 所以总会遇到一些问题, 因为订阅的漫画或者动漫太多, 总会忘记自己看到那一章节或者不知道什么时候更新. 故会有这么一个需求, 想记录自己想看的漫画或...

numpy使用技巧之数组过滤实例代码

本文研究的主要是numpy使用技巧之数组过滤的相关内容,具体如下。 当使用布尔数组b作为下标存取数组x中的元素时,将收集数组x中所有在数组b中对应下标为True的元素。使用布尔数组作为下...

django+tornado实现实时查看远程日志的方法

大致思路: 1.利用tornado提供的websocket功能与浏览器建立长连接,读取实时日志并输出到浏览器 2.写一个实时读取日志的脚本,利用saltstack远程执行,并把实时日志发...

Python 如何提高元组的可读性

这篇文章主要介绍了Python 如何提高元组的可读性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 假设学生系统中数据为固定格式:(名...