Python中的小细节

1.算术运算符

d=10
d/=8  #其中d=1.25
d//=8  #其中d=1 直接取整数(非四舍五入)
3.0//2=1.0

CJ: **幂运算–优先级
当其左侧为一元运算符,其优先级高于一元运算符
当其右侧为一元运算符,其优先级低于一元运算符

举例:

-3**2=-9  #相当于-(3**2)
3**-2=0.111111  #相当于3的-2次方

2.列表[ ]

list1=list[:] 
list2=list    #列表浅拷贝
list3=list1+list2 #拼接列表其中+左右两侧数据类型必须一致  一般建议使用extend方法
temp=[12,'jerry',[3,45]]
>>>3 not in temp
True #只能判断一层
list=['jerry',1,24]
list1=list[:]
list2=list
list.append('Phil')
print("list:{},地址为:{}".format(list,id(list)))
print("list1:{},地址为:{}".format(list1,id(list1)))
print("list2:{},地址为:{}".format(list2,id(list2)))
运行结果:
list['jerry', 1, 24, 'Phil'],地址为:2377760461320
list1:['jerry', 1, 24],地址为:2377760461832
list2:['jerry', 1, 24, 'Phil'],地址为:2377760461320

3.元组Tuple
对于元组来说,逗号’,'是关键

tuple=(1,2,3,4,5)
temp=(1)  #其类型为int
temp=(1,) #其类型为tuple
temp=2,3,4,5  #其类型为tuple
temp=()  #表示空元组
8*(8)=64
8*(8,) #表示8个元素为8的元组(8,8,8,8,8,8,8,8)
tu=('王','李','赵')
tu=tu[:1]+('郑',)+tu[1:]  #其中逗号不能省略,因为+两边的数据类型要保持一致  tu=('王','郑','李','赵')

4.字典
不是序列,字典里面的顺序不一

a={1:'one',2:'two',3:'three'}
b=a.copy()#浅拷贝
c=a
c[4]='four'
print("a:{},地址为:{}".format(a,id(a)))
print("b:{},地址为:{}".format(b,id(b)))
print("c:{},地址为:{}".format(c,id(c)))
运行结果:
a:{1: 'one', 2: 'two', 3: 'three', 4: 'four'},地址为:2377789929896
b:{1: 'one', 2: 'two', 3: 'three'},地址为:2377789929976
c:{1: 'one', 2: 'two', 3: 'three', 4: 'four'},地址为:2377789929896
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值