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

Getting Started with Python

In this lesson, you'll explore the basics of Python programming using the Micro:bit Python editor. Follow step-by-step instructions to understand Python syntax, write your first program, and experiment with displays and loops on a virtual or physical Micro:bit.
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 Python

    Python is a widely-used programming language known for its simplicity and readability. It was created by Guido van Rossum and first released in 1991. Python has become one of the most popular programming languages in the world, used in various fields such as web development, data analysis, artificial intelligence, and more.

    Python has a large community of developers, which means you'll find plenty of resources and support as you learn. With Python, you can build websites, create games, analyse data, and even program robots.

    In this lesson, you will learn the basics of Python and get familiar with the Micro:bit Python editor. 

    You do not need to have a physical micro:bit to do this course because you can use the virtual micro:bit in the editor. But if you do have a physical micro:bit you can load your code onto it for each of the projects.


    2 - Access the Micro:bit Python Editor

    Let's start off by opening the the Micro:bit Python editor at the URL python.microbit.org. This will open up a new browser tab and show the Python code editor where you will write and test your Python code throughout this course.

    When you start a new project, you might see some default code like this:

    # 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')

    Keep the first line of the code, which is:

    from microbit import *

    This line is important because it imports all the necessary functions and classes from the Micro:bit library, allowing you to use them in your code. Without this line, you won't be able to access the Micro:bit's features like the display or buttons.

    Delete the rest of the code before moving to the next step.
    Take a moment to explore the editor interface. Look at the virtual Micro:bit simulator on the left, the code area in the middle, and the buttons for running, downloading, and saving your code.

    3 - Understanding Python Syntax and Indentation

    Python has a simple syntax that makes it easy to read and write.

    One key feature is that Python uses indentation to define code blocks. Each level of indentation is represented by 4 spaces or a tab. This is different from other languages, such as JavaScript, that might use curly braces {}. Proper indentation is crucial because it tells Python which lines of code belong together.

    Here's an example:

    x = 5
    
    if x > 0:
        display.scroll("x is positive")  # this line is indented
    
    y = -3  # this line is NOT indented
    
    if y < 0:
        display.scroll("y is negative")

    Notice how the display.scroll statements are indented to show that they're inside the if blocks. The next lines of code after the first if block are not indented, indicating that they are not inside the first if block.

    Try typing this above code into your editor (remember to add the import line at the top). You can run the code if you wish but we mainly want to focus on getting the indentation right. If you make a mistake with indentation, Python will give you an error (a red dot will appear beside the line with the error).
    You don't need to run this code now; it's just to understand indentation. Delete it after practising.

    For comparison, here is what this code would look like in JavaScript, using curly braces to define the code blocks:

    let x = 5;
    
    if (x > 0) {
        console.log("x is positive");  // this line is inside the block
    }
    
    let y = -3;  // this line is outside
    
    if (y < 0) {
        console.log("y is negative");
    }

    Notice how the curly braces {} enclose the code that belongs to each if statement, unlike Python's indentation.

    4 - Working with Python Comments

    Comments are an essential part of any programming language. They allow you to add notes and explanations to your code without affecting how the code runs. In Python, you can create comments using the # symbol. Any text after the # symbol on the same line will be considered a comment and ignored by the Python interpreter.

    When we say 'ignored by the interpreter', it means that the Python interpreter, which reads and executes your code, will not consider the text after the # symbol as part of the code. It will treat it as a note or explanation, and it won't affect the execution of your program.

    Here's an example:

    # This is a comment
    display.scroll("Hello, World!")  # This is an inline comment

    Practise by adding some comments to a blank script in your editor. For example, write a comment describing what you think the code will do, even if there's no code yet.

    You don't need to add this code to your project permanently; just understand how adding comments works.

    5 - Write Your First Hello World Program

    Now, let's write your first Python program, the classic 'Hello, World!'. In the Micro:bit Python editor, type the following code:

    from microbit import *
    
    display.scroll("Hello, World!")
    Click the 'Run' button on the virtual micro:bit to see the output of your program. You should see the text "Hello, World!" scrolling across the virtual Micro:bit display.
    Observe how the text scrolls. Try changing the message to something else, like your name, and run it again to see the change.

    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