Getting Started with Raspberry Pi Zero
上QQ阅读APP看书,第一时间看更新

Basic programming constructs on Raspberry Pi Zero

Now that you know how to enter and run a simple Python program on Raspberry Pi Zero, let's look at some more complex programming tools. Specifically, we'll cover what to do when we want to determine the instructions to execute and how to loop our code to do that more than once. I'll give a brief introduction on how to use libraries in the Python version 2.7 code and how to organize statements into functions. Finally, we'll very briefly cover object-oriented code organization.

Note

Indentation in Python is very important; it will specify which group of statements is associated with a given loop or decision set, so watch your indentation carefully.

The if statement

As you have seen in previous examples, your programs normally start by executing the first line of code and then continue with the following lines until the program runs out of code. But, what if you want to decide between two different courses of action? We can do this in Python using an if statement. The following screenshot shows some example code:

The following are the details of the code shown in the previous screenshot:

  • #!/usr/bin/python: This is included so that you can make your program executable.
  • a = input("Input value: "): The data will be input by the user and stored in a. b = input("Input second value: "): This data will also be input by the user and stored in b.
  • if a > b:: This is an if statement. The expression evaluated in this case is a > b. If it is True, the program will execute the next one or more statements that are indented; in this case, c = a - b. If not, it will skip that statement.
  • else::The else statement is an optional part of the command. If the expression in the if statement is evaluated as False, the indented statements after the else: statement will be executed; in this case, c = b - a.
  • print c: Another basic purpose of our program is to print out results. The print command prints out the value of c.

You can run the previous program a couple of times, checking both the True and False possibilities of the if expression, as shown in the following screenshot:

The while statement

Another useful construct is the while construct; it allows us to execute a set of statements over and over again until a specific condition has been met. The following screenshot shows a set of code that uses this construct:

The following are the details of the code shown in the previous screenshot:

  • #!/usr/bin/python: This is included so you can make your program executable.
  • a = 0: This line sets the value of variable a to 0. We'll need this only to make sure that we execute the loop at least once.
  • b = 1: This line sets the value of the variable b to 1. We'll need this only to make sure that we execute the loop at least once.
  • while a != b:: The expression a != b (in this case, != means not equal to) is verified. If it is True, the indented statements are executed. When the statement is evaluated as False, the program jumps to the statements (none in this example) after the indented section.
  • a = input("Input value: "): The data will be input by the user and stored in a.
  • b = input("Input second value: "): This data will also be input by the user and stored in b.
  • c = a + b: The variable c is loaded with the sum of a and b.
  • print c: The print command prints out the value of c.

Now you can run the program. Note that when you enter the same value for a and b, the program stops, as shown in the following screenshot:

Working with functions

The next concept that we need to cover is how to put a set of statements into a function. We use functions to organize code and group sets of statements together when it makes sense to organize them in the same location. For example, if we have a specific calculation that we might want to perform many times, instead of copying the set of statements every time we want to perform it, we group them into a function. I'll use a fairly simple example here, but if the calculation takes a significant number of programming statements, you can see how that would make our code significantly easier to maintain, as we don't need to duplicate our code over and over. It can also make our code easier to read. The following screenshot shows the code:

The following is the explanation of the code from our previous example:

  • #!/usr/bin/python: This is included so you can make your program executable.
  • def sum(a, b):: This line defines a function named sum. The sum function takes a and b as arguments.
  • c = a + b: Whenever this function is called, it will add the values in the variable a to the values in variable b.
  • return c: When the function is executed, it will return variable c to the calling expression.
  • d = input("Input value: "): This data will also be input by the user and will be stored in d. The prompt Input second value: will be shown to the user.
  • e = input("Input second value: "):This data will also be input by the user and stored in e. The prompt Input second value: will be shown to the user.
  • f = sum(d, e): The function sum is called. The program then goes to the sum function and executes it. The value in variable d is copied to the variable a and the value in the variable e is copied to the variable b. The value is returned from the function and then stored in variable f.
  • print f: The print command prints out the value of f.

The following screenshot is the result received when you run the code:

Libraries/modules in Python

The next topic we need to cover is how to add functionality to our programs using libraries/modules. Libraries or modules, as they are sometimes called in Python, include a functionality that someone else has created and that you want to add to your code. As long as the functionality exists and your system knows about it, you can include the library in the code.

So, let's modify our code again by adding the library, as shown in the following screenshot:

The following is a line-by-line description of the code:

  • #!/usr/bin/python: This is included so that you can make your program executable.
  • import time: This includes the time library. The time library includes a function that allows you to pause for a specified number of seconds.
  • d = input("Input value: "): This data will be input by the user and will be stored in d. The prompt Input second value: will be shown to the user.
  • time.sleep(2): This line calls the sleep function in the time library, which will cause a 2 second delay.
  • e = input("Input second value: "): This data will also be input by the user and will be stored in b. The prompt Input second value: will be shown to the user.
  • f = d + e: The f variable is loaded with the value d + e.
  • print f: The print command prints out the value of f.

The following screenshot shows the result after running the previous example code:

Of course, this looks very similar to other results. But you will notice a pause between you entering the first value and the appearance of the second value.