Topics covered: Lists, tuples, dictionaries, sets, list comprehensions, CSV file handling with the csv module
Learning objectives: By the end of this week you will be able to apply data structures and file handling concepts to real datasets, write executable Python code for each technique, and complete both graded assignments independently.
A list is an ordered, mutable collection. Lists support indexing (0-based), negative indexing (-1 for last element), and slicing. Key methods: append(), extend(), insert(), remove(), pop(), sort(), len(). A tuple is an ordered, immutable sequence. Use tuples for data that must not change: coordinates, column definitions, configuration constants.
scores = [72, 88, 65, 91, 78, 55, 83, 90, 67, 74]
print(f'Count: {len(scores)}')
print(f'Max: {max(scores)}')
print(f'Min: {min(scores)}')
print(f'Mean: {sum(scores)/len(scores):.1f}')
scores.sort(reverse=True)
print(f'Top 3: {scores[:3]}')
# Tuple for geographic coordinates (immutable)
ibadan_coords = (7.3775, 3.9470)
lagos_coords = (6.5244, 3.3792)
cities = [('Ibadan', ibadan_coords), ('Lagos', lagos_coords)]
for city_name, (lat, lon) in cities:
print(f'{city_name}: {lat}N, {lon}E')
A dictionary stores key-value pairs in curly braces. Keys must be unique and immutable. Access values with dict['key'] or the safer dict.get('key', default). Iterate with .items() for key-value pairs. A set is an unordered collection of unique items. Use sets to remove duplicates and for fast membership testing. Set operations: union (|), intersection (&), difference (-).
# Dictionary - research participant record
participant = {
'id': 'P001', 'name': 'Chinwe Eze', 'age': 34,
'diagnosis': 'Type 2 Diabetes',
'hba1c_values': [7.2, 6.9, 7.5],
'enrolled': True
}
print(participant.get('blood_pressure', 'Not recorded'))
# Add field
participant['treatment_group'] = 'intervention'
# Sets - unique survey regions
regions = ['South-West','North-Central','South-West','South-East','South-West']
unique = set(regions)
print(f'{len(unique)} unique regions: {unique}')
A list comprehension: [expression for item in iterable if condition]. More concise than for loops and often faster. Python's csv.DictReader() reads each row as a dictionary with column names as keys. Always use the with statement (context manager) to open files - it closes the file automatically even if an exception occurs.
# List comprehension - normalise scores to 0-1 range
scores = [55, 72, 88, 91, 64]
min_s, max_s = min(scores), max(scores)
normalised = [(s - min_s) / (max_s - min_s) for s in scores]
print([round(v, 3) for v in normalised])
# Even squares only
even_squares = [n**2 for n in range(1,11) if n % 2 == 0]
print(even_squares) # [4, 16, 36, 64, 100]
# Write CSV file
import csv
results = [
{'name': 'Amara', 'score': 88, 'grade': 'Distinction'},
{'name': 'Emeka', 'score': 74, 'grade': 'Merit'},
]
with open('results.csv', 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=['name','score','grade'])
writer.writeheader()
writer.writerows(results)
Submit completed notebooks to your GitHub repository before the next session. Feedback within 48 hours.
Create a student_database dictionary with 5 records (student ID as key, dict with name/age/marks/course as value). Write a function that returns the student ID with the highest average mark.
Read any CSV dataset, extract one numerical column, compute mean and standard deviation using a loop only (no numpy), and write a summary to a text file.