Level: Basic | Prerequisite: Week 5 (Loops)
Learning Goals
- Creating, indexing, and slicing lists
- Commonly used list methods (
append,pop,sort, etc.) - Features of tuples and unpacking
- Differences between lists and tuples
1. Lists
A data structure that holds multiple values in order. Square brackets [...].
fruits = ["Apple", "Banana", "Grape"]
print(fruits[0]) #Apple
print(fruits[-1]) #Grapes (counting from the back)
print(len(fruits)) # 3
2. Slicing
[start:end:interval]. The end is not included.
nums = [10, 20, 30, 40, 50]
print(nums[1:4]) # [20, 30, 40]
print(nums[:3]) # [10, 20, 30]
print(nums[2:]) # [30, 40, 50]
print(nums[::-1]) #[50, 40, 30, 20, 10] (reverse order)
print(nums[::2]) # [10, 30, 50]
3. Frequently Used Methods
| Method | Action |
|---|---|
append(x) |
Add to the end |
insert(i, x) |
Insert at position i |
pop() / pop(i) |
Remove and return the last / i-th element |
remove(x) |
Remove value x |
sort() |
Sort (in-place) |
reverse() |
Reverse (in-place) |
index(x) |
Position of x |
count(x) |
Count of x |
nums = [3, 1, 4, 1, 5]
nums.append(9) # [3, 1, 4, 1, 5, 9]
nums.sort() # [1, 1, 3, 4, 5, 9]
nums.reverse() # [9, 5, 4, 3, 1, 1]
print(nums.count(1)) # 2
4. Iteration and enumerate
fruits = ["Apple", "Banana", "Grape"]
for fruit in fruits:
print(fruit)
for i, fruit in enumerate(fruits, start=1):
print(f"Number {i}: {fruit}")
5. Tuples
Similar to lists but immutable. Parentheses (...).
point = (10, 20)
x, y = point #Unpacking
print(x, y) # 10 20
#Tuples are immutable
# point[0] = 5 # TypeError
6. Lists vs Tuples
| List | Tuple | |
|---|---|---|
| Syntax | [...] |
(...) |
| Mutability | Mutable | Immutable |
| Usage | Frequently changing data | Fixed data (coordinates, RGB, etc.) |
| Speed | Slow | Fast |
Key Examples (examples/)
| File | Content |
|---|---|
01_list_basic.py |
Creating and indexing lists |
02_slicing.py |
Slicing |
03_methods.py |
append/pop/sort, etc. |
04_tuple.py |
Tuples and unpacking |
⚠️ Common Mistakes
- Index out of range
a = [1, 2, 3] print(a[3]) # IndexError
→ The last is a[-1] or a[len(a)-1].
- Assign the return value of
sort()to a variablesorted_list = nums.sort() #None! No return value since it is in-place alignment
→ After nums.sort(), nums itself is sorted. For a new list, use sorted(nums).
- List copy pitfall
a = [1, 2, 3] b = a #Refers to the same list! b.append(4) print(a) #[1, 2, 3, 4] — a also changes
→ To copy, use b = a.copy() or b = a[:].
- Creating an empty tuple
()is an empty tuple. A single-item tuple is(5,)(comma required).(5)is just the integer 5.
❓ FAQ
Q1. Can different types be mixed in a list?
A. Yes. [1, "hi", True, [1,2]] is possible. However, the same type is recommended.
Q2. Why is the end of a slice non-inclusive?
A. range, for consistency with indices, etc. Length calculation is naturally end - start.
Q3. What is the difference between sort() and sorted()?
A. sort() is a method that modifies the original. sorted() is a function that returns a new list.
📝 Exercises (exercises/)
exercise1.md— Student Score Statisticsexercise2.md— Lotto Number Generatorexercise3.md— List Reversal (Two Methods)
Next Week
Week 7. Dictionary and Set — Key-Value Mapping