Basic programming constructs on Raspberry Pi

Now that you know how to enter and run a simple Python program on Raspberry Pi, let's look at some more complex programming tools. Specifically, we'll cover what to do when we want to determine what instructions to execute and show 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 are 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 next lines until your program runs out of code. This is fine; 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 if statement

The following is the detail of the code shown in the previous screenshot, line by line:

  • #!/usr/bin/python – This is included so you can make your program executable.
  • a = input("Input value: ") – One of the basic needs of a program is to get input from the user. raw_input allows us to do that. The data will be input by the user and stored in a. The prompt Input value: will be shown to the user.
  • b = input("Input second value: ") – This data will also be input by the user and stored in b. The prompt Input second value: will be shown to the user.
  • 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 statement(s) that is indented. If not, it will skip that statement(s); in this case, c = a - b.
  • else: – The else statement is an optional part of the command. If the expression in the if statement is evaluated as false, the indented statement(s) 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 if statement

The while statement

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

The while statement

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 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 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 statement(s) that is indented is executed. When the statement is evaluated as false, the program jumps to the statements after the indented section.
  • a = input("Input value: ") – One of the basic purposes of a program is to get input from the user. raw_input allows us to do that. The data will be input by the user and stored in a. The prompt Input value: will be shown to the user.
  • b = input("Input second value: ") – This data will also be input by the user and stored in b. The prompt Input second value: will be shown to the user.
  • 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. Notice that when you enter the same value for a and b, the program stops, as shown in the following screenshot:

The while statement

Working with functions

The next concept we need to cover is how to put a set of statements into a function. We use functions to organize code, grouping sets of statements together when it makes sense that they be organized and 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 each 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 more efficient. The following screenshot shows the code:

Working with functions

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 the variable c to the calling expression.
  • if __name__=="__main__": – In this particular case, you don't want your program to start executing each statement from the top of the file; you would rather it started at a particular point. This line tells the program to begin its execution at this particular point.
  • d = input("Input value: ") – This data will also be input by the user and stored in b. 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 b. The prompt Input second value: will be shown to the user.
  • f = sum(d, e)– The function sum is called. In the sum function, 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 program then goes to the sum function and executes it. The return value is then stored in the 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:

Working with functions

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 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:

Libraries/modules in Python

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

  • #!/usr/bin/python – This is included so 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.
  • if __name__=="__main__": – In this particular case, you don't want your program to start executing each statement from the top of the file; you would rather it started from a particular line. This line tells the program to begin its execution at this specified point.
  • d = input("Input value: ") – This data will also be input by the user and stored in b. 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 two-second delay.
  • e = input("Input second value: ") – This data will also be input by the user and 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:

Libraries/modules in Python

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

The object-oriented code

The final topic we need to cover is object-oriented organization in our code. In object-oriented code, we organize a set of related functions in an object. If, for example, we have a set of functions that are all related, we can place them in the same class and then call them by associating them with a specified class. This is a complex and difficult topic, but let me just show you a simple example in the following screenshot:

The object-oriented code

The following is an explanation of the code in the previous screenshot:

  • #!/usr/bin/python – This is included so you can make your program executable.
  • class ExampleClass(object): – This defines a class named ExampleClass. This class can have any number of functions associated with it.
  • def add(self, a, b): – This defines the function add as part of ExampleClass. We can have functions that have the same names as long as they belong to different classes. This function takes two arguments: a and b.
  • c = a + b – This statement indicates the simple addition of two values.
  • return c – This function returns the result of the addition.
  • if __name__=="__main__": – In this particular case, you don't want your program to start executing each statement from the top of the file; you would rather it started from a specific line. This line tells the program to begin its execution at the specified point.
  • example = ExampleClass() – This defines a variable named example, the type of which is ExampleClass. This variable now has access to all the functions and variables associated with the class ExampleClass.
  • d = input("Input value: ") – This data will also be input by the user and stored in the variable b. 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 b. The prompt Input second value: will be shown to the user.
  • f = example.add(d,e) – The instance of ExampleClass is called and its function add is executed by sending d and e to that function. The result is returned and stored in f.
  • print f – The print command prints out the value of the variable f.

The result after running the previous example code is as shown in the following screenshot:

The object-oriented code

The result shown in the previous screenshot is the same as the other codes discussed earlier, and there is no functionality difference; however, object-oriented techniques have been used to keep similar functions organized together to make the code easier to maintain. This also makes it easier for others to use your code.

Now that you have a feel for the basics of Python coding, I'll introduce you very briefly to the C coding language.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.122.46