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

Python的条件语句与运算符优先级详解
Python条件语句Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。可以通过下图来简单了解条件语句的执行过程:Python

全面解析Python的While循环语句的使用方法
Python编程中while语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:while判断条件:执行语句

解析Python中while true的使用
无限循环如果条件判断语句永远为true,循环将会无限的执行下去,如下实例:#!/usr/bin/python#-*-coding:UTF-8-*-var=1whilevar==1:#该条件永远为true,循环将无限执