This project has been excerpted and modified from Make: Getting Started with Raspberry Pi – 2nd Edition.

What You’ll Be Making

You’ll create a very simple counting program using Python. Python is considered an “interpreted language,” which means that you can write a program or script and execute it directly rather than compiling it into machine code. Interpreted languages are a bit quicker to program with, and Python comes with a few side benefits of its own. For example, in Python you don’t have to explicitly tell the computer whether a variable is a number, a list, or a string; the interpreter figures out the data types when you execute the script.

The Python interpreter can be run in two ways: as an interactive shell to execute individual commands, or as a command-line program to execute standalone scripts. The integrated development environment (IDE) bundled with Python and the Raspberry Pi is called IDLE. You’ll learn the basics of Python through this mini project.

If you aren’t familiar with what a shell is, it is a user interface for accessing an operating system’s services. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer’s role and particular operation.

What You’ll Learn

  • The basics of Python, a programming language
  • How to put chunks of code into functions for easy re-use
  • Working with variables and loops

Project Steps

Getting Started with IDLE

The best way to start learning Python is to jump right in. Although you can use any text editor to start scripting, we’ll start out using the IDE. Open up the IDLE 3 application. To run IDLE, double-click the IDLE 3 icon on the desktop, or click the desktop menu in the lower left, and choose Programming→IDLE 3.

IDLE can take several seconds to start up, but when it appears, you’ll see a window with the interactive shell (Image on left). The triple chevron (>>>) is the interactive prompt; when you see the prompt, it means the interpreter is waiting for your commands.

To start a new Python program, select File→New Window, and IDLE will give you a script editing window (Image on right).

Write Your Program

If you’re coming to Python from the Arduino world, you’re used to writing programs (known as sketches in Arduino, but often called scripts in Python) in a setup/loop format, where setup() is a function run once and loop() is a function that executes over and over. The following example shows how to achieve this in Python.

With the script editing window you opened in the previous step, type the code in the first image. It may not be obvious that each level of indentation is four spaces, not a tab (but you can press tab in IDLE and it will dutifully insert spaces for you). It’s important to watch your whitespace. Python is a highly structured language where the whitespace determines the structure. This differs from languages like C that delimit blocks of code with brackets or other markers.

To run your program, select Run Module and give your script a name (such as EvenIntegers.py). As it runs, you should see all even integers printed (press Ctrl-C to interrupt the output, because it will go on forever!).

You can change this program to create a loop that just counts the first 100 even integers. Add the code in Image 2 to the bottom of your code.

Using Functions to Create Reusable Chunks of Code

Now, we’re going to use functions to put chunks of code into a code block that can be invoked from other places in your script. To rewrite your program with functions, type the code in Image 1.

Note: Everything indented one level below the loop() function is considered part of that function. The end of a loop is determined by where the indentation moves up a level (or end of the file).

Run your code and save it as CountEvens.py.

Based on the code you’ve written, the output will be every even number from 102 on. Here’s how it works (reference the numbers above).

  1. The variable n is defined as a global variable that can be used in any block in the script.
  2. The setup() function is defined (but not yet executed).
  3. Similarly, the loop() function is defined.
  4. In the main code block, the setup() function is called once, then the loop() function is called.

The use of the global keyword in the first line of each function is important; it tells the interpreter to use the global variable n rather than create a second n variable that can only be used in the function.

Learn More About Python

To learn more about coding in Python, refer to Make: Getting Started with Raspberry Pi — 2nd Edition. Chapter 4, “Python on Pi” contains the project you just completed as well as additional information and more mini-projects. Happy coding!