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

Mastering Variables

In this step-by-step lesson, you'll delve into the world of Python variables, learning how to declare, assign, and manipulate them. You'll explore different variable types and naming conventions. To put your newfound knowledge into practice, you'll create a higher or lower game using the Microbits Python editor.
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 variables in Python, how to declare and assign them, their types, and good naming conventions. You will also create a higher or lower game using the Microbits Python editor.

    2 - What are Variables?

    Variables are used to store values in a program. They have a name and can store different types of data such as numbers, text, and more. In Python, you don't need to specify the type of data a variable will store, as it automatically detects the type based on the value you assign to it.

    Let's take a look at some examples of declaring and assigning variables of different types:

    age = 42
    message = "Hello, World!"
    isOpen = True
    productPrice = 3.14

    In this example, we have created four variables:

    • 'age' stores an integer
    • 'message' stores some text
    • 'isOpen' stores a true/false value
    • 'productPrice' stores a decimal number

    3 - Declaring and Assigning Variables

    To declare and assign a variable in Python, you simply write the variable name followed by an equal sign and the value you want to store. For example:

    my_variable = 10

    This creates a variable named 'my_variable' and assigns it the value 10.

    4 - Variable Types

    Python has several variable types, including:

    • int: integer values (e.g., 5)
    • float: decimal values (e.g., 5.5)
    • str: string values (e.g., 'Hello')
    • bool: boolean values (e.g., True or False)

    Python automatically detects the variable type based on the value you assign to it. For example:

    number_variable = 5
    float_variable = 5.5
    string_variable = 'Hello'
    boolean_variable = True

    5 - Changing Variable Values

    Let's learn how to change variable values, including incrementing, decrementing, and string concatenation.

    1. Incrementing a variable:

    counter = 5
    counter = counter + 1 # the value counter will now be 6

    This increases the value of 'counter' by 1, making it 6.

    2. Decrementing a variable:

    counter = 5
    counter = counter - 1 # the value counter will now be 4

    This decreases the value of 'counter' by 1, making it 4.

    3. String concatenation:

    greeting = "Hello"
    name = "John"
    message = greeting + ", " + name # the message counter will now be 'Hello, John'

    This combines the strings 'greeting' and 'name' with a comma and space, resulting in the message 'Hello, John'.

    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 Coding Ireland. 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