Aufgabe 1: Datentypen erkennen

Gegeben sind:

x = 5
y = "Hallo"
z = 3.14

Gib mit type() den Datentyp jeder Variable aus (Integer, String, Float).

Musterlösung: Datentypen bestimmen
x = 5
y = "Hallo"
z = 3.14
print(type(x))  
print(type(y)) 
print(type(z))  

Aufgabe 2: Typkonvertierung

Erstelle eine Variable zahl_str = "123". Wandle sie in eine Ganzzahl zahl_int um und addiere 7. Gib das Ergebnis aus (sollte 130 sein).

Musterlösung: String zu Integer
zahl_str = "123"
zahl_int = int(zahl_str)
print(zahl_int + 7)

Aufgabe 3: Boolesche Werte

Erstelle zwei Variablen: ist_wahr = True und ist_falsch = False. Gib beide Variablen aus.

Musterlösung: Boolesche Werte
ist_wahr = True
ist_falsch = False
print(ist_wahr)
print(ist_falsch)

Aufgabe 4: Float zu Int

Erstelle x = 7.9. Wandle x in eine ganze Zahl um und gib das Ergebnis aus (sollte 7 sein).

Musterlösung: Float zu Int
x = 7.9
y = int(x)
print(y)  

Aufgabe 5: Typkombination

Erstelle zahl = 5 und text = "5". Versuche, zahl + text auszugeben. Füge dann die nötige Umwandlung hinzu, damit 5 + 5 = 10 korrekt angezeigt wird.

Musterlösung: Umwandlung
zahl = 5
text = "5"
print(zahl + int(text)) 

Nach oben scrollen