Runden
| print(round(math.pi, 2)) |
3.14 |
| print(round(math.pi, 6)) |
3.141593 |
| print(round(3.555, 2)) |
3.56 |
Aufrunden (zur nächst größeren Zahl)
| print(math.ceil(2.5)) |
3.0 |
| print(math.ceil(2.8)) |
3.0 |
| print(math.ceil(3.1)) |
4.0 |
| print(math.ceil(3.4)) |
4.0 |
| print(math.ceil(-2.1)) |
-2.0 |
| print(math.ceil(-2.5)) |
-2.0 |
| print(math.ceil(-2.8)) |
-2.0 |
| print(math.ceil(-3.1)) |
-3.0 |
Abrunden (zur nächst kleineren Zahl)
| print(math.floor(2.5)) |
2.0 |
| print(math.floor(2.8)) |
2.0 |
| print(math.floor(3.1)) |
3.0 |
| print(math.floor(3.4)) |
3.0 |
| print(math.floor(-2.1)) |
-3.0 |
| print(math.floor(-2.5)) |
-3.0 |
| print(math.floor(-2.8)) |
-3.0 |
| print(math.floor(-3.1)) |
-4.0 |
Umwandeln in Ganze Zahl
| print(int(3.1)) |
3 |
| print(int(3.8)) |
3 |
| print(int(-3.1)) |
-3 |
| print(int(-3.8)) |
-3 |
| print(int("3")) |
3 |
Umwandeln in Gleitkommazahl
| print(float(3)) |
3.0 |
| print(float(3.333)) |
3.333 |
| print(float("3.333")) |
3.333 |
Umwandeln in String
| print(str(3)) |
3 |
| print(str(3.333)) |
3.333 |
| print("Das Quadrat von "+str(4)+" ist "+str(4**2)) |
Das Quadrat von 4 ist 16 |
Variablentyp identifizieren
| print(type(3)) |
<type 'int'> |
| print(type(3.33)) |
<type 'float'> |
| print(type("Hallo")) |
<type 'str'> |
| print(type([1, 2, 3])) |
<type 'list'> |
| print(type(False)) |
<type 'bool'> |