Python is a high-level, interpreted, general-purpose programming language. It was created by Guido van Rossum and first released in 1991.
Python is an excellent choice for those who are starting to learn programming, as well as for experienced programmers looking for an efficient and powerful language.
Python is characterized by:
- Simplicity and Readability: Its clear and concise syntax makes the code easy to read and write.
- Versatility: Python can be used for a variety of applications, from web development to data analysis and automation.
- Large Community and Ecosystem: It has a wide range of libraries and modules that facilitate the development of complex projects.
- Compatibility with Raspberry Pi: Python is the default programming language for many activities on Raspberry Pi due to its ease of use and the wide availability of resources.
- Hardware Compatibility: Python has specific libraries such as
RPi.GPIO
andgpiozero
that simplify interaction with the GPIO pins of Raspberry Pi.
If you need more information about Python, here is a link to the Python course 👇
Installing Python on Raspberry Pi
The Raspberry Pi generally comes with Python pre-installed. But you may be running an image without it, or you may not have the latest version.
In any case, let’s go through the steps to ensure you have Python set up correctly:
Verifying the Installation
You can verify that Python has been installed correctly by running:
python3 --version
You should see the version of Python 3 that has been installed.
Installing Python
If you did not have Python installed, we can easily install it. First, it is a good practice to ensure your Raspberry Pi is up to date. Run the following commands:
sudo apt update
sudo apt upgrade
Next, we install python3
, which is the most recent and recommended version of Python, using the following command:
sudo apt install python3
Programming in Python on Raspberry Pi
Now that you have Python installed, you can start programming. Open a text editor like nano
, vim
, or an IDE like Thonny (which comes pre-installed on Raspberry Pi OS) and start writing your Python code.
Let’s go with a simple example. Open a terminal and create a new Python file:
nano hello.py
Write the following code,
print("Hello, Raspberry Pi!")
Save the file and exit the editor (in nano
, press CTRL + X
, then Y
to confirm and Enter
to save).
Finally, run the script:
python3 hello.py
You should see the output “Hello, Raspberry Pi!” in the terminal.
Package Management with pip
Pip is a tool that allows you to install additional Python packages and modules. You can use pip to install additional Python modules as per your needs.
For example, to install the requests
module, you can run:
pip3 install requests
Example of Python Usage on Raspberry Pi
To illustrate how you can use Python on your Raspberry Pi, here is an example code that uses the GPIO
module to control an LED connected to your Raspberry Pi:
import RPi.GPIO as GPIO
import time
# Set up the pin mode
GPIO.setmode(GPIO.BCM)
# Set up pin 17 as output
led_pin = 17
GPIO.setup(led_pin, GPIO.OUT)
try:
while True:
# Turn on the LED
GPIO.output(led_pin, GPIO.HIGH)
time.sleep(1)
# Turn off the LED
GPIO.output(led_pin, GPIO.LOW)
time.sleep(1)
except KeyboardInterrupt:
# Stop the program when Ctrl+C is pressed
GPIO.cleanup()
This code turns on and off an LED connected to GPIO pin 17 of your Raspberry Pi in an infinite loop.