Microbit
Beginner
40 mins
Teacher/Student led
+180 XP

Conditionals and Loops in Python

Students use PRIMM to explore conditionals and loops in Python on the micro:bit, building from simple if statements through nested structures and combined loop-and-condition patterns, then solve two short challenges.

Teacher Class Feed

Load previous activity

    1 - Start: What We're Building ~4 mins

    Illustration for Start: what we're buildingToday you are coding in Python on the micro:bit. You will build control structures step by step at your device: conditionals that choose a path, and loops that repeat actions.

    Before you run anything, think: if a condition in an if statement is false, what do you expect the program to do?

    You will predict, build, run and fix as you go, then solve two short challenges that mix a loop with decisions.

    2 - Introduction ~2 mins

    In this lesson on control structures, you'll explore the building blocks that allow your programs to make decisions and repeat actions, essential for creating interactive and dynamic code on the Micro:bit. Control structures like conditionals and loops will enable you to respond to inputs, process data, and build more complex programs.

    Here's what you'll be learning and doing:

    1. Understand conditional statements (if, else, elif) to make decisions based on conditions.
    2. Explore nested if statements for handling multi-layered decisions.
    3. Learn about loops (for and while) to repeat code efficiently.
    4. Discover nested loops and combining loops with conditionals for advanced control.

    3 - Understanding Conditional Statements ~2 mins

    Conditional statements are a fundamental part of programming that allow your code to make decisions. They let your program execute different blocks of code depending on whether a certain condition is true or false. This is like choosing different paths based on the situation – for example, if it's raining, you might decide to take an umbrella; otherwise, you leave it behind.

    The basic building block is the if statement. It checks a condition, and if that condition evaluates to true, the code inside the if block runs. If you want to handle the case when the condition is false, you can add an else clause. For checking multiple conditions, you can use elif (which stands for 'else if') to test additional scenarios.

    Conditions are typically built using comparison operators. Here are some common ones:

    • == (equal to)
    • != (not equal to)
    • > (greater than)
    • < (less than)
    • >= (greater than or equal to)
    • <= (less than or equal to)

    You can also combine conditions using logical operators like and (both must be true), or (at least one must be true), and not (reverses the condition).

    For instance, imagine you're programming a Micro:bit to monitor temperature. You might write code to check if the temperature is above 30 degrees to display a 'Hot' warning, or between 20 and 30 for 'Warm', and below 20 for 'Cool'. This kind of decision-making makes your programs interactive and responsive.

    Think about a real-life decision you make daily, like what to wear based on the weather. How could you express that as an if-elif-else structure? In the next steps, we'll put this into practice with actual code.

    4 - Simple If Statement ~2 mins

    This simple if statement is like a gatekeeper – it only lets the code through if the condition is met. They are the foundation of making decisions in your code. In this step, we'll create a program that checks if a temperature variable is greater than 20 and, if it is, scrolls the message 'Warm' on the display. If the condition isn't met, nothing happens – that's how a basic if works.

    We'll start by importing the Micro:bit library, setting a variable, and then using the if statement to check the condition. Remember, indentation is key in Python – the code inside the if must be indented (usually by 4 spaces or by pressing the tab key).

    Add the following complete code to your editor:

    from microbit import *
    
    temperature = 25
    
    if temperature > 20:
        display.scroll("Warm")

    Here's a quick breakdown:

    • from microbit import *: This imports everything from the Micro:bit module so we can use functions like display.scroll.
    • temperature = 25: We create a variable called temperature and set it to 25.
    • if temperature > 20:: This checks if temperature is greater than 20. If true, the indented code runs.
    • display.scroll("Warm"): If the condition is true, this scrolls 'Warm' on the LED display.
    Now, run your code. You should see 'Warm' scrolling across the Micro:bit display because 25 is indeed greater than 20, making the condition true.
    Let's experiment to understand it better. Change the value of temperature to 15 and run the code again. Nothing should appear on the display because 15 is not greater than 20, so the condition is false and the code inside the if doesn't run. Try other values like 20, 21, or 10 to see exactly when the message displays. What happens if you use == instead of > in the condition? Test it out and observe.

    5 - If-else Statement ~2 mins

    Building on the simple if statement, we can add an else clause to handle what happens when the condition is false. This is like having a plan B – if the first condition isn't met, the code in the else block runs instead. It ensures that your program always does something, no matter the outcome of the condition.

    In this step, we'll update our temperature checker to display 'Warm' if the temperature is greater than 20, and 'Cool' otherwise. This makes the program more complete, as it provides feedback in both cases.

    Update your code to this complete version:

    from microbit import *
    
    temperature = 25
    
    if temperature > 20:
        display.scroll("Warm")
    else:
        display.scroll("Cool")

    Here's a quick breakdown:

    • temperature = 25: Sets the temperature variable.
    • if temperature > 20:: Checks if temperature is greater than 20.
    • display.scroll("Warm"): Runs if the condition is true.
    • else:: This block runs if the if condition is false.
    • display.scroll("Cool"): Scrolls 'Cool' when the condition isn't met.
    Now, run your code. You should see 'Warm' scrolling because 25 is greater than 20. Change the temperature to 15 and run again – this time, 'Cool' should appear since the condition is false.
    Experiment to deepen your understanding. Try changing the condition to temperature < 0 and update the else to display 'Freezing'. Test with values like 25, 0, -5, or 20. What if you use >= instead of >? Observe how it affects when 'Warm' or 'Cool' displays. This will help you see how if-else covers all possibilities.
    123learn · Online learning platform

    Unlock the full learning experience

    You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.

    Hundreds of curriculum-aligned lessons
    Interactive activities in every lesson
    Printable resources & progress tracking
    Copyright Notice
    This lesson is copyright of Coding Ireland 2017 - 2025. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more