Engineering applications
Five general building blocks that every engineering calculation needs - reading, calculating, constants & manage units and round & output, classify. Every programming task addresses one or more of these.
Foundation 1 — Input & Conversion
Read measured values at runtime instead of being fixed in the code.
What does input()?
input() read one line as Text (str). For calculations, you convert the text into a number - with float() (decimal number) or
int() (integer). float() also understands scientific notation (1e-6 = 0.000001):
alter = int(input()) # "20" -> 20 (Ganzzahl) groesse = float(input()) # "1.75" -> 1.75 (Kommazahl) nu = float(input()) # "1e-6" -> 1e-06 (wiss. Schreibweise)
Every call from input() reads that next line. You read multiple values with multiple input()-Call one after the other — the
Order determines which value goes into which variable.
TipIn the exercise
There is no keyboard window in the programming task: the exam feeds the inputs automatically and compare your output. That's why the exercise only has the "Check code" button - you don't have to type anything in yourself.
Foundation 2 — Calculating with quantities
Writing a formula as a Python expression — operators, order of operations and parentheses.
Arithmetic symbols and brackets
You translate a formula character by character into a Python expression: the symbol “·” becomes
*, the fraction line to /, “high” to **. It applies
Point before line like in mathematics.
With Parentheses you force the order - important as soon as a composite denominator or a bracketed counter is in play:
# Geschwindigkeit: v = s / t v = weg / zeit # Bruchstrich wird zu / # Klammern steuern die Reihenfolge: x / (a + b) # teilt durch die SUMME (a + b) x / a + b # teilt nur x durch a, dann + b ← meist falsch
Example 2.1 — Velocity v = s / t
Loading Python…Tests
Foundation 3 — Constants & Units
Create fixed values in the code and output a unit.
Fixed values as constants in code
Not every value is read in: physical constants (e.g. B. the acceleration due to gravity
g or the density ρ) you put directly in the code as a named variable. This The decimal separator is the period — 9.81, never
9,81 (with a comma, Python reads two separate values).
g = 9.81 # Erdbeschleunigung in m/s², Punkt als Dezimaltrennzeichen masse = 2.0 gewichtskraft = masse * g
Include a unit in the output
Append a unit as another text argument on print() on.
print(...) sets between every two arguments exactly one space:
print("Gewichtskraft:", round(gewichtskraft, 1), "N")
# -> Gewichtskraft: 19.6 N
Example 3.1 — Constant and Unit
Loading Python…Tests
Foundation 4 — Rounding & exact output
Round to a fixed number of digits and match the format exactly.
round() — round to decimal places
Calculation results often have many decimal places. round(wert, n) rounds up
n Spots — clearly visible if the result is previously “crooked”:
round(10/3, 2) # 3.3333… wird zu 3.33 round(2.71828, 2) # wird zu 2.72
print("Text:", wert) sets automatically exactly one space
between text and value.
Example 4.1 — Round and print
Loading Python…Tests
ImportantExactly the lines requested
The programming tasks check the required output lines. Give
exactly the lines required from - usually one, but sometimes also
several — and no additional ones beyond that. You can create multiple lines with several print()- Call:
print("Zeile 1:", 1.0)
print("Zeile 2:", 2.0)
The checks accept trailing zeros: 14.0 and 14.00 count as the same numeric value. What matters is the correctly rounded value and the required output lines.
Rounding and formatting are different: round(wert, n) rounds the numeric value to n decimal places but does not add zeros. An f-string with .2f displays exactly two decimal places. Decimal places are not significant figures.
Foundation 5 — Classifying values
A calculated number with if/elif/else divide into areas.
Case distinction with if/elif/else
Often one should calculated Value not only output, but also rating
- what area does it fall into? For this you compare it with boundaries (<, >= …) and forward via
if/elif/else a text variable.
The order counts: check the smallest limit first.
if wert < 10:
bereich = "klein"
elif wert < 100:
bereich = "mittel"
else:
bereich = "gross"
Example 5.1 — Classify into areas
Loading Python…Tests
TipUse the result twice
You can achieve the same intermediate result output as well as for the
Classification use - first calculate, then use
if/elif/else sort. The own module “if/elif/else” goes deeper.
Sources & further literature
Where the knowledge imparted here is officially documented.
Official Python documentation
- Python Library —
input(): docs.python.org/3/library/functions.html#input - Python Library —
round(): docs.python.org/3/library/functions.html#round - Python tutorial — 4. More Control Flow Tools (
if/elif/else): docs.python.org/3/tutorial/controlflow.html
Tutorials & Manuals
- Real Python — Basic Input and Output in Python: realpython.com/python-input-output
- Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023.
- Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021.