在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栈的基本定义与使用方法。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #! python3 #在桟的设计中,我们需要定义一...

Django模型序列化返回自然主键值示例代码

场景 在设计表结构时,难免需要建立一些外键关联。例如这样两个模型: from django.db import models class Person(models.Model...

轻松实现TensorFlow微信跳一跳的AI

轻松实现TensorFlow微信跳一跳的AI

作为python和机器学习的初学者,目睹了AI玩游戏的各种风骚操作,心里也是跃跃欲试。 然后发现微信跳一跳很符合需求,因为它不需要处理连续画面(截屏太慢了)和复杂的操作,很适合拿来练手。...

python删除文件示例分享

删除文件复制代码 代码如下:os.remove(   filename )   # filename: "要删除的文件名" 产生异常的可能原因:...

Python中浅拷贝copy与深拷贝deepcopy的简单理解

以下是个人对Python深浅拷贝的通俗解释,易于绕开复杂的Python数据结构存储来进行理解! 高级语言中变量是对内存及其地址的抽象,Python的一切变量都是对象。 变量的存...