Chapter 2. Exploring Python

 

In this chapter, you’ll learn how to interact with your Raspberry Pi by using Python to

  • Do math calculations quickly and easily
  • Store information using variables
  • Get messages to display on the screen
  • Create and run your first program in Python

An exciting part of programming is getting the computer to interact with you. It’s the first step toward having the computer feel artificially intelligent.

Playing with Python

One of the best ways to learn to program is by exploring and playing. When you play, you try things and see what happens. You learn by experiencing the act of programming and seeing results. In this approach, you’ll try entering different commands and see what happens.

Open IDLE for Python 3 by clicking the Menu button and selecting Programming > Python 3 on your Raspberry Pi’s desktop (see figure 2.1). After you click it, you’ll need to wait a few seconds while IDLE opens.

Figure 2.1. The Python 3 icon on your Raspberry Pi opens an interactive programming shell for Python 3.x.
Note

There are both Python 3 and Python 2 icons under Menu > Programming on your desktop. Make sure you click Python 3 and not Python 2.

The Python 3 icon opens IDLE.[1] You’ll see a prompt, ready for your commands—this is the Python Shell (see figure 2.2). With the Python Shell open, let’s see how you can start talking to your Raspberry Pi using Python.

Discovering Python’s mathematical operators

Storing information using variables

Displaying text on a screen

Creating programs

Fruit Picker Extra: creating documents

Challenges

Summary