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

相关文章

Django 简单实现分页与搜索功能的示例代码

Django 简单实现分页与搜索功能的示例代码

假设现有需求如下: 需要一个页面分页展示信息,在该页面添加搜索框以提供检索功能。 那么,我们知道,展示信息和检索功能是在同一个页面,也就是共用一个路由。 代码如下: 第一步,写路由:为了...

python贪婪匹配以及多行匹配的实例讲解

1 非贪婪flag >>> re.findall(r"a(\d+?)", "a23b") ['2'] >>> re.findall(r...

python 中文件输入输出及os模块对文件系统的操作方法

整理了一下python 中文件的输入输出及主要介绍一些os模块中对文件系统的操作。 文件输入输出 1、内建函数open(file_name,文件打开模式,通用换行符支持),打开文件返回文...

解决python 输出是省略号的问题

这个问题非常非常重要,搞了一晚上都没解决好,但是真的很简单很简单, 如果你也 是用的numpy array, 如果你也想得到输出矩阵的全部内容,而不是省略形式, [[ 0.10284...

selenium+python自动化测试之环境搭建

selenium+python自动化测试之环境搭建

最近由于公司有一个向谷歌网站上传文件的需求,需要进行web的自动化测试,选择了selenium这个自动化测试框架,以前没有接触过这门技术,所以研究了一下,使用python来实现自动化脚本...