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 that is made to acquaint people in the field of computer science.
Some of the applications of Python are:
- 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…
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 no longer 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 as 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?
Programming using the 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. Contrarily, 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
.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-163758_1824x984_scrot-1.png)
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!
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-160938_1824x984_scrot.png)
Unlike the other compiled languages, you can do simple mathematics as is with Python. No need to use variables!
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-160952_1824x984_scrot.png)
To exit the Python shell in the terminal, simply enter exit()
or quit()
. Alternatively, you can also use the shortcut CTRL+Z to exit the shell.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161002_1824x984_scrot-1.png)
In script mode, i suggest locating the active directory first before creating your Python file. To do that, enter pwd
.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161058_1824x984_scrot.png)
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.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161133_1824x984_scrot.png)
And there it is! Just like clockwork.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161200_1824x984_scrot-1.png)
Open the Python file and input your program commands. Then, save.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161441_1824x984_scrot.png)
Finally, to run the program isimply enter python3 "your file name.py"
.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161459_1824x984_scrot.png)
Programming using an 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.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161621_1824x984_scrot-1024x758.png)
Save it to anywhere you like. No need to locate active directory!
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161636_1824x984_scrot.png)
Then press the green run button.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161644_1824x984_scrot.png)
You’ll see the output appear below. That’s the integrated python shell. That’s the same shell you initialized in Figure 1.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161653_1824x984_scrot.png)
You can use the shell for interactive mode as well for quick checking of Python lines.
![](https://www.circuitbasics.com/wp-content/uploads/2020/07/2020-07-16-161802_1824x984_scrot-1.png)