Lists, Tuples and Dictionaries
Lists — ordered, changeable
courses = ["Python", "JavaScript", "SQL"]
courses.append("Java")
courses[0] = "Python 3"
print(courses) # ['Python 3', 'JavaScript', 'SQL', 'Java']
print(len(courses)) # 4
print(courses[-1]) # 'Java' -- negative indexing counts from the end
Lists are the workhorse data structure in Python: ordered, indexable, and able to grow or shrink freely with methods like append(), remove(), and insert(). A list can also hold mixed types, though in practice most real code keeps a list’s items all the same kind of thing.
Slicing
numbers = [10, 20, 30, 40, 50]
print(numbers[1:3]) # [20, 30] -- index 1 up to (not including) 3
print(numbers[:2]) # [10, 20] -- from the start
print(numbers[2:]) # [30, 40, 50] -- to the end
print(numbers[::-1]) # [50, 40, 30, 20, 10] -- reversed
Slicing is one of Python’s most powerful and most reused features — the same [start:stop:step] syntax works on lists, strings, and tuples identically.
Tuples — ordered, unchangeable
coordinates = (28.6, 77.2)
# coordinates[0] = 30 -> this raises a TypeError, tuples are immutable
Use a tuple when the data shouldn’t change after creation — coordinates, RGB color values, or a fixed pair like (width, height). Because tuples are immutable, they’re also slightly faster than lists and can be used as dictionary keys, which regular lists cannot.
Dictionaries — key/value pairs
student = {
"name": "Yash",
"course": "Python",
"completed_lessons": 6
}
print(student["name"]) # Yash
student["completed_lessons"] += 1
print(student.get("grade", "N/A")) # N/A -- safe lookup with a default
Use .get() instead of square brackets whenever a key might not exist — student["grade"] would raise a KeyError and crash your program, while student.get("grade", "N/A") returns a safe fallback instead.
Looping over a dictionary
for key, value in student.items():
print(f"{key}: {value}")
Lists, tuples, and dictionaries cover most real-world data shapes you’ll model in any program — a course’s lessons are naturally a list, a lesson’s metadata is naturally a dictionary, and a fixed coordinate pair is naturally a tuple. Recognizing which shape fits your data is one of the most valuable instincts you’ll build as you keep coding.