举例讲解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 

相关文章

matplotlib作图添加表格实例代码

matplotlib作图添加表格实例代码

本文所示代码主要是通过Python+matplotlib实现作图,并且在图中添加表格的功能,具体如下。 代码 import matplotlib.pyplot as plt impo...

pygame实现俄罗斯方块游戏(AI篇1)

pygame实现俄罗斯方块游戏(AI篇1)

上次更新到pygame实现俄罗斯方块游戏(基础篇3) 现在继续 一、定义玩家类 定义玩家类是为了便于进行手动和机器模式或各种不同机器人模式的混合使用,增加代码扩展性。 可以先定义一个玩家...

解决Pytorch 加载训练好的模型 遇到的error问题

解决Pytorch 加载训练好的模型 遇到的error问题

这是一个非常愚蠢的错误 debug的时候要好好看error信息 提醒自己切记好好对待error!切记!切记! -----------------------分割线------------...

Python考拉兹猜想输出序列代码实践

考拉兹猜想(英语:Collatz conjecture),是指对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1。(摘自Wiki)...

Anaconda下配置python+opencv+contribx的实例讲解

Anaconda下配置python+opencv+contribx的实例讲解

先吐槽一下opencv 3.1.0的版本cv2.sift和surf就不能用了 看解释是说 什么 "non-free",,必须要到opencv_contrib库中才有,而这个库的编译不是...