First Python Program on the Raspberry Pi

In this tutorial, I will tell you how to write and run your first Python program on Raspberry PI. In the process, you’ll understand what python programs are, what the applications of Python programming are, how to write python programs on Raspberry PI and how to run those Python programs.

As I mentioned earlier in raspberry Pi tutorial (Raspberry Pi without monitor and keyboard), the main reason behind developing Raspberry Pi is to encourage the learning of computer programming and Python is one of those few programming languages that Raspberry Pi quite Has given high priority.

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 is simple for syntax reading and writing program programmers in python because they need to write a few lines of code 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 because its syntax is very clean with the readability and it  uses  simple English keywords.

Originally developed as the scripting language for Linux, Python soon became a main stream programming language. Unlike other programming languages such as C or Java, Python programs don’t need compilers, but they require python interpreters to read and execute.

Applications of Python Programs

Like any other programming language, Python can also be used along the command line, or along the command line using interactive programming environments such as Python’s REPL (Read – Eval – Print Loop).

Python can also be used as a scripting language to automate various functions and python can also be used in the following:

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

Applications of Raspberry Pi and Python

With a combination of Raspberry PI and Python you can do a lot of things. Some 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 on Raspberry Pi

To install Python 2 or Python 3 on Raspibian or any other Linux based operating system, you need to enter the following commands into the terminal.

To Install Python2  Use the following command and hit enter.

 
sudo apt-get install python

To Install Python3  Use the following command and hit enter.

 
sudo apt-get install python3

NOTE:  In Raspbian OS the Python will  come pre-installed . You can use following command to update to latest version of Python.

There are several 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 a Python IDE called Passive (for both Python 2 and Python 3). First we’ll see how to use Python from the terminal (using the command line and REPL) and then we’ll see how to launch Python Idle from the Raspbian desktop.

Python REPL (Read – Eval – Print Loop)

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

For Python2, Press Enter

python

For Python3, Press Enter

python3

Now you will enter interactive mode interpreter with the primary quick wait to enter the user’s prompts. The primary signal is usually 3 more than the signal (>>>).

The Python REPL, you can enter commands directly. For example, you can use it as a calculator by typing 2+3 and when you enter hits, you will get results directly.

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

To Exit from REPL use Ctrl+d.

Writing a Python Program

For our first program, we’ll see how ‘Hello, World’ text is printed using Python. We will use Python 3 in this example and the code is specific to Python 3 and can cause errors in Python.

To start typing the program, first type the following command and enter the hits.

 
sudo nano helloworld.py

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

Edit
print (“Hello, World”)

To Save this file use command ctrl + x and then y. notice that the file name ‘helloworld’ ends with an extension .py. It is very important to add this extension to specify it as a Python file.

pic

Congratulation you have successfully written your first Python program and saved it.

Running the Python Program

To run the Python program that we just created and saved, type the following command and enter hits

 
python3 helloworld.py

Leave a Reply

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