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.
To create a new Python project for a Microbit, open the website python.microbit.org.
This will open the code editor with a new project. It might have some example code already added such as:
# Imports go at the top
from microbit import *
# Code in a 'while True:' loop repeats forever
while True:
display.show(Image.HEART)
sleep(1000)
display.scroll('Hello')
You should delete this code except for the import
line that you will need. This imports the necessary libraries you will need to code a microbit.
# Imports go at the top
from microbit import *
Comparison operators are used to compare values. Let's take a look at the basic comparison operators in MicroPython. These operators include:
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")
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.
To run your code in the python.microbit.org code editor, click on the Play button in the microbit simulator.
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 trueFor 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')
red Toyota car
Try changing the value of the colour
and make
variables to see how it effects what conditions are met.
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:
is_greater
accordinglyis_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:
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:
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()