python学习日记(5/24-5/25)

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、位运算符

操作对象:数字(转为二进制数再操作)

&:按位与

|:按位或

^:按位异或

~:按位取反

<<:左移

>>:右移

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值