Python On Raspberry Pi – Part 7

In the previous article, ‘Raspberry PI: laying out the basics I’ we mentioned the Raspberry PI as a low-cost computing device that was initially created to help students improve their programming skills, as well as raise interest in computer science.

Thus the need for a beginner-friendly and widely used programming language arose. The Python programming language was the answer. This is because python is user-friendly, dynamic, portable and easy to learn.

Python was created by Guido Van Rossum to be used as a language for teaching elementary programming in schools and colleges. Surprisingly this very ambitious plan laid out more than a decade ago is being realized fully through the invention of the Raspberry Pi. The Raspberry comes pre-installed with two versions of python, python 2 and python 3. Most people will prefer to run python 2. This is due to the slow adoption of user-contributed packages. Python 3 also lacks backward compatibility which affects its portability.

The environment on which you run python on the Raspberry PI is called an IDLE (Integrated development for learning environment). Python is an interpreted language. This means it is executed directly without the need for compiling. An added advantage for this is faster execution by the interpreter.

Raspberry “Hello world” In Python

To learn Python, we should kick the ball running into the IDLE. (There are three ways to start, either of IDLE, IDLE3, pygames).

To run a Hello World program in python, We have to open the IDLE. It’s simple, just click on Menu -programming- Python3.

A new window, python 3.6.2 will pop up, just type print (“Hello World”) and you will have created your first python program in the Raspberry Pi. What you have done is run a python script on a python shell, similar to running a program on the terminal(command line). This is called interactive Mode and basically, it is used to test for bugs in a program(debugging) or doing simple math.

We can also create our Hello World program another way. At the top of your IDLE, go to – files -> new file on your editor, which will pop up next, input the following –

string1 = ‘Hello’
string2 = ‘world’
print(string1 + string2)

Press ctrl+s to save your file. Save it as Hello, then include a python extension py. You will have your filename as Hello.py. After saving your program just click F5 and your program will display on the shell. This mode of running programming other than from the shell enables you to run the individual application, whereas the interactive mode will only allow you to run standalone scripts.

Components of a python program

Now that you are up and running with how to run your python program, let’s take a skimmed taste of what really composes a complete Python program.

python programming in raspberry pi

Illustration 1: The above code written in python, it is used to check whether a string literal(word) is an isogram.​A typical program in python consists of;

1. data types

There are five data types in python. These data types while being used in python are stored in computer memory as variables.

2. Loops

Loops are conditional sequences that have to be followed to control the structure of a program. This helps eliminate clumsy repetition and keep a python program simple and concise.​For example –

running loop in programming

The above program is long and with a lot of irrelevant repetition. A good programmer would use a loop to eliminate the repetition. This is how a loop will appear –

while loop in python

Illustration 2: In this program, we have eliminated the repetition with a loop.Types of looping include –
The while loop – This loop allows you to loop statements.
The for loop – This loop allows you to loop a list.  

3. Logical Operators

These are logics used to link two conditions of a loop. And which means both conditions of the loop are true Or which shows that either of conditions is true.

Not which checks whether the conditions pass as true.

For example, you want to check whether a statement is true. If it’s true the computer prints your statement is true, if the statement is false, the computer prints nothing. Logical operators, therefore, go hand in hand with while and if statements (if/while the statement is true, do something, if it is not [elif] do something else).

Below is a program that checks, whether a word inputted is a string and that the string/word contains no duplicated characters by using conditional looping. This program returns True or False. This is called conditional branching.

5. Functions

Think of a function as a block of code you will write once when the need arises for you to use the code anywhere else within the program. Python comes with built-in functions. Python will also allow you to build your own functions. A function you build is called a user-defined function. You build a function by defining it using the def keyword, you name your function and then include parenthesis. Inside the parenthesis, you place the parameters.

​A good example is the following program which utilizes functions to multiply the score of a student over three semesters.

functions in python programming

Conclusion

In later articles, we are going to exploit fully the power of python. This article was only meant to give you an inside glimpse of Python and Raspberry Pi. But still based on this is pygames essentials, a very important part of python in Raspberry PI. The length cannot be discussed in this article however but in later articles.

SHARE THIS POST

MassiveGRID Banner

Leave a Reply

Your email address will not be published. Required fields are marked *