讲解Python中的标识运算符

yipeiwu_com5年前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聊天程序实例代码分享

代码简单,直接看代码吧:复制代码 代码如下:import socketimport threadingimport re#import Tkinter def ser(): &...

python读取excel指定列数据并写入到新的excel方法

如下所示: #encoding=utf-8 import xlrd from xlwt import * #------------------读数据----------------...

Python利用openpyxl库遍历Sheet的实例

方法一,利用 sheet.iter_rows() 获取 Sheet1 表中的所有行,然后遍历 import openpyxl wb = openpyxl.load_workbook...

Python中使用item()方法遍历字典的例子

Python中使用item()方法遍历字典的例子

Python字典的遍历方法有好几种,其中一种是for...in,这个我就不说明,在Python了几乎随处都可见for...in。下面说的这种遍历方式是item()方法。 item() i...

python集合比较(交集,并集,差集)方法详解

python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), differe...