Python is a wonderful and powerful programming language that's easy to use (easy to read and write) and, with Raspberry Pi, lets you connect your project to the real world.
Python can be easily downloaded and used for free. It is an open-source program, and unlike many other free programs, the code created can also be used for commercial products. So for developers world-wide, Python software is the free platform they use to create programs for websites or other digital, machine learning, AI projects. Python syntax is very clean, with an emphasis on readability, and uses standard English keywords. The easiest introduction to Python is through Thonny, a Python3 development environment. Open Thonny from the Desktop or applications menu: Thonny gives you a REPL (Read-Evaluate-Print-Loop), which is a prompt you can enter Python commands.
Python syntax is very clean, with an emphasis on readability, and uses standard English keywords.
The Python 3.7 interpreter and runtime. Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but. Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on. Platform: GNU/Linux/macOS/Windows/Solaris. Eclipse is an integrated.
The easiest introduction to Python is through Thonny, a Python3 development environment. Open Thonny from the Desktop or applications menu:
Thonny gives you a REPL (Read-Evaluate-Print-Loop), which is a prompt you can enter Python commands into. Because it's a REPL, you even get the output of commands printed to the screen without using
You can use variables if you need to but you can even use it like a calculator. For example:
Thonny also has syntax highlighting built in and some support for autocompletion. You can look back on the history of the commands you've entered in the REPL with
Alt + P (previous) and
Alt + N (next).
Basic Python usage
Hello world in Python:
Simple as that!
Some languages use curly braces
} to wrap around lines of code which belong together, and leave it to the writer to indent these lines to appear visually nested. However, Python does not use curly braces but instead requires indentation for nesting. For example a
for loop in Python:
The indentation is necessary here. A second line indented would be a part of the loop, and a second line not indented would be outside of the loop. For example:
whereas the following:
To save a value to a variable, assign it like so:
Note that data types were not specified with these variables, as types are inferred, and can be changed later.
This time I used comments beside the increment command.
Comments are ignored in the program but there for you to leave notes, and are denoted by the hash
# symbol. Multi-line comments use triple quotes like so:
Python also has lists (called arrays in some languages) which are collections of data of any type:
Lists are denoted by the use of square brackets
 and each item is separated by a comma.
Some data types are iterable, which means you can loop over the values they contain. For example a list:
This takes each item in the list
numbers and prints out the item:
Note I used the word
number to denote each item. This is merely the word I chose for this - it's recommended you choose descriptive words for variables - using plurals for lists, and singular for each item makes sense. It makes it easier to understand when reading.
Other data types are iterable, for example the string:
This loops over each character and prints them out:
The integer data type is not iterable and trying to iterate over it will produce an error. For example:
However you can make an iterable object using the
range(5) contains the numbers
4 (five numbers in total). To get the numbers
5 (inclusive) use
You can use functions like
len to find the length of a string or a list:
You can use
if statements for control flow:
Python files in Thonny
To create a Python file in Thonny, click
File > New and you'll be given a
For example, in the new window, type:
Then save this file (
File > Save or
Ctrl + S) and run (
Run > Run Module or hit
F5) and you'll see the output in your original Python window.
Executing Python files from the command line
You can write a Python file in a standard editor, and run it as a Python script from the command line. Just navigate to the directory the file is saved in (use
ls for guidance) and run with
Other ways of using Python
The standard built-in Python shell is accessed by typing
python3 in the terminal.
This shell is a prompt ready for Python commands to be entered. You can use this in the same way as Thonny, but it does not have syntax highlighting or autocompletion. You can look back on the history of the commands you've entered in the REPL by using the Up/Down keys. Use
Ctrl + D to exit.
IPython is an interactive Python shell with syntax highlighting, autocompletion, pretty printing, built-in documentation, and more. IPython is not installed by default. Install with:
Then run with
ipython from the command line. It works like the standard
python3, but has more features. Try typing
len? and hitting
Enter. You're shown information including the docstring for the
Try the following dictionary comprehension:
Python Software Download Latest Version
This will pretty print the following:
In the standard Python shell, this would have printed on one line:
You can look back on the history of the commands you've entered in the REPL by using the Up/Down keys like in
python. The history also persists to the next session, so you can exit
ipython and return (or switch between v2/3) and the history remains. Use
Ctrl + D to exit.
Installing Python libraries
Some Python packages can be found in the Raspberry Pi OS archives, and can be installed using apt, for example:
This is a preferable method of installing, as it means that the modules you install can be kept up to date easily with the usual
sudo apt update and
sudo apt full-upgrade commands.
Not all Python packages are available in the Raspberry Pi OS archives, and those that are can sometimes be out of date. If you can't find a suitable version in the Raspberry Pi OS archives, you can install packages from the Python Package Index (known as PyPI).
To do so, install pip:
Then install Python packages (e.g.
Read more on installing software in Python here.
The official Python Package Index (PyPI) hosts files uploaded by package maintainers. Some packages require compilation (compiling C/C++ or similar code) in order to install them, which can be a time-consuming task, particlarly on the single-core Raspberry Pi 1 or Pi Zero.
piwheels is a service providing pre-compiled packages (called Python wheels) ready for use on the Raspberry Pi. Raspberry Pi OS is pre-configured to use piwheels for pip. Read more about the piwheels project at www.piwheels.org.