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

相关文章

python实现txt文件格式转换为arff格式

本文实例为大家分享了python实现txt文件格式转换为arff格式的具体代码,供大家参考,具体内容如下 将文件读取出来的时候默认都是字符型的,所以有转换出来有点问题,但是还是可以用的。...

将string类型的数据类型转换为spark rdd时报错的解决方法

在将string类型的数据类型转换为spark rdd时,一直报这个错,StructType can not accept object %r in type %s” % (obj, t...

Python科学计算之Pandas详解

Python科学计算之Pandas详解

起步 Pandas最初被作为金融数据分析工具而开发出来,因此 pandas 为时间序列分析提供了很好的支持。 Pandas 的名称来自于面板数据(panel data)和python数...

Python Series从0开始索引的方法

如下所示: b.reset_index(drop=True) reset_index代表重新设置索引,drop=True为删除原索引。 以上这篇Python Series从0开始索...

Django后端发送小程序微信模板消息示例(服务通知)

Django后端发送小程序微信模板消息示例(服务通知)

模板消息 官方文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-mess...