讲解Python中的标识运算符

yipeiwu_com6年前Python基础

 下表列出了所有Python语言支持的标识运算符。

2015514101018679.jpg (589×219)

示例:

试试下面的例子就明白了所有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中二维阵列的变换方法。分享给大家供大家参考。具体方法如下: 先看如下代码: arr = [ [1, 2, 3], [4, 5, 6], [7, 8,9],...

python 实现将字典dict、列表list中的中文正常显示方法

在代码文件中定义中文时,经常会遇到问题,要么编码错误,要么是无法正常打印显示。 例如,dict_chinese.py: #!/usr/bin/python a={'name': 'f...

Django使用 Bootstrap 样式修改书籍列表过程解析

Django使用 Bootstrap 样式修改书籍列表过程解析

展示书籍列表: 首先修改原先的 book_list.html 的代码: <!DOCTYPE html> <!-- saved from url=(0042)htt...

Python利用ORM控制MongoDB(MongoEngine)的步骤全纪录

简介: MongoEngine 是一个Document-Object Mapper (想一下ORM, 但它是针对文档型数据库),Python通过它与MongoDB交互。你可能会说那PyM...

Pycharm2017版本设置启动时默认自动打开项目的方法

Pycharm2017版本设置启动时默认自动打开项目的方法

最新版本不同于旧版本的设置,网上检索一番方法都不适用。 新版的设置位置在“configure->setting->Appearance&Behavior->System...