讲解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部署web开发程序的几种方法

1、fastcgi ,通过flup模块来支持,在nginx里对应的配置指令是 fastcgi_pass 2、http,nginx使用proxy_pass转发,这个要求后端appplica...

用Python实现随机森林算法的示例

拥有高方差使得决策树(secision tress)在处理特定训练数据集时其结果显得相对脆弱。bagging(bootstrap aggregating 的缩写)算法从训练数据的样本中建...

Python小游戏之300行代码实现俄罗斯方块

Python小游戏之300行代码实现俄罗斯方块

前言 本文代码基于 python3.6 和 pygame1.9.4。 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一个俄罗斯方块。但是想到旋转,停靠,消除等操...

python3人脸识别的两种方法

python3人脸识别的两种方法

本文实例为大家分享了python3实现人脸识别的具体代码,供大家参考,具体内容如下 第一种: import cv2 import numpy as np filename = 't...

Python中低维数组填充高维数组的实现

Python中低维数组填充高维数组的实现

今天遇到这样一种业务情况: 我的图片的画布是(4,4,3)的三维数组,而得到的图片是(2,2,3)的三维数组,我要把图片放到画布的中间某个位置应该怎么做呢? 大家首先想到是遍历循环,但是...