Python is a programming language that is very easy to learn and ideal for entering the world of programming. Despite its simplicity, this language also offers the possibility of writing complex programs for a wide range of applications. Python is particularly suitable for the rapid development of large applications. This technology has become known under the keyword RAD (Rapid Application Development).
The Python programming language was introduced in 1991. Python 2, the classic version, has been around since 2000. It still exists in large, long-lasting projects and is still supported by the Python developers, currently with version 2.7. Python 3 was introduced in 2008 and with version 3.6 it is the current version for beginners.
The Python programming language combines the following advantages:
- A simple, clear syntax: Python is an ideal programming language for beginners. It is limited to simple, clear instructions and often to a single possible solution. This is quickly remembered and the developer is trusted.
- Clear structures: Python requires developers to write in an easily readable structure. The arrangement of the program lines also results in the logical structure of the program.
- Reuse of code: The modularization, i.e. the breaking down of a problem into partial problems and the subsequent merging of the partial solutions into an overall solution, is made very easy in Python. The existing partial solutions can easily be used for other tasks, so that you as a developer will soon have an extensive pool of modules.
- Object manipulation: In Python, all data is stored as objects. This leads to a uniform treatment for objects of different types. On the other hand, the physical storage of the objects by Python takes place automatically, i.e. without the intervention of the developer. The developer does not have to worry about reserving and releasing suitable memory areas.
- Interpreter / Compiler: Python programs are interpreted immediately. They don’t have to be compiled and bound first. This enables frequent, quick changes between the coding and testing phases.
- Independence from the operating system: Both programs that are operated from the command line and programs with graphical user interfaces can be used on different operating systems (Windows, Linux, macOS) without redevelopment or adaptation.
Python and the Raspberry Pi
The Raspberry Pi is a complete computer on a small circuit board, which enables a very inexpensive entry into the software world of programming as well as into the hardware world of electronics. The open source system Raspbian, a variant of the Linux distribution Debian, is often used as the operating system. It has been continuously developed since its first appearance in 2012.
The Pi in the name stands for Python interpreter. Python is available as the primary programming language on every Raspberry Pi. You can create electronic circuits and connect them to the Raspberry Pi via an interface provided for this purpose. You send signals to these circuits using a Python program in order to control them. In addition, you receive signals from these circuits via a Python program in order to determine certain values. You can display these values on the screen, save them or make them available via the Internet.
Python user interfaces
If you would like to provide the user with a comfortable graphical user interface for operation, then with Python the use of the tried and tested and largely standardized library Tk is an option. It’s well integrated on Windows as well as Ubuntu Linux and macOS. The tkinter module represents an interface to this library. After installing Python, it is already available under the various operating systems. It offers a number of classes for creating the elements of the surface. Event-oriented programming comes to the fore.
There are other libraries for graphical user interfaces with an interface to Python. However, the integration is sometimes quite complex and differs in the various operating systems. Examples include the Gimp Toolkit (GTK +) with the pyGTK interface, wxWidgets with the wxPython interface or QT with the PyQt interface.
An example program
An example program follows in this section. If you do not have any programming experience, it should show you the simple and clear structure of Python programs. If you already have knowledge of another programming language, you will find many familiar elements, but also Python-specific elements, in the code and the short comments. Of course, this demo example is not yet suitable for a thorough learning of Python.
The program serves as an exercise in mental arithmetic. First of all, the user decides on the number of tasks that are given to him. Then, with the help of a random generator, tasks from the basic arithmetic operations are set, with numbers from different areas. The user has a maximum of three attempts to find the correct solution. An evaluation of his entries then takes place. One possible output of the program:
Wieviele Aufgaben (1 bis 10):
3
Aufgabe 1 von 3 : 32 + 2
Bitte eine Zahl eingeben:
34
34 ist richtig
Ergebnis: 34
Aufgabe 2 von 3 : -44 + 36
Bitte eine Zahl eingeben:
8
8 ist falsch
Bitte eine Zahl eingeben:
-8
-8 ist richtig
Ergebnis: -8
Aufgabe 3 von 3 : 95 + -11
Bitte eine Zahl eingeben:
85
85 ist falsch
Bitte eine Zahl eingeben:
86
86 ist falsch
Bitte eine Zahl eingeben:
83
83 ist falsch
Ergebnis: 84
Richtig: 2 von 3
The following is the code of the program in the “kopverchnen.py” file:
# Zufallsgenerator
import random
random.seed()
# Anzahl Aufgaben
anzahl = -1
while anzahl<0 or anzahl>10:
try:
print(“Wieviele Aufgaben (1 bis 10):”)
anzahl = int(input())
except:
continue
# Anzahl richtige Ergebnisse
richtig = 0
# Schleife mit “anzahl” Aufgaben
for aufgabe in range(1,anzahl+1):
# Operatorauswahl
opzahl = random.randint(1,4)
# Operandenauswahl
if(opzahl == 1):
a = random.randint(-50,100)
b = random.randint(-50,100)
op = “+”
c = a + b
elif(opzahl == 2):
a = random.randint(1,100)
b = random.randint(1,100)
op = “-”
c = a – b
elif(opzahl == 3):
a = random.randint(1,15)
b = random.randint(1,15)
op = “*”
c = a * b
# Sonderfall Division
elif(opzahl == 4):
c = random.randint(1,15)
b = random.randint(1,15)
op = “/”
a = c * b
# Aufgabenstellung
print(“Aufgabe”, aufgabe, “von”,
anzahl, “:”, a, op, b)
# Schleife mit 3 Versuchen
for versuch in range(1,4):
# Eingabe
try:
print(“Bitte eine Zahl eingeben:”)
zahl = int(input())
except:
# Falls Umwandlung nicht erfolgreich
print(“Sie haben keine Zahl eingegeben”)
# Schleife unmittelbar fortsetzen
continue
# Kommentar
if zahl == c:
print(zahl, “ist richtig”)
richtig = richtig + 1
break
else:
print(zahl, “ist falsch”)
# Richtiges Ergebnis der Aufgabe
print(“Ergebnis: “, c)
# Anzahl richtige Ergebnisse
print(“Richtig:”, richtig, “von”, anzahl)