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

yipeiwu_com6年前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 

相关文章

pip命令无法使用的解决方法

今天在学习Python时需要安装Requests    使用命令:pip install requests    &...

python列表推导和生成器表达式知识点总结

首先来看一下代码: chars = "abcd" tmp = [] for char in chars: tmp.append(ord(char)) print(tmp) 这是一...

python2与python3的print及字符串格式化小结

python2与python3的print及字符串格式化小结

最近一直在用python写程序,对于python的print一直很恼火,老是不按照预期输出。在python2中print是一种输出语句,和if语句,while语句一样的东西,在pytho...

Python实现把json格式转换成文本或sql文件

python如何将json格式的数据快速的转化成指定格式的数据呢?或者转换成sql文件? 下面的例子是将json格式的数据准换成以#_#分割的文本数据,也可用于生成sql文件。 [r...

python画双y轴图像的示例代码

很多时候可能需要在一个图中画出多条函数图像,但是可能y轴的物理含义不一样,或是数值范围相差较大,此时就需要双y轴。 matplotlib和seaborn都可以画双y轴图像。 一个例子:...