The first article on teaching the Python programming language, which is the official language of the Raspberry Pi.

About Python

As I wrote above – 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 low-level languages ​​such as C and C ++, Python uses the Python interpreter, which translates a Python program into machine-specific instructions.

As long as you have the Python interpreter installed on your computer, you can run any Python program written on any machine.

Moreover, the syntax is simple compared to other languages. Syntax is the arrangement of symbols, words, and phrases required for a Python program to run.

Python’s friendly syntax makes programming fun, perfect for minicomputers designed to introduce people to computer science.

Python versions

There are currently two versions of Python: Python 2 and Python 3.

However, Python 2 was already retired on January 1, 2020, which means it is no longer supported and will not be supported by the Python developers.

reference… As of January 1, 2020, Python 2 is no longer supported.

Thus, the best way to start programming in Python is to learn Python 3. The main differences between the two versions are:

  • Python 3 is now Unicode based. Unicode is an international computer standard for the encoding, representation, and processing of 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. In principle, it is easier to learn.
  • Python 3 is designed with longevity in mind. This means that learning Python 3 is a good investment in your future, and you don’t have to worry about Python 4 coming out soon and you have to relearn.

In this tutorial, we’ll focus on using Python 3.

Raspbian and Python

The current version of the Raspberry Pi OS has both Python 2 and Python 3 by default, which means the following Python components are already preloaded:

  • Interpreter
  • Shell
  • IDE
  • Text editors

There is no need to install other software unless you have your own preferences. Let’s move on to how we get started writing in Python using the Raspberry Pi.

Writing a Python program in the terminal

There are two modes in which you can write a Python program in the terminal:

  • interactive mode
  • scenario mode.

AT interactive mode you write a line, hit Enter and get the result right away. This is because you are given direct access to the Python shell.

On the contrary, scenario mode requires you to create a file in python before seeing the output of your program.

To start a Python 3 shell using the terminal, just type:

$ python3

We should see the following:

The terminal should return useful information about your Python 3 interpreter, including the exact version installed on your Raspberry Pi.

To test the interactive mode, let’s create a canonical Hello World program.

Enter the command below and make sure you use regular parentheses, otherwise your program will not work (do not put a semicolon at the end):

print("Hello World")

We see that the terminal showed us the result:

Unlike other compiled languages, you can do simple math just like in Python. No need to use variables:

To quit the Python shell in the terminal, just type exit() or quit()… Alternatively, you can also use the keyboard shortcut CTRL + Z

In script mode, I suggest finding the active directory first, before creating the Python file. First, we need to understand where we are and for this we use the command pwd:

$ pwd

She will tell you which folder we are in:

Then we enter the command below in order to create the file:

$ touch "имя_файла.py"

I named my file myprogram.py

I expect to see a file in explorer with the exact name and file extension:

Open your Python file and write your program code. Then save.

Finally, to run the program, just type:

$ python3 "имя_файла.py"

The terminal worked perfectly:

Writing a Python Program in Thonny IDE

If you don’t like the terminal setup, you can use the IDE. An IDE, or Integrated Development Environment, is a software application that makes programming easier.

IDEs provide comprehensive programming tools and features in a single graphical user interface. The Raspberry Pi has many IDEs, and you can easily access them using the Start button.

For this tutorial, we are going to use the Thonny IDE. To write a program in Thonny, simply enter the commands in your code editor:

print("Hello World")

In this way:

Save the file anywhere. There is no need to search for the active directory.

Then press the green start button.

The result will appear below. It is an integrated Python wrapper. This is the same shell that you initialized at the beginning.

You can use a wrapper for interactive mode as well as quickly validating Python strings.

Thus, we got acquainted with Python and created our first program in this language in the Raspberry Pi.

If you are interested in how Python works with Arduino, then our Arduino plus Python course will help you.

No Comments
Comments to: Introduction to the Python programming language

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

Good Reads

Worlwide

Trending

Advertiser

Login

Welcome to Typer

Brief and amiable onboarding is the first thing a new user sees in the theme.
Join Typer
Registration is closed.