Zhiguang Huo (Caleb)
Wednesday Oct 26th, 2022
## ['H', 'e', 'l', 'l', 'o']
## [1, 1, 1]
## [1, 0, 3]
## ['Alice', 'Beth', 'Carl', 'Dan', 'Emily']
## ['Alice', 'Beth', 'Dan', 'Emily']
## ['L', 'u', 'c', 'k', 'y']
## 'Lucky'
## 'Lemonade'
## [1, 2, 3, 4, 5]
## [1, 2, 3, 4, 5]
## [1, 5]
## [0, 1, 2, 3]
## 2
## 2
## 1
## [0, 1, 2, 3, 4, 5]
## [0, 1, 2, 3, 4, 5]
## [0, 1, 2]
## [0, 1, 2, 3, 4, 5]
## ['to', 'be', 'or', 'not', 'to', 'be']
## 0
## 3
## 'not'
alist.index("XX")
## [1, 2, 3, 'four', 5, 6]
## [1, 2, 3, 'four', 5, 6]
## 9
## [0, 1, 2, 3, 4, 5, 6, 7, 8]
## 8
## [0, 1, 2, 3, 4, 5, 6, 7]
## ['to', 'be', 'or', 'not', 'to', 'be']
## ['be', 'or', 'not', 'to', 'be']
alist.remove("XX")
## ['c', 'b', 'a']
## [3, 4, 5]
## ['a', 'b', 'c']
## None
## [3, 4, 5]
## [3, 4, 5]
## [3, 4, 5]
## [5, 3, 4]
## [3, 4, 5]
## ['bb', 'aaa', 'cccc']
## [5, 4, 3]
## {'Alice': 2341, 'Beth': 4971, 'Carl': 9401}
## 3
## 4971
## {'Alice': 1358, 'Beth': 4971, 'Carl': 9401}
## {'Alice': 9572, 'Beth': 4971, 'Carl': 9401}
## True
## {'name': 'Amy', 'age': 24}
## {}
## {'key': 'value'}
## {'key': 'value'}
## {'key': 'value'}
## {}
## {'username': 'admin', 'machines': ['foo', 'bar']}
## {'username': 'Alex', 'machines': ['foo', 'bar']}
## {'username': 'admin', 'machines': ['foo', 'bar']}
## {'username': 'Alex', 'machines': ['foo']}
## {'username': 'admin', 'machines': ['foo']}
## {'username': 'admin', 'machines': ['foo', 'bar']}
## {'username': 'admin', 'machines': ['foo']}
## {'username': 'admin', 'machines': ['foo', 'bar']}
## {'name': None, 'age': None}
## {'name': None, 'age': None}
## {'name': 'unknown', 'age': 'unknown'}
## 'Amy'
## 'Amy'
d["XX"]
d.get("XX")
d.get("XX", "No exist") ## set your own return value for get
## {'Alice': 2341, 'Beth': 4971, 'Carl': 9401}
## dict_items([('Alice', 2341), ('Beth', 4971), ('Carl', 9401)])
## [('Alice', 2341), ('Beth', 4971), ('Carl', 9401)]
## Alice--> 2341
## Beth--> 4971
## Carl--> 9401
## 2341
## 4971
## 9401
## Alice--> 2341
## Beth--> 4971
## Carl--> 9401
## dict_values([2341, 4971, 9401])
## [2341, 4971, 9401]
## 2341
## 4971
## 9401
names = ["Amy", "Beth", "Carl", "Dan", "Emily", "Frank"]
import random
students_scores = {name: random.randint(0, 100) for name in names}
passed_students = {key: value for (key, value) in students_scores.items() if value > 60}
passed_students
## {'Frank': 93}
## 2341
## {'Beth': 4971, 'Carl': 9401}
## ('Carl', 9401)
## {'Alice': 2341, 'Beth': 4971}
## (0, 1, 2, 3, 4, 5)
## (0, 1, 1, ['You', 'like', 'python'])
## 2
## 3
sets are a collection of unordered unique elements
create
## {1, 2, 3, 4}
## {0, 1, 2, 3}
set_a = {"a", "b", "c", "d"}
set_b = {"c", "d", "e", "f"}
print(set_a.union(set_b), '----------', set_a | set_b)
## {'e', 'd', 'b', 'c', 'a', 'f'} ---------- {'e', 'd', 'b', 'c', 'a', 'f'}
## {'d', 'c'} ---------- {'d', 'c'}
## {'a', 'b'} ---------- {'a', 'b'}
## {'e', 'b', 'a', 'f'} ---------- {'e', 'b', 'a', 'f'}
## set()