ONE MONTH.
一、运算符(二)
1、逻辑运算符
与and、或or、非not
应用:数字类型、字符串、列表、元组、字典
字符串:
>>> 'a' and 'b'
'b'
>>> 'a' or 'b'
'a'
>>> not ''
True
>>> not '0'
False
>>> not []
True
>>> not [0,]
False
>>> not [0]
False
>>> not ()
True
>>> not {}
True
#or:有0取非零,都是非零取前面的
>>> [1] or []
[1]
>>> [] or [1]
[1]
>>> [1] or [5]
[1]
>>> [5] or [1]
[5]
2、成员运算符
in、not in
>>> a=1
>>> a in [1,2,3,4,5]
True
>>> b=6
>>> b in [1,2,3,4,5]
False
>>> b not in [1,2,3,4,5]
True
#对字典判断的是key
>>> b = 'c'
>>> b in {'c':1}
True
>>> b in {1:'c'}
False
3、身份运算符
is、not is.is 与==作用不同,==比较数值,is比较变量的内存地址(身份)
>>> a = 1
>>> b = 2
>>> a is b
False
>>> b=1
>>> a is b
True
>>> a==b
True
>>> b=1.0
>>> a==b
True
>>> a is b
False
#is 比较变量的内存地址(身份)
>>> a = {1,2,3}
>>> b = {2,1,3}
>>> a==b
True
#集合是无序的
>>> a is b
False
>>> c = (1,2,3)
>>> d = (1,3,2)
>>> c==d
False
#元组是有序的
>>> c is d
False
#instance
>>> a='hello'
>>> isinstance(a,str)
True
>>> isinstance(a,(int,float,str))
True
>>> isinstance(a,(int,float))
False
>>>
4、位运算符
操作对象:数字(转为二进制数再操作)
&:按位与
|:按位或
^:按位异或
~:按位取反
<<:左移
>>:右移