Loops β For & While
Automate repetition. Loops are the backbone of efficient programming β they let you execute code multiple times without writing it repeatedly.
Why Loops Matter
Imagine printing numbers 1 to 100. Without loops, you'd write 100 print statements. With a loop: just 2 lines. Loops are essential for processing lists, reading files, and building algorithms.
The for Loop
A for loop iterates over a sequence (like a range, list, or string). It executes the code block once for each item in the sequence.
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
---
2
5
8
---
P - 80
y - 121
t - 116
h - 104
o - 111
n - 110
range(5) gives 0,1,2,3,4. range(2,10,3) starts at 2, stops before 10, steps by 3 β giving 2,5,8. The stop value is exclusive.The while Loop
A while loop runs as long as its condition is True. Use it when you don't know in advance how many iterations you need.
Count: 1
Count: 2
Count: 3
Count: 4
---
0
1
2
3
4
---
0
1
3
4
break as a safety net.Knowledge Check β Module 7
Q1: What does range(3, 8, 2) produce?
Lists & Dictionaries
Two of Python's most powerful data structures for storing collections of data.
Lists β Ordered, Mutable Collections
Lists store multiple items in a single variable. They are ordered (items have a position), mutable (can be changed), and allow duplicates.
| Operation | Code | Result |
|---|---|---|
| Create | fruits = ["apple", "banana"] | List with 2 items |
| Access | fruits[0] | "apple" |
| Add | fruits.append("mango") | Adds to end |
| Remove | fruits.remove("banana") | Removes by value |
| Length | len(fruits) | Number of items |
mango
3
['apple', 'banana', 'mango', 'orange']
['banana', 'mango']
3
Dictionaries β Key-Value Pairs
Dictionaries store data as key: value pairs. They are unordered (Python 3.7+ maintains insertion order), mutable, and keys must be unique. Think of them like a real dictionary β you look up a word (key) to find its meaning (value).
92
dict_keys(['name', 'age', 'grades', 'is_active', 'major'])
dict_values(['Alice', 21, [85, 92, 78], True, 'CS'])
N/A
Knowledge Check β Module 8
How do you safely access a dictionary key that might not exist?
Functions
Reusable blocks of code. Functions are the building blocks of clean, maintainable programs.
Why Functions?
Functions let you write code once and reuse itζ ζ°ζ¬‘. They make code readable, testable, and modular. Instead of copying-pasting the same logic, you call a function.
5 + 3 = 8
Hello, Alice!
Hi, Bob!
Low: 2, High: 9
return sends a value back to the caller. print() just displays it. A function without return returns None. Use return when you need the result for further computation.Practice Exercises
Apply everything from Modules 1-9 with hands-on problems.
10 Practice Problems
- Write a function that returns the factorial of a number.
- Create a program that counts vowels in a string.
- Build a simple calculator using functions.
- Find the largest number in a list without using max().
- Write a function that checks if a string is a palindrome.
- Create a dictionary from two lists (keys and values).
- Build a number guessing game using while loop.
- Write a function that returns Fibonacci sequence up to n.
- Create a program that removes duplicates from a list.
- Build a simple to-do list manager.
File I/O β Reading & Writing Files
Persist data beyond program execution. Read from and write to files on your system.
File Modes
| Mode | Description |
|---|---|
'r' | Read (default) |
'w' | Write (overwrites) |
'a' | Append (adds to end) |
'r+' | Read + Write |
Python is amazing.
with: The with statement automatically closes the file, even if an error occurs. No need to call f.close() manually.Exception Handling
Gracefully handle errors so your program doesn't crash unexpectedly.
Execution complete.