讲解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使用新浪微博API发送微博的例子

Python使用新浪微博API发送微博的例子

1、注册一个新浪应用,得到appkey和secret,以及token,将这些信息写入配置文件sina_weibo_config.ini,内容如下,仅举例: 复制代码 代码如下:[user...

python实现回旋矩阵方式(旋转矩阵)

我们知道Python中是没有数组 这种数据结构的,所以要想实现回旋矩阵,需要先导入一个numpy包, 它是一个由多维数组对象和用于处理数组的例程集合组成的python扩充程序库,可以用来...

Python箱型图处理离群点的例子

Python箱型图处理离群点的例子

首先我们简单地区分一下离群点(outlier)以及异常值(anomaly): 离群点: 异常值: 个人觉着异常值和离群点是两个不同的概念,当然大家在数据预处理时对于这两个概念不做细致...

基于django ManyToMany 使用的注意事项详解

使用场景一: 如果在一张表中ManayTOManay字段关联的是自身,也就是出项这样的代码: ManyToManyField(self) 那么,你需要注意一点,当你采用add方法将一个自...

简单实现python数独游戏

网上看到一个python写的数独,很好玩,分享给大家。 import random import itertools from copy import deepcopy def m...