Lists
Dicts
Tuples
Lists
Intro to list
- List is an ordered sequence of data (有順序性的連續資料, 用中括號表示), such as [12, “Hello”, True]. List is just like array in other programming languages.

- If we have similar or associated data, we can store them in a list instead of putting them individually in memory.
friend1 = "Mike"
friend2 = "Josh"
friend3 = "Jerry"
print(f"{friend1}, {friend2}, {friend3} are my friend")
# Mike, Josh, Jerry are my friend
friendList = ["Mike", "Josh", "Jerry"]
print(f"{friendList[0]}, {friendList[1]}, {friendList[2]} are my friend")
# Mike, Josh, Jerry are my friend
# 與loop配合使用
- List of Lists can be used to represent a matrix

- len can check the length of list
friendList = ["Mike", "Josh", "Jerry"]
print(len(friendList))
# 3
- Indexing rule from string can be used in list and slicing rules work with list as well
luckyNumbers = [3, 6, 9, 11, 17, 26, 31]
print(luckyNumbers[-1]) # 31
print(luckyNumbers[0:3]) # [3, 6, 9]
print(luckyNumbers[::2]) # [3, 9, 17, 31]
print(luckyNumbers[::-1]) # [31, 26, 17, 11, 9, 6, 3]
- count() method can count the occurrence of elements. index() method returns the first index of the object.
No find() method in List.
x = [1, 2, 3, 1, 3, 1, 1]
print(x.count(1)) # 4
print(x.index(1)) # 0