Collections, Control Flow, Functions, and Error Handling
Summary
Building on the variables and data types from Chapter 1, this chapter introduces Python's collection types (lists, dictionaries, and tuples) and the control flow tools that make programs truly powerful: conditionals, loops, and functions. You will learn how to repeat actions with for and while loops, make decisions with if-else statements, organize code into reusable functions, import modules, and catch errors gracefully with try-except blocks. These skills are used in every MicroPython program you will write throughout the rest of the course.
Concepts Covered
This chapter covers the following 15 concepts from the learning graph:
- List
- Dictionary
- Tuple
- Conditional Statement
- If-Else Statement
- For Loop
- While Loop
- Function Definition
- Function Call
- Return Value
- Module Import
- Input Validation
- Error and Exception
- Try-Except Block
- Logical Operators
Prerequisites
This chapter builds on concepts from:
Welcome to Chapter 2
Great work finishing Chapter 1, coder! Now you know variables and data types. In this chapter you will learn to collect data, make decisions, repeat actions, and organize code into functions. These four skills are the engine of every MicroPython project you will build. Let's build something amazing!
Storing Multiple Values with Collections
In Chapter 1, each variable held exactly one value. Sometimes you need to store many values together — like a list of LED brightness levels, or a table of note names and their frequencies. Python gives you three built-in collection types for this.
Lists
A list is an ordered collection of values. You create a list by placing values inside square brackets [], separated by commas. Lists can hold any data type, and you can change them after you create them.
1 2 | |
You access individual items using an index — a number that counts from zero:
1 2 3 | |
You can add items with .append() and find the length with len():
1 2 | |
Dictionaries
A dictionary maps keys to values — like a real dictionary that maps words to definitions. You create a dictionary with curly braces {}.
1 2 3 4 5 6 7 8 9 | |
Tuples
A tuple is like a list, but you cannot change it after you create it. Use parentheses (). Use a tuple for values that should never change — for example, the pin numbers of your I2C bus.
1 2 | |
The following table summarizes the three collection types:
| Type | Brackets | Changeable? | Best used for |
|---|---|---|---|
| List | [ ] |
Yes | Growing collections: readings, queues |
| Dictionary | { } |
Yes | Lookups: name → value mappings |
| Tuple | ( ) |
No | Fixed groups: pin pairs, RGB constants |
Making Decisions with Conditional Statements
A conditional statement lets your program choose between two paths. Python checks a condition that evaluates to True or False, then runs only the matching block.
The if-else statement is the most common form:
1 2 3 4 5 6 | |
You can chain multiple conditions with elif:
1 2 3 4 5 6 7 8 9 10 | |
Logical Operators
Logical operators combine two or more conditions. There are three: and, or, and not.
and— both conditions must be Trueor— at least one condition must be Truenot— flips True to False, and False to True
1 2 3 4 5 6 7 8 9 | |
Key Idea: Colons and Indentation Define Blocks
Every if, elif, and else line ends with a colon :. The indented lines below it are the block that runs when that branch is chosen. When indentation returns to the original level, you are back to code that always runs.
Diagram: Interactive Conditional Flowchart
Interactive Conditional Flowchart MicroSim
Type: microsim
sim-id: conditional-flowchart
Library: p5.js
Status: Specified
Bloom Level: Understand (L2) Bloom Verb: explain Learning Objective: Students can trace the execution path of an if-elif-else chain for a given input value.
Canvas layout: - Left 65%: flowchart with four decision diamonds and four output rectangles - Right 35%: slider for the input value and a result display
Visual elements: - Diamonds: "reading < 10000?", "reading < 30000?", "reading < 50000?" - Rectangle outputs: "Very dark", "Dim", "Bright", "Very bright" - Active path highlighted in green; inactive paths in gray
Interactive controls: - Slider: "Sensor reading" (0–65535, default 512). As the slider moves, the active path animates. - Hover any diamond for a tooltip showing the Python condition and its True/False result.
Data Visibility Requirements: - Show current value, condition checked, result (True/False), and which output fires.
Instructional Rationale: Live slider lets students see routing through an if-elif-else chain as a directed path rather than a code listing, making control flow concrete.
Implementation: p5.js. Diamonds/rectangles drawn with quad/rect; active path redrawn on slider change; createSlider() for input.
Repeating Actions with Loops
A loop runs a block of code multiple times.
For Loops
A for loop repeats once for each item in a collection. Think of it as: "for every item on the list, do this."
1 2 3 4 | |
You can also loop over a range of numbers:
1 2 3 4 5 | |
While Loops
A while loop keeps running as long as a condition is True. Use it when you do not know in advance how many times to loop.
1 2 3 4 5 | |
Infinite Loops!
A while loop whose condition never becomes False runs forever and freezes your Pico. Always change the loop variable inside the loop. If your Pico freezes, hold the BOOTSEL button while plugging in USB to recover.
Organizing Code with Functions
A function is a named block of code that you can run whenever you need it. Write it once; call it many times. Think of a function as a recipe card — you write the recipe once, then follow it whenever you want that dish.
A function definition uses def, then the name, then parentheses, then a colon:
1 2 3 | |
A function call runs the function:
1 2 | |
Parameters and Return Values
Functions become more powerful with parameters (inputs) and a return value (output).
1 2 3 4 5 6 7 | |
Monty's Tip
Name your functions clearly. celsius_to_fahrenheit is perfect — you know exactly what it does. do_stuff or func1 tells you nothing. Good names let you read code like a sentence.
Importing Modules
A module is a file of ready-made functions. Instead of writing everything yourself, you import a module and use what is inside.
1 2 3 4 | |
You can import just the parts you need:
1 2 3 | |
The machine module gives you access to the Pico's hardware:
1 2 3 4 | |
Input Validation and Error Handling
Real programs receive input from sensors or users. That input is not always correct. Input validation means checking a value before you use it.
1 2 3 4 5 | |
Even with validation, things go wrong. An error (exception) is what Python raises when something unexpected happens. A try-except block catches it so your program keeps running.
1 2 3 4 5 6 7 | |
The following table shows the most common exceptions you will encounter:
| Exception | When it happens |
|---|---|
ValueError |
Wrong value type: int("hello") |
TypeError |
Wrong type for the operation: "hi" + 5 |
ZeroDivisionError |
Dividing by zero: 10 / 0 |
OSError |
File or hardware I/O problem |
Putting It All Together
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Key Takeaways
- A list can change; a tuple cannot; a dictionary maps keys to values.
if / elif / elselet your program choose between paths.and,or,notcombine conditions into one expression.- A for loop runs once per item; a while loop runs until its condition is False.
- A function groups code so you can reuse it by name.
try / exceptcatches errors before they crash your program.
Quick Check: What does this code print? (Click to reveal)
1 2 3 4 5 | |
60 — the loop adds 10 + 20 + 30 to the running total.
Excellent Work, Maker!
You now have the full Python toolkit: collections, decisions, loops, functions, modules, and error handling. Every MicroPython program in this course is built from exactly these pieces. In Chapter 3 you will install the tools that let you run your code on a real Pico. You are a coder!