Learning Python 3 with Raspberry Pi is a great idea. You will be able to write programs producing real physical results, while also learning more about computer science in general, system administration, hardware, etc.
Programming is not only about smashing code into a text editor. To be able to write great programs you also need to have a broader understanding of how things work.
What’s great with Raspberry Pi, is that you can first learn Python without needing to configure anything. And then, you can expand your knowledge thanks to all the Pi functionalities with Python support.
In this post I will not directly teach you Python. Instead, I will give you a global overview and tell you where to start and how to progress faster while keeping your motivation to learn more.
Why learning Python 3 with Raspberry Pi
First, Python is used everywhere: web, desktop software, dev ops, automation, robotics, gaming, data science, etc. The job market for software developers is growing a lot. Learning Python will definitely give you a good start.
Also, Python has a huge community. Which means: a lot of help – tutorials, courses, forums, etc. – from the Internet, many modules ready to be used, vast amount of open source projects, etc.
You are learning how to use Raspberry Pi to build your own projects?
Check out Raspberry Pi For Beginners and learn step by step.
A great door for robotics and many other applications
In this website I focus mostly on programming for robotics. Python is extremely useful as it will allow you to test programs very quickly.
Robotics programming is growing exponentially. And the size of the Raspberry Pi makes it ideal to be embedded in a robot. Thus, Python + robotics + Raspberry Pi is a great combo.
Now, if you start programming robots, you’ll see that 2 languages appear quite often: C++ and Python.
For example, ROS, aka Robot Operating System, is a very powerful “framework” to write robotics applications easily (ROS2 and Raspberry Pi is a great combo), with lots of plugins/libraries to develop faster. ROS is primarily written in C++, but also has a Python binding which makes it easy to use if you already know Python.
And in general, in fields related to robotics, you’ll find many other libraries written in C++ for performance, but with a Python binding for ease of use. OpenCV is another example. This is an image processing library, with which you can do quite advanced stuff, and… there’s a Python interface so you don’t even need to use C++.
Of course, if you go into robotics, in the end learning C++ will become mandatory, but Python is a first good step which will help you open a few doors.
Which Python version to learn on your Raspberry Pi
Python 2 is not supported anymore since 2020. So, as a new learner, just go with Python 3. You don’t need to worry much about the specific version number (ex: 3.6, 3.7, 3.x etc.).
First learn the basics, which are the same for any version. And then, with more experience you’ll be able to choose a particular version that suits the project you’re working on.
What not to focus on at the beginning
When you start learning Python 3 with Raspberry Pi, it’s important that you focus on the right things first. Otherwise you could get lost in an ocean of information and configuration, which will make you want to give up.
So, at the beginning, focus on writing code for applications where you can see a clear result. This means you should use existing tools to make your life easier.
It’s totally OK to skip the configuration part of Python, the package manager, etc. First, learn how to code. Understand the logic behind the code, and finish a few projects.
In the following I’ll give you more details about what to learn first, and what to learn after you have some basics.
Install Raspberry Pi OS
This is the only configuration you’ll need before you can start writing Python code on your Raspberry Pi.
This installation is definitely worth it, because once done, you’ll have everything automatically setup for you to write and execute Python programs.
To install Raspberry Pi OS, you will need a micro SD card. Choose a card with at least those specs:
- Class 10 or more (if you find an “extreme pro” card it’s even better).
- At least 8 GB of disk space. 16 or 32 GB will give you more.
Plug your micro SD card into your computer. Go to the Raspberry Pi Imager page, download and install the software.
Then, start the RPi Imager. Under “Operating System”, choose “Raspberry Pi OS”. Also, make sure that your micro SD card has been detected under “SD Card”.
Now you just need to click on “Write”. The software will download the image for the operating system and flash it into the SD card. Nothing else to do but wait.
Once done, simply remove the SD card and put it into your Raspberry Pi (the Pi must me powered off!). Power on the Raspberry Pi and finish the configuration.
Learn Python 3 basics on the Raspberry Pi
Use Thonny IDE
Thonny IDE is a great tool for beginners. Start it, write some Python code, and execute it by pressing on the “play” button. No need to install Python, configure the version you want to use, the environment variables, and the package manager.
Thonny IDE just works out of the box. And for a Python beginner, that’s just what you need to get started quickly.
Learn Python 3 basics
Now you can start to learn how to code with Python. For this you can follow online tutorials or courses. Here are the minimum core basics you need to learn:
And that’s pretty much it to get started strong. With that you certainly don’t have a complete Python knowledge, but this is enough to write any program you want and get started on your own projects.
One important thing to add to the previous list: learn how to use Python modules, which means how to:
- Install a Python module (you can easily do that from the Thonny IDE, no need to open a terminal yet).
- Import the module in your code.
- Use the functionalities from the module.
Learn how to read documentation and search on the Internet
Python and programming in general is so vast, it’s impossible to know everything. But don’t worry though: all you really need is to master the basics, and then the rest can be found in documentation.
For example, if you’re using a new Python module you don’t know, well if you know how to read a documentation in order to find the function you want, then you’ll be fine.
Reading documentation might not look like a proper skill, nor “searching on Google”. But trust me on that, those are real skills you need to develop. And this only comes with time and practice. If you get to know how to properly search for information on the Internet, you’ll progress much faster.
Learn through projects
OK, first things first, you need some basics. There is nothing you can do against that.
But after learning the core basics, the best way to progress is to work on projects and hands-on practice exercices.
Now, what kind of project can you start? At the beginning this can be quite difficult to choose a project that is both not too easy so you can progress, and not too hard so you don’t feel like being thrown in the middle of the ocean with a 1 person sailing boat.
Fortunately for you, Raspberry Pi comes with a lot of possible projects that check the “for beginners” box. Check out this project list for more ideas. Also in the following of this guide I’m going to introduce a few more concepts and tools which will give you more ideas on what to do next.
Learn Python 3 with the Raspberry Pi functionalities
As I told before, Raspberry Pi has a lot of nice functionalities you can use for your projects. And most of those functionalities come with Python modules you can use directly in your Python programs.
Control Raspberry Pi GPIOs with Python 3
This is a very good first step if you also plan to learn about hardware and robotics. You will be able to see what your Python programs do in real life.
The “RPi.GPIO” Python module is already installed on Raspberry Pi OS and allows you to directly interact with the hardware pins, with just a few lines of code.
Motion detector with a PIR sensor
PIR means Passive InfraRed. A PIR sensor will detect movement in front of it (without emitting any infra-red, just by receiving them). This sensor will allow you to create all kinds of security systems, or simply to do an action (ex: open a door), when someone has been detected.
And, if you though that this sensor would be hard to control with your Raspberry Pi, well it’s quite the opposite! Once you’ve plugged it, with just 4-5 lines of code you can read the data from the sensor.
The Pi Camera module V2 is a nice addition to the Raspberry Pi. This module is also a Python “plug and play” module. You just plug the camera into the camera slot, and then you can directly take a photo, either from the terminal, or with the PiCamera Python module.
If you use Raspberry Pi OS, PiCamera is already installed, and Python already configured. So, once you’ve plugged the camera and booted your Pi, you can really take a photo in a matter of seconds.
After you feel comfortable with the camera module, you can start to go further and use OpenCV to apply image processing algorithms to the images and videos. And if you remember, OpenCV has a Python binding too! Which means that you can use the PiCamera and OpenCV modules in the same program. Detecting a face from the camera images will take you just a few lines of code.
You can create and host a complete web server on your Pi with Python 3. Thanks to a VPN, you can also get access to the web server from the outside, anywhere in the world.
Start with the Flask framework: very easy to use and configure. Django framework is also used a lot by many people and companies around the world.
The environment around Python 3 on your Raspberry Pi
Great, now that you’ve learned the basics and started to work on a few projects, you can start to properly learn more about Python3. Because Python 3 is not just about the language itself.
If you want to become a good Python 3 developer, you’ll need to know how to:
Use the terminal on Unix
You can start by running your code directly from the Thonny Python IDE. But after some time, you’ll find that running scripts directly from the terminal will be more practical and faster. Also, what if you just want to run the script without editing it? Starting a complete IDE for that is a little bit overkill.
So, you’ll have to learn how to use the terminal with Python 3, and also learn the basic Linux terminal commands, to navigate, start programs, change settings, etc. Don’t worry though: you don’t need to know 100% of the commands to get started. With 10 to 20 commands you’ll be able to do 90% of what you need to do on a daily basis.
And why have I put the terminal section after learning the core basics of Python? Well simply because if you need to learn everything at the same time – configuration, terminal, and Python programming – there are chances that you’ll be lost. Focusing on Python first, without worrying about what’s behind, will allow you to get some solid basics. And then it’s time to learn the terminal.
Manage the environment, versions, and packages with pip3
The Python environment can be quite tricky. There are many different versions. Also you need to correctly setup your environment or else you’ll use the wrong version.
And then, on top of the code Python modules, you’ll have many additional modules you can install – using the pip3 terminal utility, each with different versions.
When you start on Raspberry Pi OS, you don’t need to worry about any of that because it’s already configured for you. But then, if you change to a different environment, you’ll need to understand how to setup Python and how to manage modules.
Learn other programming languages
In this post we’re talking about learning Python, so why am I now talking about other languages?
Well, a programming language is a tool. In fact, when learning Python with Raspberry Pi, you don’t just learn Python. You also learn software programming, system administration, robotics, etc. Python is the tool which allows you to actually make things happen.
Now, ideally you’d need to have different tools in your tool set. So, learning a different and complementary language would be a good idea to expand on what you know with a different way to do it.
Also, Python is not an explicit typed language. You can create a variable without giving it a type and it will work. But then, I think it’s a good idea to learn at least one strong typed language when you start, you’ll understand a lot of things better.
If you’re into robotics, automation, or even game development, then I recommend learning C++ next. You’ll learn a lot and then will have a nice tool set: C++ for performance, Python for fast development.
Now, time for you to learn Python 3 with your Raspberry Pi
In this post I gave you some tips on how to get started with Python3, in an efficient way and with a Raspberry Pi board.
So, first focus on writing code and forget about any configuration. Just use tools that work out of the box.
Then, expand your horizon in 2 ways. Start to build projects to practice with coding and discover different modules. And expand your developer knowledge by learning how to use a terminal, manage Python modules, etc.
After you become more confident and start to get your head around Python3, you can start to dive into more advanced and fun projects.
Remember: the key word here is “project”. Set some challenges for yourself with your Raspberry Pi. As you progress with your projects, you will feel that you’re stuck at some point. At this specific point, you’ll know that you need to get more knowledge about this point.