You're previewing the student experience. You don't need to complete this lesson — your progress and XP aren't saved.
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 - Introduction to Python ~6 mins

    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. In fact, Python is often the first language taught in schools and universities due to its beginner-friendly syntax.

    In this lesson, you will learn the basics of Python and get familiar with the Micro:bit Python editor. The Micro:bit is a small, programmable device that can be used to create exciting projects and learn programming concepts. By the end of this lesson, you'll have a solid foundation in Python and be ready to explore more advanced topics.

    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, analyze data, and even program robots. Let's get started on your Python journey!

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


    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 commands you need 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.

    6 - Write Your First Python Program ~2 mins

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

    from microbit import *
    
    display.scroll("Hello, World!")

    Click the 'Run' button to see the output of your program. You should see the text "Hello, World!" displayed in the output area.


    7 - Add Comments to Your Code ~3 mins

    Now, let's add some comments to your code to explain what it does. Update your code to look like this:

    # This is a simple Python program
    
    # Import the microbit libraries
    from microbit import *
    
    # Print a message to the screen
    display.scroll("Hello, World!")

    Run your code again to make sure it still works as expected. Remember, the comments won't affect the output of your program.

    8 - Understanding Code Sequence ~4 mins

    Now let's look at the sequence in which the code runs. Update your code to the following:

    # This is a simple Python program
    
    # Import the microbit libraries
    from microbit import *
    
    # Display an image
    display.show(Image.HEART)
    sleep(1000)
    display.show(Image.ANGRY)

    Let's break down the code:

    • We use the display.show() function to display the 'HEART' image on the Micro:bit screen.
    • We use the sleep(1000) function to pause the program for 1000 milliseconds (1 second).
    • Finally, we display the 'ANGRY' image on the Micro:bit screen.

    Run your code and observe the sequence of events on the Micro:bit screen.


    9 - Wrap up ~2 mins

    Congratulations on completing this introductory lesson on Python!

    You've learned the basics of Python programming, including its syntax, indentation, and comments. You've also explored the Micro:bit Python editor and written your first Python program. Keep practicing and experimenting with Python and Micro:bit to build more complex projects and enhance your digital skills.

    10 - Make Sense: What Worked, What Broke ~3 mins

    As a class, share one thing that worked straight away and one thing that broke.

    How did you spot the bug, and what change fixed it? If you made a prediction at the start, check it against what the program actually did.

    11 - Reflect ~2 mins

    Discuss:

    • Where did your code not do what you expected the first time, and how did you find and fix the bug?
    • What transferred from blocks, and what felt new about typing and syntax?

    12 - What You Covered ~1 mins

    Today you:

    • Opened the micro:bit Python editor and met Python indentation and comments
    • Wrote and ran a first Python program that shows a message
    • Used sequence so images and pauses run in order
    • Practised debugging when the output was not what you expected

    Before you finish, save your project where your teacher has told you to keep your work, so it is there next lesson.

    End of lesson
    123learn · Sample lesson

    That was a sample lesson from Advanced Coding

    Every lesson in the course is ready to teach like this one. See what else comes with it, and how to order it.

    Copyright Notice
    This lesson is copyright of Coding Ireland 2017 - 2026. 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