举例讲解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读取Kafka实例

python读取Kafka实例

1. 新建.py文件 # pip install kafka-python from kafka import KafkaConsumer import setting conf...

解决python replace函数替换无效问题

python replace函数替换无效问题 str = "hello,china!" str.replace("hell","well") print(str) hello,C...

启动targetcli时遇到错误解决办法

 启动targetcli时遭遇ImportError: cannot import name ALUATargetPortGroup故障 targetcli是一个iSCSI配置...

pygame游戏之旅 如何制作游戏障碍

pygame游戏之旅 如何制作游戏障碍

本文为大家分享了pygame游戏之旅的第6篇,供大家参考,具体内容如下 定义一个障碍模型函数: def things(thingx, thingy, thingw, thingh,...

Python基础学习之类与实例基本用法与注意事项详解

本文实例讲述了Python基础学习之类与实例基本用法与注意事项。分享给大家供大家参考,具体如下: 前言 和其他编程语言相比,Python用非常少的新语法和语义将类加入到语言中。Pytho...