What is Python?

Python is the official programming language of the Raspberry Pi. It is known for its two unique characteristics: cross-platform compatibility and easy-to-understand syntax. Unlike lower-level languages like C and C++, Python uses a Python interpreter that translates the Python program into machine-specific instructions. So as long as you have a Python interpreter on your computer, you can run any Python program written on whatever machine. Moreover, compared to other languages, the syntax is simple. Syntax is the arrangement of symbols, words, and phrases needed to make a Python program work. The friendly Python syntax makes programming fun, which is perfect for a credit card computer made to acquaint people in computer science.

Some applications of Python:

  • Web Development
  • Game Development
  • Designing Graphical User Interfaces (GUIs)
  • Process Automation
  • Machine learning and Artificial Intelligence

With a Raspberry Pi, a set of computer peripherals, and some hardware components, you can make projects for any of the applications listed above. The sky is the limit! Just one thing you need to know though…

Python Versions

Currently, there are two versions of Python: Python 2 and Python 3. However, Python 2 already retired on January 1, 2020. That means it is no longer supported and will not be maintained by Python developers. Thus, the best way to start Python programming is to learn Python 3. Here are the major differences between the two versions:

  • Python v3 is now based on Unicode. Unicode is an international computer standard in encoding, representing, and handling digital text. On the other hand, Python 2 is based on ASCII, which only handles English characters. Unicode supports non-English characters.
  • Python 3 is more compact than Python 2. Basically, it is easier to learn.
  • Python 3 is being developed with the mindset of longevity. This means learning Python 3 is a good investment and you don’t need to worry about shifting gears for Python 4 in the near future.

In this tutorial, we are going to focus on using Python 3.

Raspbian and Python

The current Raspberry Pi OS version has both Python 2 and Python 3 by default. This means the following Python components are already pre-loaded:

  • An interpreter
  • Python shell
  • An IDE
  • Text editors

No need to install other software unless you prefer another. So how do we get started with writing Python using the Raspberry Pi?

Writing a Python Program on a Terminal

There are two modes in which you can write a Python program on a terminal: interactive mode and script mode. In the interactive mode, you write a line, you press enter, and you immediately get the result. This is because you are given direct access to the Python shell. On the contrary, script mode requires you to create a python file before seeing the output of your program.

To start the Python 3 shell using the Terminal, simply enter python3.

Figure 1: Terminal Interactive Mode

The terminal should return useful details of your Python 3 interpreter, including the exact version installed on your Raspberry Pi. To test interactive mode, let us do the canonical Hello World program. Enter print("Hello World"). Also, make sure you use straight braces, or your program won’t work!

Figure 2: Terminal Interactive Hello World

Unlike the other compiled languages, you can do simple mathematics as is with Python. No need to use variables!

Figure 3: Terminal Interactive Math

To exit the Python shell in the terminal, simply enter exit() or quit(). Alternatively, you can also use the shortcut CTRL+Z to leave the shell.

Figure 4: Terminal Interactive Exit

In script mode, I suggest locating the active directory first before creating your Python file. To do that, enter pwd.

Figure 5: Terminal Script Find Directory

Then, to create the file, enter touch "your file name.py". I named my file myprogram.py so I expect to see it in the file explorer with the exact name and file extension.

Figure 6: Terminal Script Create Python File

And there it is! Just like clockwork.

Figure 7: Terminal Script File Explorer

Open the Python file and input your program commands. Then, save.

Figure 8: Terminal Script Hello World

Finally, to run the program, simply enter python3 "your file name.py".

Figure 9: Terminal Script Run

Writing a Python Program in Thonny IDE

If the terminal setup is not to your liking, you can use an IDE. IDE or Integrated Development Environment is a software application made to make programming easier. IDEs provide comprehensive tools and features for programming in a single graphical user interface. The Raspberry Pi has tons of IDEs, and you can easily access them via the start button.

In this tutorial, we are going to use Thonny IDE. To write the Hello World program in Thonny, simply enter the commands in the code editor.

Figure 10: Thonny IDE Script

Save it to anywhere you like. No need to locate active directory!

Figure 11: Thonny Script Save

Then press the green run button.

Figure 12: Thonny Script Run

You’ll see the output appear below. That’s the integrated python shell. That’s the same shell you initialized in Figure 1.

Figure 13: Thonny Script Run

You can use the shell for interactive mode as well for quick checking of Python lines.

Figure 14: Thonny Interactive

Thanks for reading and be sure to leave a comment below if you have any questions!