You are on page 1of 26

Collection

Tuan Nguyen - AI4E


Fundamental data types
● int: Integer, -100, 0, 1, 1000

● float: Float, -0.1, -100, 0.8

● bool: Boolean, True/False

● str: String, “hello world”, “Thanh Tuan”

=> Python collection


Python Collections
There are 4 collection data types in Python
● List: ordered and changeable. Allows duplicate members.
[1, 2, 3]
● Tuple: ordered and unchangeable. Allows duplicate members.
(1, 2, 3)
● Set: unordered and unindexed. No duplicate members.
{1, 2, 3}
● Dictionary is unordered, changeable and indexed. No duplicate members.
{‘Tuan’: 1, ‘Minh’: 2}
List
List_name = [item1, item2, item3, ...]

len(list_name): get length of the list


Access item
List_name[index]
● Index starts 0
● Negative: count from the end
Change item
List_name[index] = new_value
Range
List_name[start : end]
Default: start = 0, end = len(list_name)
Loop item
For x in my_list:
print(x)
List comprehension
Num = [1, 2, 3, 4, 5]
Odd = [i for i in num if i % 2 == 0]
Check item exist
If item in my_list
Other methods
● append(new_item) : add new item to the end of list
● remove(item): remove item from list
● insert(position, item): insert item into position
● pop(position): remove item at the position
● clear(): remove all items
● extend(other_list): join 2 list
Other methods
Exercises
1. fruits = ["apple", "banana", "cherry"]
a. In list, length list
b. Xóa phần tử thứ hai khỏi list
c. Thêm “grape” vào cuối list
d. Kiểm tra xem “coca” có trong list không?
e. Xóa tất các phần tử trong list

1. Input list = [1, 2, 3, 5] -> output list = [1, 4, 9, 25]


Tuple
My_tuple = (item1, item2, item3)
My_tuple = item1, item2,
Tuple is ordered and unchangeable
Tuple unpack
Var1, var2, … = my_tuple
Tuple methods
● count(item): count the appearance of item in tuple
● index(item): find index of item

Exercise: Cho 1 tuple điểm học sinh của 1 lớp, kiểm tra xem: a.có bao nhiêu học
sinh được điểm 8, b. Tìm điểm cao nhất, c. Tính điểm trung bình của lớp.
Tuple in list
List = [(item1, item2), (item3, item4), (item5, item6)]
Set
My_set = {item1, item2, item3}
Unordered and unindexed
Similar to List, but:
● Cannot change item’s value
● No duplicated value
Set methods
● add(item)
● remove(item)
● clear
● set(my_list): convert set to list
When to use Set?
● Get unique element in list
● Math operation like union, intersection,...
https://www.datacamp.com/community/tutorials/sets-in-python
Dictionary
My_dict = {key1:value1, key2:value2}
unordered, changeable and indexed
Dictionary Loop
.values() : value
.items():
key, value
Dict methods
● Key in dict: check key exist
● len(dict): length of dictionary
● Dict[new_key] = new_value: add item
● dict.pop(key): remove item
Bài tập
1. Cho 1 dict điểm học sinh. Cho người dùng nhập tên 1 bạn, kiểm tra điểm bạn
đấy, nếu không có trong danh sách lớp thì thông báo cho người dùng.
2. Merge 2 list thành 1 dict:
Input: keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
Output: dics = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
Q&A
The end

You might also like