![]() |
|
[Tut] Learn the Basics of MicroPython for Absolute Python Beginners - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Learn the Basics of MicroPython for Absolute Python Beginners (/thread-100170.html) |
[Tut] Learn the Basics of MicroPython for Absolute Python Beginners - xSicKxBot - 11-01-2022 Learn the Basics of MicroPython for Absolute Python Beginners 5/5 – (1 vote) ![]() 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. ![]() (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 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
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 PicoWriting 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: 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()![]() 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. ![]() Once you’ve saved, click the green Code: RunCode: Hello, World!![]() 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 ![]() 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![]() 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: ![]() 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![]() 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! ![]() 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! |