Container
容器
By Rex Wu && 小草
容器?
Container Datatypes
list1 = []
list2 = list()
nameList = ['Tom', 'John', 'Rex']
intList = [1, 2, 3, 4, 5]
floatList = [1.1, 2.2, 3.3, 4.4, 5.5]
mixList = ['Tom', 2, 3.3]
list('cat')
tuple1 = ('dog', 'cat', 'fish')
list(tuple1)
date = '2019/11/13'
date.split('/')
list = ['dog', 'cat', 'fish', 'bird']
list[0] #dog
list[-1] #bird
list = ['dog', 'cat', 'fish', 'bird']
list[0:2] #['dog','cat']
list[::2] #['dog','fish']
list[::-1] #['bird','fish','cat','dog']
list = ['dog', 'cat', 'fish', 'bird']
list[2] = 'pig'
#'fish' => 'pig'
list.append('cow')
# ['dog','cat','pig','bird','cow']
list.insert(3, 'sheep')
# ['dog','cat','pig','sheep','bird','cow']
list.remove('bird')
# ['dog','cat','pig','sheep','cow']
list.pop()
# ['dog','cat','pig','sheep'] & return cow
del list[0]
# ['cat','pig','sheep']
list.clear()
# ['cat','pig','sheep']
list.index('sheep') # 2
'pig' in list # True
'fish' in list # False
list.count('pig') # 1
len(list) # 3
numList = [3,1,10,5,2]
numList.sort()
numList.sort(reverse=True)
list = [1,[2,3],4]
# 如何輸出 2 or 3
串列中的串列
list[1][0] # 2
list[1][1] # 3
輸入一個陣列:
取最大值
取最小值
取中位數
(提示:for)
tuples = 'dog', 'cat', 'fish'
a, b, c = tuples
list = ['dog', 'cat', 'fish']
tuple(list)
簡單來說 tuple 就是不可變更的 list
key - value
value 可以有多種型態
key 只能為 string
個人檔案
摩斯密碼對照(密碼: 明碼)
菜單(餐點: 價錢)
Others...?
{"name": "小草", "height": 180, "weight": None}
"name": "小草"
key
value
for key in dict:
print(key)
and store values in a list
list = []
for key in dict:
list.append(dict[key])
dict.update({"nickname": "falcon",
"comment": "I like python"})
dict = {"name": "小草", "height": 180, "weight": 76}
dict.update(dict2)
dict["weight"] = 76
dict = {"name": "小草", "height": 180, "weight": None,
"other": None}
dict[key] = value
del dict["other"]
dict = {"name": "小草", "height": 180, "weight": 76,
"other": None}
del dict[key]
dict.keys()
dict.values()
value = dict.get(key)
value = dict[key]
dict.pop(key) #Pop an item by key
dict.popitem() # FILO
dict.setdefault(key, value)
dict.clear()
名字
身高
年齡
一句你想說的話
值不重複
set 內可以有多種型態
交集、連集、差集運算
a = {1, 2, 3}
b = {2, 4, 6}
s = set()
s.add(value)
s.discard(value)
s.pop()
a.union(b) # = a | b
a.intersection(b) # = a & b
a.difference(b) # = a - b
a.symmetric_difference(b) # = a ^ b
a.issubset(b)
sum(s)
len(s)
min(s)
max(s)
且 A 是 B 的子集合
Hint: 使用 list,裡面包含有朋友資訊的 dict
你是海大新聞的總編輯。有人投稿了一篇文章,你決定要使用在下週的報紙上,請你使用Python寫出個計算稿費的程式算出要給投稿者的稿費~
計費方式如下,大寫不計費
小寫部分只計算母音,價目表如下
a: 2元 e: 2元 i: 3元 o: 3元 u: 8元