Microbit Python
Advanced
60 mins
Teacher/Student led
135 points
What you need:
Chromebook/Laptop/PC
Microbit

Operators Decoded

In this step-by-step lesson, you'll explore comparison operators, logical operators, and conditional Booleans in MicroPython. You'll apply these concepts in a practical project to create a Temperature Indicator using the Microbit's online editor. By the end, you'll have a deeper understanding of how operators work and how to utilise them in your coding projects.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction

    In this lesson, you will learn about comparison operators, logical operators, and conditional Booleans in MicroPython. You'll then use what you learnt to create a Temperature Indicator project.

    You'll be using the Microbit's online editor to create and run your code. Let's get started by creating a new project.

    2 - Comparison Operators

    Comparison operators are used to compare values. Let's take a look at the basic comparison operators in MicroPython. These operators include:

    • Equal to (==)
    • Not equal to (!=)
    • Greater than (>)
    • Less than (<)
    • Greater than or equal to (>=)
    • Less than or equal to (<=)

    For example:

    5 == 5 # True
    5 != 5 # False
    5 > 3  # True
    5 < 3  # False
    5 >= 5 # True
    5 <= 5 # True

    Now, let's see how to use these comparison operators in if and elif statements. Add the following code to your project (you can copy and paste to speed things up):

    from microbit import *
    
    number = 7
    
    if number == 5:
        display.scroll("equal to 5")
    elif number != 5:
        display.scroll("not equal to 5")
    
    if number > 5:
        display.scroll("greater than 5")
    elif number < 5:
        display.scroll("less than 5")
    
    if number >= 5:
        display.scroll("greater than or equal to 5")
    elif number <= 5:
        display.scroll("less than or equal to 5")
    When you run the code, you should see the following output:
    not equal to 5
    greater than 5
    greater than or equal to 5
    Try changing the value of the number variable to see how it effects what conditions are met.

    3 - Logical Operators

    Logical operators are used to combine conditional statements. The basic logical operators in MicroPython are:

    • and i.e. (condition 1) and (condition 2) are true.
    • or i.e. (condition 1) or (condition 2) is true.
    • not i.e. (condition 1) is not true

    For example:

    (colour == 'red') and (make == 'Toyota') # red Toyota cars
    (colour == 'red') or (make == 'Toyota') # red cars or Toyota cars
    not (colour == 'red') # cars that are not red
    

    Now, let's see how to use these logical operators in if and elif statements. Replace the previous code with the following code:

    from microbit import *
    
    colour = 'red'
    make = 'Toyota'
    
    if (colour == 'red') and (make == 'Toyota'):
        display.scroll('red Toyota car')
    elif (colour == 'red') or (make == 'Toyota'):
        display.scroll('either a red car or a Toyota car')
    else:
        display.scroll('not a red Toyota')
    When you run this code, you should see the following output:
    red Toyota car
    Try changing the value of the colour and make variables to see how it effects what conditions are met.

    4 - Conditional Booleans

    Conditional Booleans are used to make decisions in your code based on the result of a condition. The condition can be a comparison or a logical operation.

    If you look at the following code, we create a boolean (true or false) variable called is_greater and set it to False:

    x = 5
    y = 3
    is_greater = False

    Then in the following code:

    • we compare the values of x and y
    • set the boolean variable is_greater accordingly
    • display a message on the Microbit's LED screen based on the value of is_greater
    if x > y:
        is_greater = True
    
    if is_greater:
        display.scroll('x is greater than y')
    else:
        display.scroll('x is not greater than y')

    This is how conditional booleans are used in if and elif statements, we can just use if booleanVariable: or if not booleanVariable: rather than if booleanVariable == True: or if booleanVariable != True:

    5 - Micro:bit Temperature Indicator

    Now, let's a create a new project that uses what we just learnt about Operators. We're going to create a temperature indicator using the micro:bit's built-in temperature sensor. The micro:bit will display:

    • "Cold" when the temperature is below 15°C.
    • "Warm" when the temperature is between 15°C and 25°C.
    • "Hot" when the temperature is above 25°C.
    • "Very Hot" when the temperature is above 35°C.
    • "Extremely Hot" when the temperature is above 40°C.

    Create a new project and add the following code to import the microbit libraries, then in a loop, create a variable and read the temperature from the micro:bit's sensor into it:

    from microbit import *
    
    while True:    
        current_temperature = temperature()

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. 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