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

yipeiwu_com4年前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 pygame实现方向键控制小球

python pygame实现方向键控制小球

最后一个项目用到了pygame,  实现方向键控制小球,对于模块不熟悉的我还是查询了一些资料介绍。 import sys import pygame from pygame...

python同时遍历数组的索引和值的实例

你想在迭代一个序列的同时跟踪正在被处理的元素索引。 获取索引 内置的 enumerate() 函数可以很好的解决这个问题: >>> my_list = ['a',...

python中for用来遍历range函数的方法

python中for用来遍历range函数的方法

栗子:计算斐波那契数列(任一个数都是前两个数之和的数字序列) Python2.7实现代码如下: <strong><span style="font-size:14p...

Python删除Java源文件中全部注释的实现方法

本文实例讲述了Python删除Java源文件中全部注释的实现方法。分享给大家供大家参考,具体如下: 同事想删除一个Java项目中的全部注释,让我帮忙想想办法。 没找不到合适工具,就写了这...

Python中基本的日期时间处理的学习教程

Python中基本的日期时间处理的学习教程

Python程序能用很多方式处理日期和时间。转换日期格式是一个常见的例行琐事。Python有一个 time 和 calendar 模组可以帮忙。 什么是Tick? 时间间隔是以秒为单位的...