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

Scope Showdown: Local vs. Global

Embark on a journey to understand the concept of variable scope in programming. Discover the differences between local and global variables, their lifetimes, and their accessibility. Learn best practices for using them and apply your knowledge in a practical project using MicroPython on a Micro:bit to create a temperature logger.
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

    Have you ever wondered where variables live and how long they stick around in your programs? Just like how we sometimes have private thoughts and public announcements, variables in programming can be private (local) to a specific part of your program or public (global) for the whole program to access.

    This lesson will uncover the mysteries behind these concepts. Ready to dive deep? Let's go.



    2 - Variable Scope

    In programming, the term scope refers to the region of the program where a variable can be accessed. Not all variables are accessible from every part of our program, and sometimes, this is a good thing! It helps keep our code organized and error-free.

    3 - Local Variables

    Local variables are variables that are defined inside a function and cannot be accessed outside of that function. Their "lifetime" or duration of existence is only as long as the function is running.

    from microbit import *
    
    def show_message():
        message = "Hello from inside the function!"
        display.scroll(message)
    
    show_message()
    # display.scroll(message)  # This would cause an error, as 'message' is local to the function.
    

    Here, the variable message is local to the show_message() function. If you try to access it outside of the function, Python will raise an error.

    Create a new project and add in this code. Uncomment the last line by removing the # at the beginning. The editor will show an error as you cannot access the message variable outside of the show_message() function.

    4 - Global Variables

    Global variables are defined outside of all functions and can be accessed from any part of the program, including inside functions (provided we use the global keyword within the function).

    from microbit import *
    
    global_variable = "I am global!"
    
    def display_global():
        global global_variable # we need to include this so we can access global_variable inside the function
        display.scroll(global_variable)
    
    display_global()  # This will display the global variable's value.
    

    Here, global_variable is accessible both outside and inside of our function.

    Replace the code in your project with this code and run it. The microbit should display "I am global!".

    5 - Best Practices

    It's important to use global variables sparingly. Overusing them can make your code harder to understand and debug. Local variables, being confined to their functions, often make your intentions clearer and your code more organized.

    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