Python - list (课堂笔记整理)

  1. Building a List from Scratch
stuff = list() # an empty list
stuff.append('book') # add an element
stuff.append(99)
print(stuff)
# ['book', 99]
stuff.append('cookie')
print(stuff)
# ['book', 99, 'cookie']
  1. Is Something in a List ?
some = [1, 9, 21, 10, 16]
9 in some # True
15 in some # False
20 not in some # True
  1. Lists are in Order
friends = ['b', 'c', 'a']
friends.sort()
print(friends) # ['a', 'b', 'c']
print(friends[1]) # b
  1. Some Build-in Functions
nums = [3, 41, 12, 9, 74, 15]
print(len(nums)) # 6
print(max(nums)) # 74
print(min(nums)) # 3
print(sum(nums)) # 154
print(sum(nums) / len(nums)) #25.6
  1. Best Friends: Strings and Lists
    tips:
    使用.split() 默认是space,也可以.split(‘特定的分隔符’)
    可以用好多次哦!分1次,再分1次!得到想要的结果!
abc = 'With three	words'
stuff = abc.split()
print(stuff)
# ['With', 'three', 'words']
print(len(stuff)) # 3
print(stuff[0]) # With
for w in stuff :
	print(w)
# With
# three
# words
line = 'first;second;third'
thing = line.split()
print(thing)
# ['first;second;third']
print(len(thing)) # 1
thing = line.split(';')
print(thing)
# ['first', 'second', 'third']
print(len(thing)) # 3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值