举例讲解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集合用法实例分析

本文实例讲述了python集合用法。分享给大家供大家参考。具体分析如下: # sets are unordered collections of unique hashable el...

Ubuntu下安装PyV8

这几天需要在使用PyV8来进行python与javascript的交互。之前在window下安装过,直接使用的exe安装的,也没有遇到什么问题。 结果这次在Ubuntu安装遇到了不少坑-...

Python3实现将一维数组按标准长度分隔为二维数组

如下所示: def trans_data_to_pair(self,data,index): contents=[ data[i:i+index] f...

django框架创建应用操作示例

本文实例讲述了django框架创建应用操作。分享给大家供大家参考,具体如下: 18.1.5  安装Django 安装Django node2:/root#pip insta...

pandas-resample按时间聚合实例

pandas-resample按时间聚合实例

如下所示: import pandas as pd #如果需要的话,需将df中的date列转为datetime df.date = pd.to_datetime(df.date,...