十、Python——公共方法总结

1.+:可拼接,可用于字符串、元组、列表

print('hello'+'world')  # 拼接字符串
print(('hello','world') + ('ok'))  # 拼接元组

2.-:只能用于集合,求差集

print({1,2,3}-{3})  # {1.2}

3.*:用于字符串、元组、列表,表示重复多次,不能用于字典和集合(字典与集合不允许重复)

print('hello'*3)  #hellohellohello
print([1,2,3] * 3)  # [1,2,3,1,2,3,1,2,3]

4.in:成员运算符,都可用

注意:in用于字典时判断的是key

print('a' in 'abc') # True
print(i in [1,2,3]) # True
print(4 in (3,4,5)) # True
print(2 in {1,2,3}) # True
print('name' in {'name':'sxl','age':125})  # True
print('sxl' in {'name':'sxl','age':125})  # False

5.带下标的遍历:enumerate类的使用,一般用于列表和元组等有序的数据

nums = [4,6,2,8]
count = (4,6,2,8)


# i 是下标,e是nums中的每一个元素值
for i,e in enumerate(nums):
	print('第%d个数据是%d' % (i,e))
# 结果
# 第0个数据是4
# 第1个数据是6
# 第2个数据是2
# 第3个数据是8

for i,e in enumerate(count):
	print('第%d个数据是%d' % (i,e))
# 结果
# 第0个数据是4
# 第1个数据是6
# 第2个数据是2
# 第3个数据是8

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值