Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Learn the Basics of MicroPython for Absolute Python Beginners

#1
Learn the Basics of MicroPython for Absolute Python Beginners

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;852391&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="683" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-286-1024x683.png" alt="" class="wp-image-852426" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-286-1024x683.png 1024w, https://blog.finxter.com/wp-content/uplo...00x200.png 300w, https://blog.finxter.com/wp-content/uplo...68x512.png 768w, https://blog.finxter.com/wp-content/uplo...ge-286.png 1401w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>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 <strong><em>MicroPython</em></strong>. </p>
<p>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. </p>
<p>For the purposes of using a microcontroller, you only need to know some basics to get started. </p>
<p>Before we jump in, let’s take a quick moment to familiarize you with your Python IDE, <a href="https://blog.finxter.com/getting-started-with-thonny-the-optimal-ide-for-the-raspberry-pi-pico/" data-type="post" data-id="802754" target="_blank" rel="noreferrer noopener">Thonny</a>.</p>
<h2>Using Thonny</h2>
<p>To get started, there are three parts of the interface that you should be aware of — the toolbar, the editor, and the shell.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="688" height="346" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-279.png" alt="" class="wp-image-852393" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-279.png 688w, https://blog.finxter.com/wp-content/uplo...00x151.png 300w" sizes="(max-width: 688px) 100vw, 688px" /></figure>
</div>
<h3>(1) Toolbar <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1fa9b.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h3>
<p>The <strong>toolbar</strong> 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. </p>
<p>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. </p>
<p>We’ll get into that in the next tutorial.</p>
<h3>(2) Editor <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4dd.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h3>
<p>The <strong>editor</strong> 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.</p>
<h3>(3) Shell <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/25fc.png" alt="◼" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h3>
<p>The <strong>Python shell</strong> has two purposes. </p>
<p>You can run individual instructions by hitting the <code>Enter</code> 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.</p>
<p>The second purpose is to provide information about scripts that you have written, including where errors might be in your code. </p>
<p>This is sometimes referred to as <strong><em>REPL</em></strong>, which stands for </p>
<ul>
<li><strong>R</strong>ead, </li>
<li><strong>E</strong>valuate, </li>
<li><strong>P</strong>rint, and </li>
<li><strong>L</strong>oop.</li>
</ul>
<p>In the following tutorial and video, I’ll show you how to get started with the Thonny IDE—feel free to check it out! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/getting-started-with-thonny-the-optimal-ide-for-the-raspberry-pi-pico/" data-type="URL" data-id="https://blog.finxter.com/getting-started-with-thonny-the-optimal-ide-for-the-raspberry-pi-pico/" target="_blank" rel="noreferrer noopener">Getting Started With Thonny – The Optimal IDE for the Raspberry Pi Pico</a></p>
<h2>Writing Your First Code</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="683" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-287-1024x683.png" alt="" class="wp-image-852427" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-287-1024x683.png 1024w, https://blog.finxter.com/wp-content/uplo...00x200.png 300w, https://blog.finxter.com/wp-content/uplo...68x512.png 768w, https://blog.finxter.com/wp-content/uplo...ge-287.png 1401w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Ok, so now that you know the parts of the interface, let’s try some code to get started. </p>
<p>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 <a href="https://blog.finxter.com/python-one-line-x/" data-type="post" data-id="10612" target="_blank" rel="noreferrer noopener">one-line</a> command. </p>
<p>Type the following command like so:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print("Hello, World!")</pre>
<p>The results should be that the words <code>"Hello, World!"</code> print in the shell, but without quotation marks. </p>
<p>This is the syntax we use when we want to print something – <code><a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">print()</a></code>, with whatever you want printed put inside the parentheses. If it’s words, we put them inside quotes.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="941" height="309" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-280.png" alt="" class="wp-image-852413" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-280.png 941w, https://blog.finxter.com/wp-content/uplo...300x99.png 300w, https://blog.finxter.com/wp-content/uplo...68x252.png 768w" sizes="(max-width: 941px) 100vw, 941px" /></figure>
</div>
<p>Now, we’re going to run the program from the editor. </p>
<p>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+k</code> on a Mac or <code>Control+k</code> on a PC. </p>
<p>Personally, I like to keep my shell clean and clear when I’m doing something new.</p>
<p>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>Save</code> icon. </p>
<p>When prompted to decide where to save the file, choose the <strong>Raspberry Pi Pico</strong> and title your file <code>"Hello_World.py"</code>. Don’t forget to add the <code>".py"</code> to the end, as this is the file extension that denotes a Python file. </p>
<p>We’ll do that with every file we create with Thonny.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="937" height="618" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-281.png" alt="" class="wp-image-852414" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-281.png 937w, https://blog.finxter.com/wp-content/uplo...00x198.png 300w, https://blog.finxter.com/wp-content/uplo...68x507.png 768w" sizes="(max-width: 937px) 100vw, 937px" /></figure>
</div>
<p>Once you’ve saved, click the green <code>Run</code> button in the toolbar, and you’ll see <code>Hello, World!</code> print in the shell as you did before.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="938" height="619" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-282.png" alt="" class="wp-image-852416" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-282.png 938w, https://blog.finxter.com/wp-content/uplo...00x198.png 300w, https://blog.finxter.com/wp-content/uplo...68x507.png 768w" sizes="(max-width: 938px) 100vw, 938px" /></figure>
</div>
<p>There are two things going on here.</p>
<p>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 <em>string </em>of text.</p>
<p>In programming languages like <a href="https://blog.finxter.com/python-crash-course/" data-type="post" data-id="3951" target="_blank" rel="noreferrer noopener">Python</a>, there are what are known as “primitives”, which are basic data types to work with in your programming. </p>
<p>Those data types are:</p>
<ul>
<li><strong>Strings</strong> – just plain ol’ text, like <code>"Hello World"</code></li>
<li><strong>Integers</strong> – whole numbers, like <code>42</code></li>
<li><strong>Floats</strong> – numbers with decimals, like <code>3.14</code></li>
<li><strong>Booleans</strong> – choices between two options, like <code>True</code> or <code>False</code></li>
</ul>
<h2>Loops and Indentation</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="768" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-288-1024x768.png" alt="" class="wp-image-852431" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-288-1024x768.png 1024w, https://blog.finxter.com/wp-content/uplo...00x225.png 300w, https://blog.finxter.com/wp-content/uplo...68x576.png 768w, https://blog.finxter.com/wp-content/uplo...ge-288.png 1246w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Now you’re going to write your first loop, which is simply a term that refers to a set of instructions to be repeated. </p>
<p>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. </p>
<p>It only knows what you tell it, so the way we give instructions to the Python interpreter is by grouping those instructions with <a href="https://blog.finxter.com/indentationerror-unindent-does-not-match-any-outer-indentation-level-how-to-fix-this-crazy-bug-in-python/" data-type="post" data-id="25874" target="_blank" rel="noreferrer noopener">indentations</a>. </p>
<p>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”.</p>
<p>There are two primary loop types, but today, we are only going to focus on what is called a “<code>for</code> loop”, which runs a defined number of times. </p>
<p>First, let’s start a new program by clicking the <code>new file</code> icon (the white piece of paper <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4c3.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />) then writing some code in the editor like so:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print("Loop starting!")
for i in range(10):
</pre>
<p>Then hit the <code>Enter</code> (PC) or <code>Return</code> (Mac) key. </p>
<p>What you’re doing is assigning a variable, <code>i</code>, for the loop to use as a counter and telling it that <code>i</code> will be every number in the <a href="https://blog.finxter.com/python-range-function/" data-type="post" data-id="18290" target="_blank" rel="noreferrer noopener">range</a> of 10 as the loop is performed. </p>
<p>In other words, the instructions will be repeated 10 times, as defined by the range. </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Info</strong>: 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 <em>i</em> will be the numbers 0-9 in this example.</p>
<p>The other thing to notice here is the colon <code>:</code> at the end of the line. </p>
<p>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>Enter</code> or <code>Return</code> key, not only will a new line be created, but it will also automatically start at an indented 4 spaces in.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="406" height="284" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-283.png" alt="" class="wp-image-852419" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-283.png 406w, https://blog.finxter.com/wp-content/uplo...00x210.png 300w" sizes="(max-width: 406px) 100vw, 406px" /></figure>
</div>
<p>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. </p>
<p>However, this time, we’re going to print both a string and whatever number <code>i</code> happens to be for each repetition of the loop. </p>
<p>So to do that, type </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print("Loop number", i)</pre>
<p>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.</p>
<p>After that, hit <code>Enter</code> or <code>Return</code> for a new line, but this time, hit the <code>backspace</code> key. </p>
<p>We don’t want Thonny to repeat the next instructions, so we need to make sure they are <em>outside</em> of the loop. </p>
<p>Then type </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print("Loop ended!")</pre>
<p>Your whole program will look like this:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="310" height="171" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-284.png" alt="" class="wp-image-852423" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-284.png 310w, https://blog.finxter.com/wp-content/uplo...00x165.png 300w" sizes="(max-width: 310px) 100vw, 310px" /></figure>
</div>
<p>Here for copy&amp;paste:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print("Loop starting!")
for i in range(10): print("Loop number", i)
print("Loop ended!")</pre>
<p>Now go to the toolbar and click the <code>save</code> button. We’re going to call this “<code>Indentation.py</code>”. Once saved, click the green <code>Run</code> button, and you’ll see the results:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="942" height="712" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-285.png" alt="" class="wp-image-852425" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-285.png 942w, https://blog.finxter.com/wp-content/uplo...00x227.png 300w, https://blog.finxter.com/wp-content/uplo...68x580.png 768w" sizes="(max-width: 942px) 100vw, 942px" /></figure>
</div>
<p>Ok, I think that’s enough for today. Try changing the range from 10 by adding a start and stop number like this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i in range(x, y):</pre>
<p>With <code>x</code> and <code>y</code> being whatever numbers you choose. </p>
<p>For example, if you use <code><a href="https://blog.finxter.com/python-range-function/" data-type="post" data-id="18290" target="_blank" rel="noreferrer noopener">range(3, 14)</a></code>, <code>i</code> 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 <code>range(1, 11)</code> and so on.</p>
<h2>Thanks for Reading!</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="683" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-289-1024x683.png" alt="" class="wp-image-852436" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-289-1024x683.png 1024w, https://blog.finxter.com/wp-content/uplo...00x200.png 300w, https://blog.finxter.com/wp-content/uplo...68x512.png 768w, https://blog.finxter.com/wp-content/uplo...ge-289.png 1401w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>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!</p>
</div>


https://www.sickgaming.net/blog/2022/10/...beginners/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016