python中的运算符一些通用的,算术运算符、比较(关系)运算符、赋值运算符、逻辑运算符、位运算符就不再赘述了,这里主要就看一下成员运算符和身份运算符。
成员运算符
运算符 | 描述 | 实例 |
---|---|---|
in | 如果在指定的序列中找到值返回 True,否则返回 False。 | x 在 y 序列中 , 如果 x 在 y 序列中返回 True。 |
not in | 如果在指定的序列中没有找到值返回 True,否则返回 False。 | x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。 |
例子
#!/usr/bin/python
# -*- coding: UTF-8 -*-
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print "变量 a 在给定的列表中 list 中"
else:
print "变量 a 不在给定的列表中 list 中"
if ( b not in list ):
print "变量 b 不在给定的列表中 list 中"
else:
print "变量 b 在给定的列表中 list 中"
# 修改变量 a 的值
a = 2
if ( a in list ):
print "变量 a 在给定的列表中 list 中"
else:
print "变量 a 不在给定的列表中 list 中"
运行结果
变量 a 不在给定的列表中 list 中
变量 b 不在给定的列表中 list 中
变量 a 在给定的列表中 list 中
身份运算符
身份运算符用于比较两个对象的存储单元,其实就是类似于id(x),id(x)主要是获取对象内存地址。
运算符 | 描述 | 实例 |
---|---|---|
is | is 是判断两个标识符是不是引用自一个对象 | x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False |
is not | is not 是判断两个标识符是不是引用自不同对象 | x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。 |
例子
#!/usr/bin/python
# -*- coding: UTF-8 -*-
a = 20
b = 20
if ( a is b ):
print "1 - a 和 b 有相同的标识"
else:
print "1 - a 和 b 没有相同的标识"
if ( a is not b ):
print "2 - a 和 b 没有相同的标识"
else:
print "2 - a 和 b 有相同的标识"
# 修改变量 b 的值
b = 30
if ( a is b ):
print "3 - a 和 b 有相同的标识"
else:
print "3 - a 和 b 没有相同的标识"
if ( a is not b ):
print "4 - a 和 b 没有相同的标识"
else:
print "4 - a 和 b 有相同的标识"
运行结果
1 - a 和 b 有相同的标识
2 - a 和 b 有相同的标识
3 - a 和 b 没有相同的标识
4 - a 和 b 没有相同的标识
is 与 == 区别
is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。
>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True
>>> b == a
True
>>> b = a[:]
>>> b is a
False
>>> b == a
True
虽然数字的字符串值被认为与整型值和浮点型值完全不同, 但整型值可以与浮点值相等。
>>> 42 == '42'
False
>>> 42 == 42.0
True
>>> 42.0 == 0042.000
True
Python 进行这种区分, 因为字符串是文本, 而整型值和浮点型都是数字。
赋值
>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False
结果是截然不同的,为什么呢?
出现上面这种现象是因为一个叫小整数对象池的东西,python为了优化速度,会把【-5,256】之间的数据提前存放在小整数对象池中,程序中只要用的【-5,256】之间的数据不会再重新创建一份,都是指向对象池中的同一份数据,除了这个区间之外的数据,每次使用时系统都会重新申请一块内存,用来存储数据。
参考
当创建一个对象并给它赋一个变量的时候,这个变量仅仅参考那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定。如果想要复制一个列表或者类似的序列或者其他复杂的对象(不是如整数那样的简单对象),那么必须使用切片操作符来取得拷贝。如果你只是想要使用另一个变量名,两个名称都参考同一个对象,那么如果不小心的话可能会引来各种麻烦。
例子
#!/usr/bin/python
print 'Simple Assignment'
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist # mylist is just another name pointing to the same object!
del shoplist[0]
print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that both shoplist and mylist both print the same list without
# the 'apple' confirming that they point to the same object
print 'Copy by making a full slice'
mylist = shoplist[:] # make a copy by doing a full slice
del mylist[0] # remove first item
print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that now the two lists are different
运行结果
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']
具体更加深入的细节可以参考:
https://blog.youkuaiyun.com/kobebryantlin0/article/details/73391584