Learn the Basics of MicroPython for Absolute Python Beginners

5/5 - (4 votes)

When learning how to program your Raspberry Pi Pico, you need to learn basic concepts of a limited version of the Python programming language known as MicroPython.

Learn the Basics of MicroPython for Absolute Python Beginners

It’s a great way to learn Python, as it is much simpler than Python, which can be very complicated and take a long time to master.

For the purposes of using a microcontroller, you only need to know some basics to get started.

Before we jump in, let’s take a quick moment to familiarize you with your Python IDE, Thonny.

Using Thonny

To get started, there are three parts of the interface that you should be aware of — the toolbar, the editor, and the shell.

(1) Toolbar 🪛

The toolbar has icons that will help you do a few things, such as creating new files, opening existing ones, and saving, just like you see with most programs.

The other things you’ll want to notice are the green button, which will run your programs that you write in the editor and the stop button which will end programs that run forever.

We’ll get into that in the next tutorial.

(2) Editor 📝

The editor is where you will write your programs that you will save and run by using the run button in the toolbar. You may also see or hear this referred to as the script area, as this is where you write your scripts.

(3) Shell ◼️

The Python shell has two purposes.

You can run individual instructions by hitting the Enter key without having to write them in the editor and run them, but this only really is good for simple instructions. You won’t use it to write complex code.

The second purpose is to provide information about scripts that you have written, including where errors might be in your code.

This is sometimes referred to as REPL, which stands for

  • Read,
  • Evaluate,
  • Print, and
  • Loop.

In the following tutorial and video, I’ll show you how to get started with the Thonny IDE—feel free to check it out! 👇

🌍 Recommended Tutorial: Getting Started With Thonny – The Optimal IDE for the Raspberry Pi Pico

Writing Your First Code

Ok, so now that you know the parts of the interface, let’s try some code to get started.

We’ll begin with the universal first program, “Hello, World”. First, we’re going to try it in the shell since it’s only a one-line command.

Type the following command like so:

print("Hello, World!")

The results should be that the words "Hello, World!" print in the shell, but without quotation marks.

This is the syntax we use when we want to print something – print(), with whatever you want printed put inside the parentheses. If it’s words, we put them inside quotes.

Now, we’re going to run the program from the editor.

If you want to clear out anything you’ve done in the shell or just start fresh, then click in the shell and press either Command+k on a Mac or Control+k on a PC.

Personally, I like to keep my shell clean and clear when I’m doing something new.

Ok, so now that you’ve cleared the shell (if that’s what you chose to do), type the same command as before into the editor, then click the Save icon.

When prompted to decide where to save the file, choose the Raspberry Pi Pico and title your file "Hello_World.py". Don’t forget to add the ".py" to the end, as this is the file extension that denotes a Python file.

We’ll do that with every file we create with Thonny.

Once you’ve saved, click the green Run button in the toolbar, and you’ll see Hello, World! print in the shell as you did before.

There are two things going on here.

First, you are telling Thonny to print something out in the shell, and second, by using quotation marks, you are telling Thonny that what is to be printed is a string of text.

In programming languages like Python, there are what are known as “primitives”, which are basic data types to work with in your programming.

Those data types are:

  • Strings – just plain ol’ text, like "Hello World"
  • Integers – whole numbers, like 42
  • Floats – numbers with decimals, like 3.14
  • Booleans – choices between two options, like True or False

Loops and Indentation

Now you’re going to write your first loop, which is simply a term that refers to a set of instructions to be repeated.

You will be writing those instructions in a specific type of way. The reason for this is that Python programs run from top to bottom, but your interpreter is dumb.

It only knows what you tell it, so the way we give instructions to the Python interpreter is by grouping those instructions with indentations.

What we’re going to do is tell Thonny to print some text, run a loop, then print more text. When we give the instructions that are to be repeated by the loop, we will indent those instructions so that Thonny will “understand”.

There are two primary loop types, but today, we are only going to focus on what is called a “for loop”, which runs a defined number of times.

First, let’s start a new program by clicking the new file icon (the white piece of paper 📃) then writing some code in the editor like so:

print("Loop starting!")
for i in range(10):

Then hit the Enter (PC) or Return (Mac) key. 

What you’re doing is assigning a variable, i, for the loop to use as a counter and telling it that i will be every number in the range of 10 as the loop is performed.

In other words, the instructions will be repeated 10 times, as defined by the range.

💡 Info: Python is known as a 0-indexed language, which means the default start of the range is 0 rather than 1, so 10 is not included in the range. Therefore, numbers starting with 0 is the range 0-9, so i will be the numbers 0-9 in this example.

The other thing to notice here is the colon : at the end of the line.

That tells Thonny that you are about to give it instructions for the loop you just told it will be performed. When you hit the Enter or Return key, not only will a new line be created, but it will also automatically start at an indented 4 spaces in.

This is where you’re going to tell Thonny what instructions to repeat. For the sake of this exercise, we’re simply going to print to the shell.

However, this time, we’re going to print both a string and whatever number i happens to be for each repetition of the loop.

So to do that, type 

print("Loop number", i)

We need to separate multiple data types being printed on the same line with a comma so that Thonny “knows” that there are separate things to interpret because again, your interpreter only knows what you tell it.

After that, hit Enter or Return for a new line, but this time, hit the backspace key.

We don’t want Thonny to repeat the next instructions, so we need to make sure they are outside of the loop.

Then type 

print("Loop ended!")

Your whole program will look like this:

Here for copy&paste:

print("Loop starting!")
for i in range(10):
    print("Loop number", i)
print("Loop ended!")

Now go to the toolbar and click the save button. We’re going to call this “Indentation.py”. Once saved, click the green Run button, and you’ll see the results:

Ok, I think that’s enough for today. Try changing the range from 10 by adding a start and stop number like this:

for i in range(x, y):

With x and y being whatever numbers you choose.

For example, if you use range(3, 14), i will print as the numbers 3-13, since the last number is never included in the loop. If you want to print 1-10 instead of 0-9, you should use range(1, 11) and so on.

Thanks for Reading!

Next time, we will try a different kind of loop and explore what are known as “conditionals” that perform actions based on whether certain criteria are met. Until then, happy coding!

🌍 Next Tutorial: Learn Basics of MicroPython – Part 2