🏠 Home / Hub

🧺 Python Lesson 04 — Lists, Tuples, Sets, Dictionaries

← Back to Python Menu

1. Collection Comparison

TypeSyntaxMeaning
list[]Ordered, changeable
tuple()Ordered, not changeable
set{}Unique values, no order
dict{key: value}Key-value data

2. List

fruits = ["apple", "banana", "mango"]
print(fruits[0])
print(fruits[-1])

fruits.append("orange")
fruits.remove("banana")
fruits[0] = "grape"

print(fruits[0:2])  # slicing

3. Tuple

point = (10, 20)
x, y = point
print(x, y)

# point[0] = 99  # Error: tuple is immutable
မပြောင်းစေချင်တဲ့ fixed data တွေသိမ်းဖို့ tuple ကကောင်းတယ်။

4. Set

tags = {"php", "python", "php", "sql"}
print(tags)  # duplicate php တစ်ခုတည်းကျန်

tags.add("vue")
tags.discard("sql")

a = {1, 2, 3}
b = {3, 4, 5}
print(a | b)  # union
print(a & b)  # intersection

5. Dictionary

student = {
    "name": "Aye Aye",
    "age": 19,
    "city": "Yangon"
}

print(student["name"])
print(student.get("phone", "No phone"))

student["age"] = 20
student["email"] = "aye@example.com"

for key, value in student.items():
    print(key, value)

6. List Comprehension

numbers = [1, 2, 3, 4, 5]
squares = [n * n for n in numbers]
even_numbers = [n for n in numbers if n % 2 == 0]

print(squares)
print(even_numbers)

📌 Study Checklist