11-01-2022, 09:08 AM
Learn the Basics of MicroPython for Absolute Python Beginners
5/5 – (1 vote)
![[Image: image-286-1024x683.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-286-1024x683.png)
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.
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.
![[Image: image-279.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-279.png)
(1) Toolbar![[Image: 1fa9b.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1fa9b.png)
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![[Image: 1f4dd.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4dd.png)
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![[Image: 25fc.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/25fc.png)
The Python shell has two purposes.
You can run individual instructions by hitting the 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
In the following tutorial and video, I’ll show you how to get started with the Thonny IDE—feel free to check it out!![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Recommended Tutorial: Getting Started With Thonny – The Optimal IDE for the Raspberry Pi Pico
Writing Your First Code
![[Image: image-287-1024x683.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-287-1024x683.png)
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:
The results should be that the words print in the shell, but without quotation marks.
This is the syntax we use when we want to print something –, with whatever you want printed put inside the parentheses. If it’s words, we put them inside quotes.
![[Image: image-280.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-280.png)
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 on a Mac or 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 icon.
When prompted to decide where to save the file, choose the Raspberry Pi Pico and title your file. Don’t forget to add the 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.
![[Image: image-281.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-281.png)
Once you’ve saved, click the green button in the toolbar, and you’ll see print in the shell as you did before.
![[Image: image-282.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-282.png)
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:
Loops and Indentation
![[Image: image-288-1024x768.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-288-1024x768.png)
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 “ loop”, which runs a defined number of times.
First, let’s start a new program by clicking the icon (the white piece of paper
) then writing some code in the editor like so:
Then hit the (PC) or (Mac) key.
What you’re doing is assigning a variable,, for the loop to use as a counter and telling it that 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 or key, not only will a new line be created, but it will also automatically start at an indented 4 spaces in.
![[Image: image-283.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-283.png)
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 happens to be for each repetition of the loop.
So to do that, type
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 or for a new line, but this time, hit the 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
Your whole program will look like this:
![[Image: image-284.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-284.png)
Here for copy&paste:
Now go to the toolbar and click the button. We’re going to call this “”. Once saved, click the green button, and you’ll see the results:
![[Image: image-285.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-285.png)
Ok, I think that’s enough for today. Try changing the range from 10 by adding a start and stop number like this:
With and being whatever numbers you choose.
For example, if you use, 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 and so on.
Thanks for Reading!
![[Image: image-289-1024x683.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-289-1024x683.png)
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!
5/5 – (1 vote)
![[Image: image-286-1024x683.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-286-1024x683.png)
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.
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.
![[Image: image-279.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-279.png)
(1) Toolbar
![[Image: 1fa9b.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1fa9b.png)
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
![[Image: 1f4dd.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4dd.png)
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
![[Image: 25fc.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/25fc.png)
The Python shell has two purposes.
You can run individual instructions by hitting the
Code:
EnterThe 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!
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Recommended Tutorial: Getting Started With Thonny – The Optimal IDE for the Raspberry Pi PicoWriting Your First Code
![[Image: image-287-1024x683.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-287-1024x683.png)
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:
Code:
print("Hello, World!")The results should be that the words
Code:
"Hello, World!"This is the syntax we use when we want to print something –
Code:
print()![[Image: image-280.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-280.png)
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
Code:
Command+kCode:
Control+kPersonally, 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
Code:
SaveWhen prompted to decide where to save the file, choose the Raspberry Pi Pico and title your file
Code:
"Hello_World.py"Code:
".py"We’ll do that with every file we create with Thonny.
![[Image: image-281.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-281.png)
Once you’ve saved, click the green
Code:
RunCode:
Hello, World!![[Image: image-282.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-282.png)
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 Code:
"Hello World"
- Integers – whole numbers, like Code:
42
- Floats – numbers with decimals, like Code:
3.14
- Booleans – choices between two options, like orCode:
TrueCode:False
Loops and Indentation
![[Image: image-288-1024x768.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-288-1024x768.png)
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 “
Code:
forFirst, let’s start a new program by clicking the
Code:
new file
) then writing some code in the editor like so:Code:
print("Loop starting!")
for i in range(10):Then hit the
Code:
EnterCode:
ReturnWhat you’re doing is assigning a variable,
Code:
iCode:
iIn 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
Code:
: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
Code:
EnterCode:
Return![[Image: image-283.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-283.png)
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
Code:
iSo to do that, type
Code:
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
Code:
EnterCode:
ReturnCode:
backspaceWe don’t want Thonny to repeat the next instructions, so we need to make sure they are outside of the loop.
Then type
Code:
print("Loop ended!")Your whole program will look like this:
![[Image: image-284.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-284.png)
Here for copy&paste:
Code:
print("Loop starting!")
for i in range(10): print("Loop number", i)
print("Loop ended!")Now go to the toolbar and click the
Code:
saveCode:
Indentation.pyCode:
Run![[Image: image-285.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-285.png)
Ok, I think that’s enough for today. Try changing the range from 10 by adding a start and stop number like this:
Code:
for i in range(x, y):With
Code:
xCode:
yFor example, if you use
Code:
range(3, 14)Code:
iCode:
range(1, 11)Thanks for Reading!
![[Image: image-289-1024x683.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-289-1024x683.png)
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!

