Microbit Python Computer Science
Beginner
80 mins
Teacher/Student led
What you need:
Chromebook/Laptop/PC

Variables and Data Types

In this lesson, you'll explore the basics of variables and data types in Python using your Micro:bit. Learn how to create variables, assign different data types, and apply them in simple programs to store and manipulate information effectively.
Learning Goals Learning Outcomes Teacher Notes

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 to Variables

    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.

    Start with a new project in the editor. Remember to include the import line: from microbit import * at the top of your code.

    2 - What are Variables?

    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.

    Try creating a variable in your editor. Type: my_name = "Your Name" below the import line. This stores a string value. Then, try adding another one like favourite_number = 7.
    You don't need to run the code yet; just practise creating a few variables to get comfortable with the syntax.

    3 - Variable Syntax

    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:

    • Start with a letter (a-z or A-Z) or an underscore (_). They cannot start with a number.
    • Contain only letters, numbers, or underscores after the first character.
    • Not include spaces or special characters like !, @, #, $, or -.
    • Not use Python reserved words (keywords) such as 'if', 'for', 'while', or 'True'.

    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.

    In your editor, try creating a few variables with valid and invalid names. For example, add valid_var = 10 and invalid-var = 5. Run the code to see if errors appear for invalid names.
    Remember, good naming habits will help as your programs get more complex.

    4 - Integer Data Type

    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)
    Run the code. You should see '10' scrolling on the virtual Micro:bit display.
    Try changing the value of 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)
    Run the code. You should see '10' scroll first, then after a short pause, '25' scroll as the updated value.
    Experiment by changing the amount added (e.g., +5 or -3) or starting with a different initial value. This shows how variables can change dynamically in your programs.

    5 - Float Data Type

    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)
    Run the code. You should see '23.5' scrolling on the virtual Micro:bit display. Notice how the decimal point is handled automatically.
    Try changing the value of 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)
    Run the code. You should see '23.5' scroll first, then after a short pause, '26.0' scroll as the updated value.
    Experiment by changing the amount added (e.g., +1.75 or -3.0) or starting with a different initial value. Try multiplying instead, like temperature = temperature * 1.1, to see how floats handle different operations.

    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