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

相关文章

django celery redis使用具体实践

django celery redis使用具体实践

环境准备 python3.5.4 windows redis pip install celery pip install redis windows下启动redir...

分享给Python新手们的几道简单练习题

前言 本文主要给大家分享了一些简单的Python练习题,对学习python的新手们来说是个不错的练习问题,下面话不多说了,来一起看看详细的介绍吧。 第一题:使用while循环输入 1 2...

Django 导出项目依赖库到 requirements.txt过程解析

虚拟环境: 使用 pip freeze pip freeze > requirements.txt # 这种方式推荐配合 virtualenv ,否则会把整个环境中的包都列...

pip install python 快速安装模块的教程图解

pip install python 快速安装模块的教程图解

之前python安装模块要在网络上下载,从python2.7.9之后,以及python3,python就自带pip 这个命令,能够快速的安装模块 1, 首先打开python的主文件夹...

python实现高斯投影正反算方式

使用Python实现了一下我们同事的C++高斯投影正反算,实际跑通,可用。 #!/ usr/bin/python # -*- coding:utf-8 -*- import mat...