Today 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?
Keep the start tight. Devices on the micro:bit Python editor, students logged in. Optional predict: before anyone runs code, ask what might transfer from blocks and what might feel new with typing and syntax. Park one or two answers to revisit in make sense. Model only the editor layout briefly; pairs will type in the later build steps.
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!
Read this as a short orientation, not a lecture. Link Python’s readability to the block languages students already know. Name the micro:bit Python editor as the tool for this course. No typing yet; check that every device can see the editor tab when you move on.
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.
Model opening the editor and finding Run. Point out the default heart and scroll sample if it appears, then stress keeping the import line. Common bug: students delete from microbit import * and nothing works. Differentiation: stay with anyone who loses the tab or cannot find Run.
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.
On the board, show how four spaces place a line inside a block. Key question: which lines belong together, and which stand alone? Watch for mixed tabs and spaces or accidental indent on a line that should be flush left. Do not add the sample as a full student build; use it to investigate indentation only.
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
Demonstrate a # comment and Run so students see comments change nothing on the micro:bit. Key question: why write notes the computer ignores? Misconception: students think comments must sit only at the top, or they forget the # and cause a syntax error. Fix on the spot by adding # and re-running.
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.