Microbit
Beginner
40 mins
Teacher/Student led
+120 XP

Your First Python Program

Students write and run their first Python program in the micro:bit Python editor, linking familiar ideas from blocks to typed code, indentation, comments, and sequence.

Teacher Class Feed

Load previous activity

    1 - Start: What We're Building ~4 mins

    Illustration for Start: what we're buildingToday you are coding in Python with the micro:bit editor.

    You can already make things move and show messages with blocks. Now you will type a short first program, run it, and fix anything that does not work.

    Before you build, think: what might be the same as blocks, and what might feel new when the code is typed?

    2 - Blocks to Text: What Changes ~6 mins

    You have been building programs by dragging blocks. Today you type the same kind of instructions instead.

    The thinking does not change: still a sequence, still run in order. What changes is that the computer no longer stops you making a shape that cannot fit. Spelling has to be exact, and the spaces at the start of a line, called indentation, are part of the code rather than decoration.

    Expect errors. An error message is the editor telling you where it stopped, which is more than a block ever told you.



    3 - Access the Micro:bit Python Editor ~5 mins

    Visit the Micro:bit Python editor by clicking here. 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.


    4 - Python Indentation ~4 mins

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

    Python uses indentation to define code blocks. Each level of indentation is represented by 4 spaces or a tab. 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.

    You don't need to add this code to your project, just understand how the code indentation works.

    5 - Python Comments ~4 mins

    Comments are an essential part of any programming language. They allow you to add notes and explanations to your code. 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 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
    print("Hello, World!")  # This is an inline comment
    You don't need to add this code to your project, just understand how adding comments works.
    123learn · Online learning platform

    Unlock the full learning experience

    You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.

    Hundreds of curriculum-aligned lessons
    Interactive activities in every lesson
    Printable resources & progress tracking
    Copyright Notice
    This lesson is copyright of Coding Ireland 2017 - 2025. 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