Wednesday 19 July 2017

First Python Program on the Raspberry Pi

In this tutorial, I’ll show you how to write and run your first Python Program on Raspberry Pi. In the process, you will understand what Python Program is, what the applications of Python Programming are, how to write Python Programs on Raspberry Pi and how to run those Python Programs.

As I have mentioned in the first Raspberry Pi tutorial (Raspberry Pi without monitor and keyboard), the main reason behind developing Raspberry Pi is to encourage learning of computer programming and Python is one of those few programming languages that Raspberry Pi has given a much higher priority.

Raspberry Pi and Python
What is Python?

Python is a powerful, very useful and one of the most popular programming languages in the World.

Python is very easy to use i.e. it has an easy to read syntax and writing programs in Python is simple for programmers as they need to write few lines of code when compared to other popular programming languages like C, C++ or Java.

Python is one of the highly recommended programming languages for people who are new to coding as its syntax is very clean with its importance to readability and usage of simple English Keywords.

Originally developed as a Scripting Language for Linux, Python soon became a main stream programming language. Unlike other programming languages like C or Java, Python Programs doesn’t need a compiler but requires a Python Interpreter to read and execute them.

We will see a simple Python Program in the later sections to print Hello World and compare it with a C Program of similar task.

Applications of Python Programs

Like any other programming language, Python can also be used with command line using Python IDLE (Integrated Development and Learning Environment) or the interactive programming environment like Python’s REPL (Read – Eval – Print Loop).

Python can also be used as a scripting language for automating different tasks. Additionally, Python can also be used in the following:

  • Web and Internet Development
  • Scientific and Numeric Applications
  • Desktop Graphical User Interfaces (GUIs)
  • Educational Applications
  • Software Development
  • Games
  • Databases and many more.

Applications of Raspberry Pi and Python

There are a lot of things you can do with the combination of Raspberry Pi and Python. Some of the popular applications are mentioned below.

  • Learn programming with Python.
  • Raspberry Pi as a Web Server.
  • Raspberry Pi Cluster (Super Computer).
  • Application like Weather Monitoring Stations, Home Automation by interfacing different sensors.
  • Raspberry Pi as Monitoring and Tracking Server.

Installing Python

To install Python2 or Python3 on Raspbian or any other Linux based operating system, you need to enter the following commands in the terminal.

For Python2, type the following command and hit enter

sudo apt-get install python

For Python3 (the latest revision of the Python), type the following command and hit enter

sudo apt-get install python3

NOTE: Python will be pre-installed on the Raspbian OS and you can use the above commands to update it to the latest versions.

Installing Python
There are multiple ways to use Python on your Raspberry Pi. You can use it from the terminal or from an IDE (Integrated Development Environment). An IDE is a combination of a text editor, debugger and a compiler.

Raspbian has Python IDE called IDLE (for both Python2 and Python3). First we will see how to use Python from the Terminal (using command line and REPL) and then we will see how to launch the Python IDLE from Raspbian Desktop.

Python REPL (Read – Eval – Print Loop)

Python REPL is an interactive environment that accepts one command at a time, executes the command and prints the result and repeats the loop. To open Python REPL from the terminal, enter the following command and hit enter.

For Python2, enter

python

For Python3, enter

python3

You will now enter in to an Interactive Mode Interpreter with a Primary Prompt waiting for the user to enter prompts. The Primary Prompt is usually 3 greater than signs (>>>).

Python REPL
In the Python REPL, you can directly enter the commands. For example, you can use it as a calculator by typing 2+3 and when you hit enter, you will directly get the result.

Another example is to print a text and we will be printing the very famous Hello World. For that, all you need to do is to type the following and hit enter.

print (“Hello, World”)

Python REPL Hello World

To exit or come out of the Python REPL, you need to type CTRL+d.

Python Program

In Python REPL, we are directly entering the commands without creating or writing any program. Now, we will see how to write a program, save it and execute it.

Writing a Python Program

For our first program, we will see how to print the text ‘Hello, World’ using Python. We will be using Python3 in this example and the code is specific to Python3 and might give errors in Python.

To start writing the program, first type the following command and hit enter.

sudo nano helloworld.py

This will open the Nano text editor with name of the file being helloworld. In this, type the following lines of code.

#!/usr/bin/python3
print (“Hello, World”)

In order to save this file press CTRL+x and then y. Notice that the file name ‘helloworld’ ends with an extension .py. It is very important to add this extension in order to specify it as a Python File.

Python First Program
You have successfully written your first Python program and saved it. We will now see a bit about the above code.

The first line is #!/usr/bin/python. This is a shebang statement which tells the interpreter to look for programs in the specified path.

The next line is the actual print statement which tells the interpreter to print the text in it.

Running the Python Program

In order to run the Python program which we just created and saved, type the following command and hit enter

python3 helloworld.py

As soon as you hit enter, you will get the output. If your Python Program is in a different location, you need to go to that directory first and then use the above command.

Python Program Run

Making the Python Program Executable

By making the Python Program as an Executable file, we can directly run the program without using python3 (or python) in front of the program file name. In order to make the helloworld.py file as an executable file, type the following command and hit enter.

sudo chmod +x helloworld.py

Now, in order to run this executable file, we need to use the following command.

./helloworld.py

Python Program Executable

Replace the helloworld.py in the above commands with the name you have given to your file. Do not forget the extension.

Python from Raspbian Desktop

As mentioned earlier, the Raspbian OS, which is the Raspberry Pi’s official OS, comes with the tools for both Python2 and Python3. To launch the Python3 IDLE, go to Raspbian Menu – > Programming – > Python3 (IDLE).

Python IDLE

This will launch the Python Shell. This shell can be used as Python REPL and enter commands in interactive mode (as seen in the Terminal).

Python IDLE Shell
To write a program, go to File option in the Python Shell and select New File. A new, blank file will be opened. Here, we can write the programs and save the file (using File – > Save or CTRL+s) in our desired location.

I’ve saved the file as hello.py in the folder PythonProgs on the Desktop. The program written in hello.py is shown below.

#!/usr/bin/python3
name = (‘What is your name? ’)
print (‘Hello,’,name)

Python IDLE New File

To run this program, we need to use the Terminal. You can either execute the program by using the python3 before the file name (python3 hello.py) or make the program file an executable file and run it.

Python IDLE Run Execute

In this tutorial, you have seen how to get started with Python on Raspberry Pi. If you are new to Programming (in general or Python in particular), you can use this setup to learn and practice Python.

If you are familiar with Python, then you can start developing small applications and projects.

The post First Python Program on the Raspberry Pi appeared first on Electronics Hub.

No comments:

Post a Comment

Good vibrations: New tech may lead to smaller, more powerful wireless devices

What if your earbuds could do everything your smartphone can, but better? A new class of synthetic materials could allow for smaller devices...