举例讲解Python中的身份运算符的使用方法

yipeiwu_com5年前Python基础

Python身份运算符
身份运算符用于比较两个对象的存储单元

以下实例演示了Python所有身份运算符的操作:

#!/usr/bin/python

a = 20
b = 20

if ( a is b ):
  print "Line 1 - a and b have same identity"
else:
  print "Line 1 - a and b do not have same identity"

if ( id(a) == id(b) ):
  print "Line 2 - a and b have same identity"
else:
  print "Line 2 - a and b do not have same identity"

b = 30
if ( a is b ):
  print "Line 3 - a and b have same identity"
else:
  print "Line 3 - a and b do not have same identity"

if ( a is not b ):
  print "Line 4 - a and b do not have same identity"
else:
  print "Line 4 - a and b have same identity"

以上实例输出结果:

Line 1 - a and b have same identity
Line 2 - a and b have same identity
Line 3 - a and b do not have same identity
Line 4 - a and b do not have same identity 

相关文章

Python 中urls.py:URL dispatcher(路由配置文件)详解

Python 中urls.py:URL dispatcher(路由配置文件)详解

urls.py:URL dispatcher(路由配置文件) URL配置(URLconf)就像是Django所支撑网站的目录。它的本质是URL模式以及要为该URL模式调用的视图函数之间的...

Python中的 enum 模块源码详析

起步 上一篇 《Python 的枚举类型》 文末说有机会的话可以看看它的源码。那就来读一读,看看枚举的几个重要的特性是如何实现的。 要想阅读这部分,需要对元类编程有所了解。 成员名不允...

分享一个简单的python读写文件脚本

先来看一段创建文件并写入文本的代码,然后作介绍。 #!/usr/bin/env python 'makeFile.py -- create a file'...

python实现键盘输入的实操方法

python实现键盘输入的实操方法

python中有指定的代码进行输入操作,所以今天就由小编来为大家介绍python怎么实现键盘输入。 第一首先打开电脑的python编辑工具。 再创建python项目。 第二然后应用sy...

对python中数据集划分函数StratifiedShuffleSplit的使用详解

对python中数据集划分函数StratifiedShuffleSplit的使用详解

文章开始先讲下交叉验证,这个概念同样适用于这个划分函数 1.交叉验证(Cross-validation) 交叉验证是指在给定的建模样本中,拿出其中的大部分样本进行模型训练,生成模型,留小...