ICT582 Python Revision
🎯 Learning Objectives
πŸ“š Key Concepts

Lists β€” Ordered, Mutable

Created with square brackets []. Items can be any type and can be changed.

fruits = ["apple", "banana", "cherry"]
nums   = [10, 20, 30, 40, 50]

# Indexing (starts at 0)
print(fruits[0])    # apple
print(fruits[-1])   # cherry (last element)

# Slicing [start:stop] β€” stop is exclusive
print(nums[1:4])    # [20, 30, 40]
print(nums[:3])     # [10, 20, 30]
print(nums[::2])    # [10, 30, 50] (every 2nd)

List Methods

MethodWhat it does
L.append(x)Add x to end
L.extend([a,b])Add all items from another list
L.insert(i, x)Insert x at index i
L.remove(x)Remove first occurrence of x
L.pop(i)Remove & return item at index i (default: last)
L.sort()Sort in place (ascending)
L.reverse()Reverse in place
L.index(x)Return index of first x
L.count(x)Count occurrences of x
len(L)Number of elements
L = [3, 1, 4, 1, 5]
L.append(9)   # [3, 1, 4, 1, 5, 9]
L.sort()      # [1, 1, 3, 4, 5, 9]
L.pop()       # removes 9; L = [1, 1, 3, 4, 5]
print(len(L)) # 5

Tuples β€” Ordered, Immutable

Created with round brackets (). Once created, items cannot be changed.

point  = (3, 4)
colors = ("red", "green", "blue")

print(point[0])    # 3
print(colors[1])   # green

# Tuple unpacking
x, y = point
print(x, y)        # 3 4

# Cannot modify:
point[0] = 10      # TypeError!

Use tuples for data that should not change (coordinates, RGB values, etc.).

Sets β€” Unordered, Unique Elements

Created with curly braces {}. Automatically removes duplicates. No indexing (unordered).

s = {3, 1, 4, 1, 5, 9, 2, 6, 5}
print(s)    # {1, 2, 3, 4, 5, 6, 9} β€” no duplicates

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print(A | B)    # Union:        {1, 2, 3, 4, 5, 6}
print(A & B)    # Intersection: {3, 4}
print(A - B)    # Difference:   {1, 2}
print(A ^ B)    # Symmetric diff: {1, 2, 5, 6}

Set methods: s.add(x), s.remove(x), s.discard(x), len(s).

Dictionaries β€” Key-Value Pairs

Created with curly braces {key: value}. Keys must be unique. Access by key, not index.

student = {"name": "Alice", "age": 20, "gpa": 3.8}

# Access
print(student["name"])        # Alice
print(student.get("age"))     # 20  (.get returns None if key missing)

# Add / update
student["city"] = "Perth"     # adds new key
student["age"]  = 21          # updates existing

# Delete
del student["city"]

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

Dictionary Methods

MethodWhat it does
d.keys()Returns all keys
d.values()Returns all values
d.items()Returns (key, value) pairs
d.get(key, default)Returns value or default if key missing
key in dCheck if key exists (True/False)

Comparison: List vs Tuple vs Set vs Dict

TypeOrderedMutableDuplicatesAccess by
List []YesYesYesIndex
Tuple ()YesNoYesIndex
Set {}NoYesNoMembership test
Dict {k:v}Yes*YesKeys: NoKey

*Dicts maintain insertion order in Python 3.7+

πŸ’» Code Examples

Example 1 β€” List operations

marks = [72, 85, 61, 90, 78]
marks.sort(reverse=True)      # Sort descending
print("Top mark:", marks[0])  # 90
print("Average:", sum(marks) / len(marks))  # 77.2

Example 2 β€” Word frequency counter with dict

words = ["cat", "dog", "cat", "bird", "dog", "cat"]
freq = {}
for w in words:
    freq[w] = freq.get(w, 0) + 1
print(freq)  # {'cat': 3, 'dog': 2, 'bird': 1}

Example 3 β€” Remove duplicates with set

nums = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
unique = list(set(nums))
print(sorted(unique))  # [1, 2, 3, 4, 5, 6, 9]
⚠️ Exam Focus
  1. Know how list indexing and slicing works β€” L[1:4] gives items at index 1, 2, 3.
  2. Distinguish mutable (list, dict, set) vs immutable (tuple, str) β€” tuples cannot be changed after creation.
  3. Dictionary: access with d[key] raises KeyError if missing; use d.get(key) to avoid this.
  4. Sets auto-remove duplicates β€” use when you need unique values.
  5. Know common list methods: append, pop, sort, remove.
❌ Common Mistakes
⚑ Quick Recap
← Topic 04: Loops Topic 06: Functions β†’