Welcome to Python!

Guest contribution by | 20.05.2021

This blog article will help you get started with one of the most successful and fascinating programming languages of our time: Python.

Python is successful because it is used in practically all fields of knowledge: Science, technology, mathematics, music and art. Many people find Python fascinating because programming inspires thinking and digital models can be developed elegantly and solutions to problems can be formulated in an understandable way.

After a brief introduction to some important basic computer science terms, you will learn how to install Python and execute your first instructions at the keyboard. Let’s go.

The Python programming language

Unlike “natural” languages such as German or English, which have evolved over centuries, programming languages are “artificial” languages. They were designed by experts and specially tailored to the formulation of algorithms. The first high-level programming languages (e.g. Fortran, Cobol and Lisp) were developed in the 1950s. Today (in 2021), Wikipedia lists 358 programming languages.Âą

The first version of Python was published in 1990 by the Dutch computer scientist Guido van Rossum. The name of the language is said to be reminiscent of the English comedy group Monty Python. Since 2001, the Python Software Foundation (PSF) has taken care of the maintenance, control and distribution of the language. (www.python.org).

Many digital products you know from everyday life are based on Python, e.g. Google Maps, YouTube and Instagram. In the PYPL index (Popularity of Programming Language Index), the popularity of a programming language is measured by how often a language tutorial is searched for on Google. According to this, Python is by far the most popular programming language (in 2021).

Why is Python so popular among programmers?

  • The programming language allows you to write very short programme texts. This improves the comprehensibility of a programme, makes it easier to find errors and shortens the development time.
  • The language is easy to learn because it uses familiar notation that you already know from mathematics, for example.
  • The language supports different programming styles (“paradigms”).
  • And there are many freely available extensions to Python (so-called modules) for special areas of application such as graphics, astronomy, mathematics, speech recognition, quantum computing and artificial intelligence.

 

What is an algorithm?

In computer science, an algorithm is a precise set of instructions for solving a task. An algorithm consists of a sequence of individual instructions that are formulated so precisely and unambiguously that they can be executed purely mechanically even by someone completely unfamiliar with the subject. Algorithms that are familiar from everyday life are, e.g.

  • a cooking recipe,
  • instructions for assembling a shelf,
  • an instruction manual.

A computer programme is an algorithm written in a programming language that can be “understood” and executed by a computer.

Syntax and semantics

A programming language – like any language – is defined by syntax and semantics. The syntax defines which sequences of characters are valid programme text in the respective language.

For example

print['Hallo']

is not valid Python programme text because Python syntax dictates that the word print must be followed by a round bracket.

In contrast, the string

print('Hallo')

is a syntactically correct Python programme. However, the syntax says nothing about what effect this mini-programme has. The meaning of a programme text is defined in the semantics. In this example, the semantics says that the word hello is output on the screen.

In the case of a programme text, the semantics is unambiguous. In contrast, a text in a natural language can be ambiguous. For example, “Look ahead!” is semantically ambiguous.

Interpreter and compiler

Python is a so-called higher programming language. This means that special features of the computer on which the programme is to run do not have to be taken into account. A Python programme runs on practically any computer and under any common operating system. A higher programming language is made for humans and makes it possible to write easily understandable programme texts.

A programme text written in a higher programming language is called source code. In order for the source code to be processed by the computer, it must be translated into a “machine-like language”. There are two different methods for doing this:

  • A compiler translates a complete programme text and creates a directly executable programme file that can be loaded and started by the operating system.
  • style=”margin: 8px 0;”An interpreter reads each instruction of a programme text individually and executes it directly via the operating system. If a programme is to be started, the interpreter must first be called.

Python is an interpretative programming language. This has the advantage that a Python programme works on any platform. However, the prerequisite is that a corresponding interpreter is installed on the computer. The operating system alone is not capable of executing the Python programme.

Installing Python

Python is completely free and is offered for Microsoft Windows, Linux/Unix and macOS. All the software you need to work with the programming language is free and can be downloaded from http://www.python.org/downloads.

Windows

The following refers to version 3.9, which came out in October 2020.

Download Python

Click on the button at the top left with the current version designation. Download the installation programme and start it. Make sure that the directory with the Python interpreter is added to the system path (PATH) during the installation (see next figure). This ensures that the operating system finds the necessary interpreter when you enter the command python in the console window (command prompt). Finally, click on Install Now.

Installing Python

Linux

On Linux systems, Python is usually already installed. Check which version is present by entering the following command in a console window on the command line.

$ python -V
Python 3.9.0

If you do not find a version of Python 3, you will have to install it. It is best to use the Advanced Packaging Tool (APT):

$ sudo apt-get install python3.9

macOS

As on Linux systems, Python is usually already installed on Apple computers. To check this, open a terminal window on your Mac (Programs|Utilities|Terminal) and enter the following command:

python -V

If you do not find a corresponding version, visit the Python website, download an installer file suitable for your system and run it.

Python in interactive mode

If the installation is successful, the following components will be on your computer:

  • the required interpreter,
  • the development environment IDLE (Integrated Development and Learning Environment),
  • a detailed documentation and
  • various auxiliary programmes.

You can call the interpreter directly in a console (shell) to try out individual Python commands. On a Windows computer, you can open a console in the following way: Enter the command cmd in the search field at the bottom left and press the Enter key. An application window appears with the title Command Prompt approximately as in this illustration:

Python Command Prompt

On a Mac, the console is called Terminal. Press the command key and the space bar simultaneously to start Spotlight and type Terminal.

A console contains the so-called command line, which ends with the prompt of the operating system. For Windows the prompt is the character >, for Linux and macOS $. After the prompt of the operating system, enter the command

python

and press the Enter key. (Look for the small p at the beginning.) This starts the Python interpreter in “interactive mode”. You will see this prompt under a welcome text:

>>>

In interactive mode, you have a kind of “conversation” with the Python interpreter. Behind the prompt, you enter a single statement. As soon as you press Enter, the interpreter executes the statement and returns a result in the next line – if the statement calculates a result. This principle is called Read-Eval-Print-Loop or REPL for short.

Arithmetic expressions are also valid Python statements. Try it out:

>>> 2 + 2
4
>>> (2 + 2) * 4
16
>>>

You exit the interpreter with the key combination (Ctrl)+(C).

The IDLE development environment

IDLE (Integrated Development and Learning Environment) is the standard development environment for Python. A development environment is software that programmers use when they develop programs. IDLE consists of the Python shell and an editor:

  • In the Python shell, you use the programming language in interactive mode. You use the Python shell to try out instructions and explore their semantics.
  • With the editor, you can write, save and execute a Python programme from several statements.

When you start IDLE, the Python shell opens first.

Python Shell

After a welcome text, the prompt >>> of the interpreter appears. If you enter an instruction and press Enter, the result appears in the next line.

Hotkeys for the Python Shell

There are two key combinations (hotkeys) that make working with the Python shell easier. With (Alt)+(p) and (Alt)+(n) you can go back and forth in the sequence of the last commands entered (history). First enter any two commands:

>>> 1 + 1
2
>>> 2 * 2
4
>>>

If you press the key combination (Alt)+(p) once, the previous command (previous) appears after the last prompt:

>>> 2 * 2

If you enter this hotkey again, the previous line appears:

>>> 1 + 1

Conclusion

Python is a popular programming language that is easy to learn, as the first examples here suggest. Of course it is important to know and understand basic terms like algorithm, program, syntax, semantics, interpreter etc., but with a little practice, this little introduction to installation and using Python in interactive mode you can easily take your first steps. And if you take them, you will quickly acquire a taste for it…

 

Notes (in German):

[1] Liste der Programmiersprachen

Dieser Artikel ist ein Auszug aus dem neuen Buch “Python 3 Schnelleinstieg” von Michael Weigend. Alle Infos zum Buch, das Inhaltsverzeichnis und eine kostenlose Leseprobe finden Sie hier beim Fachbuchverlag fĂĽr IT, Business und Fotografie mitp.

Python 3 Schnelleinstieg
Michael Weigend

Michael Weigend

Michael Weigend has a PhD in computer science from the University of Potsdam. He worked as a teacher for more than 30 years and gave seminars on the didactics of computer science at the Fern-Universität Hagen for 20 years. At the University of MĂĽnster, he holds lectures on Python programming as part of a teaching assignment. He is also involved in several national and international communities for the use of computers in education, including Constructionism, International Federation for Information Processing (TC 3 Computers in Education), Bebras – International Contest on Informatics and Computational Thinking. He has published over 60 scientific articles and written several books on programming, web development and visual modelling.