In this lesson, you will learn about variables and data types in Python. Variables are like containers that store data, and data types define what kind of data they can hold, such as numbers or text. Understanding these concepts is essential for writing programs that can store and manipulate information.
By the end of this lesson, you will be able to create variables, assign different data types, and use them in simple programs on the Micro:bit. This lesson builds on what you learned in 'Getting Started with Python', so make sure you have the Micro:bit Python editor open at python.microbit.org.
from microbit import *
at the top of your code.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 *
A variable is a named storage location in your program's memory. You can assign a value to it and use that name to refer to the value later. In Python, you create a variable by choosing a name and assigning a value using the equals sign (=). Variables are useful because they allow you to store data that can be reused and modified easily throughout your program, making your code more flexible and readable.
For example:
age = 16
Here, 'age' is the variable name, and 16 is the value stored in it. Another example could be:
school_year = 5
This stores the number 5 in a variable called 'school_year'.
Variable names should be descriptive to make your code easier to understand, start with a letter or underscore, and can contain letters, numbers, or underscores. They are case-sensitive, so 'Age' and 'age' are considered different variables.
my_name = "Your Name"
below the import line. This stores a string value. Then, try adding another one like favourite_number = 7
.Now that you've started creating variables, it's important to understand the rules for naming them, known as variable syntax. Following these rules ensures your code runs without errors.
Variable names must:
Variable names are case-sensitive, so myAge
and myage
are considered different variables.
Examples of valid names: student_name
, _score
, level2
Examples of invalid names: 2level
(starts with number), student-age
(has hyphen), my variable
(has space)
Using descriptive names makes your code easier to read and understand.
valid_var = 10
and invalid-var = 5
. Run the code to see if errors appear for invalid names.Integers, often abbreviated as 'int', are whole numbers that do not have decimal points. They can be positive, negative, or zero, such as 5, -3, or 0. Integers are commonly used for counting items, like the number of students in a class, indexing positions in a list, or keeping track of scores in a game. They are efficient for operations where precision with fractions isn't required, and Python handles them seamlessly for arithmetic.
Let's create an integer variable and display it on the Micro:bit. Add this code to your editor:
from microbit import *
score = 10 # An integer variable
display.scroll(score)
score
to another integer, like 25 or -5, and run it again to see the update. Notice how the display shows the new value immediately.Variables aren't fixed; you can update their values during the program. This is useful for things like incrementing a score. Let's modify the code to add to the variable and display the new value. Update your code to:
from microbit import *
score = 10 # Starting integer value
display.scroll(score)
sleep(1000) # Pause for 1 second to see the first value
score = score + 15 # Adding 15 to the current score
display.scroll(score)
Floats, often abbreviated as 'float', are numbers that include decimal points, such as 3.14, -0.5, or 100.0. They are useful for representing measurements, calculations requiring precision, or any value that's not a whole number. For example, you might use floats for temperatures, distances, or averages. Python handles floats naturally in arithmetic operations, and they can be mixed with integers in calculations.
Let's create a float variable and display it on the Micro:bit. Add this code to your editor:
from microbit import *
temperature = 23.5 # A float variable
display.scroll(temperature)
temperature
to another float, like 98.6 or -1.2, and run it again to see the update. This shows how easy it is to work with decimal values.Just like with integers, float variables can be updated during your program. This is handy for simulations, like adjusting a temperature reading. Let's modify the code to add to the variable and display the new value. Update your code to:
from microbit import *
temperature = 23.5 # Starting float value
display.scroll(temperature)
sleep(1000) # Pause for 1 second to see the first value
temperature = temperature + 2.5 # Adding 2.5 to the current temperature
display.scroll(temperature)