Dive Into the Sea of Syntax

Dipping into the Digital Stream

Now that we have Thonny installed, and our Pico and parts are on order, let's take a crack at coding!

Your First Code

If it isn't already running, start up your Thonny IDE.

Traditionally, every programmer’s first program is the "Hello World" program, which simply prints out the words “Hello World!” using the computer’s available display. It is also a good way to make sure everything is set up and working properly. 

Since this is Python, and to get a feel for the Monty-Python-ish vibe, type the following code into the code window:

print("Wake up Polly Parrot!")


This code statement (a complete line of code) uses the print() function to send text to the screen.

A function is simply a collection of code instructions that “does something”, usually a small task. In this case print() takes whatever is sent to it and prints it out on the screen, usually in the Shell window.

Between the parentheses, we insert whatever it is we want the function to print, in this case we send it a string of characters (or as it is sometimes called a string literal) that spell out Wake up Polly Parrot!. By wrapping our string of characters in quote marks, Python understands that this is a string of characters that it should print out. We will find out more about functions in a bit.

Most functions work with data that is sent to them using parameters, the bits we include between the parentheses. Not all functions require parameters. We'll learn more about this as we use, and build functions.

For now, once you have the print statement typed in, click on the RUN button in the tool bar. The RUN button is the green circle with white triangle inside (like the typical "Play" button for music and movie players).

This causes our program to run, or "execute".


Give it a try.

Screen shot of Thonny with print function written in code.

Notice that Thonny provides line numbers along the left margin of the code window. These will come in handy in referencing your code.


Adventure Tip: Data Types

Computer programs mainly work with information or data in various different ways that make sense for the type of data.

For example, computers can add two numbers together to get a sum:

Adding integer numbers: 1 + 2 = 3

Adding "floating point" (a.k.a. decimal) numbers: 3 + 0.14159 = 3.14159

Computers can also "add" two strings of characters together to get a combined, or "concatenated", string:

"So " + "there!" = "So there!"

Concatenation is a five dollar word that essentially means to connect together in a series, like a chain. It tends to come up a lot in coding.

These are a few examples of different data types: str (for string), int (for integers) and float (for floating point). Keep an eye out as we will run into more data types along the journey.

Data Types, Baked FRESH Daily!


The Results of our Statement

If you typed the code into Thonny's code window and are seeing Wake up Polly Parrot! in the Shell...

Congratulations!

You have written and executed your first computer program! You are officially a coder!

 If instead you saw something like this, with a line that says “SyntaxError:” on it:

Congratulations!

You have written your first software bug! You are officially a coder!



Debugging: Puzzles in Disguise

Things don't always go as planned. And errors, or "bugs", are a normal part of coding. While they can be frustrating when they come up, they can also be rewarding in that they are a chance to learn more about coding.

It helps to have some debugging tips though, and Thonny comes to the rescue!



Troubleshooter Tips: Code Hints

You may have noticed as you typed your code in that Thonny is very colorful, offering clues to your coding.

  • Keywords (Python commands) are colored purple when correctly typed in.
  • Background highlighting shows code that Thonny thinks is not yet completely typed in...something is missing
    • Green is for strings: text such as our wake up call for Polly. It is highlighted because the closing quote mark is missing. It should match the opening quote mark already typed.
    • Gray hints that the parameter list (the items between the parentheses) that we want to send to our print() function isn't complete. This time it is because the closing parenthesis is missing).

As we type in more of our code, Thonny immediately responds:

  • Adding the closing quote mark changed the text to green, Thonny's way of saying our string now looks correctly formatted (it has both opening and closing quotes)
  • They gray highlighting is still there because the closing parenthesis is still missing.

Once we have all of these syntax issues fixed, Thonny removes the highlighting:

  • Our keyword is purple
  • The parentheses are blue showing that they are correctly matched (both the opening "(" and closing ")" parentheses are present and accounted for.
  • The string is green, showing it is correctly formatted.


Troubleshooter Tips: Help in the Windows

The Shell and Assistant

When we run or execute code with errors, Thonny responds by giving us information to help track down the issue, displaying it in window panes in the Thonny IDE:

  • The Shell window pane (below the code window pane) points out where the problem occurred and how to find it. It tells us the line number where it found the error.
  • The Assistant window pane (shown here on the right side of the Thonny IDE) tells you a bit more about the error it found, and links to some help information.

Thonny is a great IDE for learning how to code. Helpful information is always close at hand.



Adventure Tip:

If you don't see either the Shell or the Assistant windows, remember that you can always activate them by going to Thonny's main menu, selecting the View menu, find the window you're looking for in the list and selecting it.

Sometimes we write this as a short-hand path such as: View > Assistant to find the Assistant window in the View menu in Thonny's main menu..

A couple more tips...

We haven't saved our file yet (we will soon). Thonny noticed that we have made changes to our code and haven't yet saved them.

One tip off is that the label on the tab in our coding window pane reads <untitled>, meaning it hasn't yet been saved with a filename.

The other tip is that there is an asterisk ( * ) next to where the filename should be. Thonny is indicating that there were changes to our code that have not yet been saved. Once we save our code to a file, the asterisk disappears until we make another change to our code.


Fun Fact:The Parrot Sketch

One of Monty Python's most famous sketches is the Parrot Sketch. Check it out on YouTube.




Adventure Recap

In this leg of the adventure, you have officially become a coder! Discoveries include:

  • Statements and functions, including the built-in print() function
  • What a string literal is
  • What data types are including string, integer, and floating point data types to start with
  • How to run your first program (or create your first bug) and see the output in Thonny's Shell
  • What a syntax error is
  • Some troubleshooting tips toward managing and fixing bugs
  • Where you can find the Shell, Assistant and other useful tools in the View menu

Complete and Continue  
Discussion

0 comments