Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Controlling program flow

In the previous lesson, you learned how to store and organise data using variables, lists, and dictionaries.

That was an essential step, but it raises an obvious next question:

What if we want to apply the same operation to many values?

So far, every calculation you wrote was explicit: you told Python exactly which values to use and exactly what to do with them.

This lesson introduces a shift in how you think about code.

Instead of writing more lines, you will learn how to write rules that Python can apply automatically.


1. From data to behaviour

Once data is stored in collections, such as lists or dictionaries, the real power of programming comes from being able to:

This is where loops and conditional logic come in.

Loops control repetition. Conditions control decisions.

Together, they define the flow of a program: what runs, how often, and under which circumstances.


2. Why loops matter

Without loops, programs do not scale.

Imagine spatial analysis without repetition:

Loops allow you to express intent instead of repetition:

“Do this for every value.” “Keep going until a condition is met.” “Stop as soon as something important happens.”

This lesson teaches you how to express these ideas clearly and safely.


3. Why this matters for SDS

In spatial data science, repetition is unavoidable:

Loops are the bridge between:

They turn static data into dynamic processes.


4. Learning objectives

After this lesson, you will be able to:


5. Looking ahead

Lesson 3 is about making code do more work for you.

You learn not only how loops work, but how to:

These skills are essential whenever your code must process more than a single value.

In the next lesson, you will take this one step further by learning how to package repeated logic into functions. Functions allow you to turn loops and conditions into named, reusable units, making your code easier to read, test, and extend.

If Lesson 2 was about thinking in data, Lesson 3 is about thinking in processes— and Lesson 4 will be about thinking in structure and reuse.