How are the Python semantics defined

Getting started with Python

Michael Weigend May 4th 2021

This article will help you take the first steps in using one of the most successful and fascinating programming languages ​​of our time. Python is successful because it is used in virtually all areas of knowledge: science, technology, math, music, and art. Many people find Python fascinating because programming with Python inspires thinking. With Python you can develop digital models and formulate problem solutions elegantly and understandably.

After a brief overview of the Python programming language, essential basic terms are introduced: algorithm, program, syntax, semantics, interpreter. Below is a quick guide to installing Python. The next step explains how you can try out individual statements in interactive mode (Python shell).

The Python programming language

In contrast to "natural" languages ​​such as German or English, which have developed over centuries, programming languages ​​are "artificial" languages. They have been designed by specialists and are 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 the year 2021) Wikipedia lists 358 programming languages ​​[1].

The first Python version 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. Python has been maintained, controlled and distributed by the Python Software Foundation (PSF) since 2001 [2].

Many digital products that you know from everyday life are based on Python, e.g. B. 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 Google searches for a language tutorial. Accordingly, Python is by far the most popular programming language (in 2021) [3].

What is an algorithm?

In computer science, an algorithm is a precise guide to solving a task. An algorithm consists of a sequence of individual instructions that are formulated so precisely and clearly that they can be carried out purely mechanically even by a completely ignorant person. Algorithms that we know from everyday life are e.g. B.

  • a recipe,
  • instructions for assembling a shelf or
  • a manual.

A computer program is an algorithm written in a programming language that can be "understood" and executed by a computer.

Syntax and semantics

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

For example is

print ['Hello']

not valid Python program text because the Python syntax requires that after the word print must be followed by a round bracket. Against this is the string

print ('Hello')

a syntactically correct Python program. The syntax does not say anything about the effect of this mini-program. The meaning of a program text is defined in the semantics. In this example the semantics say that on the screen the word Hello is issued.

The semantics of a program text are clear. In contrast, a text in a natural language can be ambiguous.

Interpreter and compiler

Python is a so-called higher programming language. This means that special features of the computer on which the program is to run do not have to be taken into account. A Python program runs on practically every computer and under every common operating system. A high-level programming language is made for people and makes it possible to write program texts that are easy to understand.

A program text that is written in a high-level programming language is called Source code (engl. source code). So that the source text can be processed by the computer, it has to be translated into a "machine language". There are two different methods for doing this:

  • A Compiler translates a complete program text and generates a directly executable (executable) Program file that can be loaded and started by the operating system.
  • A interpreter reads each instruction of a program text individually and executes them directly via the operating system. If a program is to be started, the interpreter must first be called.

Python is an interpretive programming language. This has the advantage that a Python program works on any platform. However, a prerequisite is that a Python interpreter is installed on the computer. The operating system alone is unable to run the Python program.

Install Python

Python is completely free and is offered for Microsoft Windows, Linux / Unix, and macOS. All software you need to work with Python is free and can be downloaded from the Python homepage [4]. This book refers to version 3.9, which was released in October 2020. If you install a newer version, however, all of the programs described in this book will work.

Windows

Installation files that match your system are offered on the download page [4].

Click the button in the top left with the latest version of Python 3. Download and run the installer. Make sure that during the installation the directory with the Python interpreter corresponds to the system path (PATH) is added (see Fig. 1). This ensures that the operating system can find the Python interpreter when you enter the command in the console window (command prompt) python enter. Finally click Install Now.

Linux

Python is usually already installed on Linux systems. Check which version you have by entering the command in a console window on the command line python -V enter.

$ python -V Python 3.9.0

If you don't find a version of Python 3, you'll need to reinstall it. Best to use that 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 can't find a version of Python 3, visit the Python website, download and run an installer file that matches your system.

Python in interactive mode

When you have downloaded and installed Python, you will find the following components on your computer:

  • The Python interpreter,
  • the IDLE (Integrated Development and Learning Environment) development environment,
  • detailed documentation and
  • Utilities.

You can call the Python interpreter directly in a console (shell) in order to then try out individual Python commands. On a Windows computer, open a console e.g. E.g. in the following way: Enter the command in the search field at the bottom left cmd and press the [Enter]. An application window with the title will appear command prompt roughly as in Fig. 2.

On a Mac, the console is called terminal. Press Command and Spacebar at the same time to turn on Spotlight to start and enter terminal a. A console contains the so-called command line, which ends with the prompt of the operating system. In Windows, the prompt is the character >, on Linux and macOS $. Enter the command at the prompt of the operating system

python

and press the [Enter] (Pay attention to the little one p at the start). This starts the Python interpreter in "interactive mode". You will see this prompt below a welcome text:

>>>

In interactive mode you have a kind of "conversation" with the Python interpreter. At the prompt, enter a single Python statement. As soon as you [Enter], the interpreter executes the instruction and returns a result in the next line - provided the instruction calculates a result. This principle is called in English Read-Eval-Print-Loop or short REPL.

Arithmetic expressions are also valid Python statements. Try it:

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

You end the Python 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 developing programs. IDLE consists of the Python shell and an editor:

  • In the Python shell, you use Python in interactive mode. You use the Python shell to try out statements and explore their semantics.
  • The editor allows you to write, save and run a Python program made up of several instructions.

When you start IDLE, the Python shell opens first. You will see an application window like in Fig. 3.

After a welcome text, the prompt >>> of the Python interpreter appears. If you enter a Python statement and [EnterIf you press], 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], the previous command appears after the last prompt (previous):

>>> 2 * 2

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

>>> 1 + 1

Conclusion

Python is very popular with programmers for a number of reasons:

  • With Python you can write very short program texts. This improves the comprehensibility of a program, makes troubleshooting easier and shortens development time.
  • Python is easy to learn because it uses familiar spellings such as: B. already knows from mathematics.
  • Python supports different programming styles ("paradigms").
  • There are many freely available extensions (so-called modules) for Python for special areas of application such as graphics, astronomy, mathematics, speech recognition, quantum computers and artificial intelligence.

The article should facilitate the introduction and the first steps in this programming language and encourage you to try it out - and maybe inspire your thinking too ...

The article is an excerpt from Michael Weigend's book "Python 3 quick start"With this book, you will get started with Python programming without any effort. You do not need any previous knowledge.

All the basics are explained in a clear and easily comprehensible manner using code examples. Exercises at the end of the chapters will help you to apply and deepen your newly acquired knowledge in a practical way.

Michael Weigend

Michael Weigend has published over 60 scientific articles and written several books on programming, web development and visual modeling.
>> Read more