Professional Documents
Culture Documents
Bài 04 - Collections Trong Python
Bài 04 - Collections Trong Python
Bài 04 - Collections Trong Python
• Lists
• Tuples
• Sets
• Dictionaries
# in các phần tử trong flowers trên dòng # kiểm tra sự tồn tại của phần tử trong List
for f in flowers: if "Mai" in flowers:
print(f) print("Chúc mừng Mai")
# duyệt và lấy ra các flower có chứa chữ a
newflowers=[]
# lấy độ dài List
for f in flowers:
print(len(flowers))
if "a" in f:
newflowers.append(f)
print(newflowers)
# có thể viết rút gọn inline
newflowers=[f for f in flowers if 'a' in f]
print(newflowers)
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
0968.27.6996
tuyensinh@bachkhoa-aptech.edu.vn
www.bachkhoa-aptech.edu.vn