111
4.13
Typumwandlungen
4.13.3 complex()
Komplexe Zahlen können nicht in einen anderen numerischen Typ (int, long oder float)
konvertiert werden. Folgende Aufrufe führen zu Fehlermeldungen:
Umgekehrt kann aber jede Zahl in eine komplexe Zahl überführt werden:
4.13.4 bool()
Der Aufruf bool() liefert zu einem beliebigen Objekt dessen Wahrheitswert in Form eines
der Literale
True oder False.
4.13.5 str()
Ein häufig vorkommender Anwendungsfall ist die Umwandlung einer Zahl in eine Zei-
chenkette (Typ
str), die dann z.B. durch Konkatenation in einen größeren Text eingebaut
wird.
Programmlauf:
int(1+2j)
float(1+0j)
>>> complex(1)
(1+0j)
>>> complex(1e10)
(10000000000+0j)
>>> bool() # Wahrheitswert von None
False
>>> bool(123) # Wahrheitswert einer Zahl ungleich 0
True
>>> bool(0) # Wahrheitswert von null
False
>>> bool([]) # Wahrheitswert einer leeren Liste
False
>>> bool(2>1) # Wahrheitswert eines Vergleichs
True
zahl = input("Geben Sie eine Zahl ein: ")
quadrat = float(zahl) * float(zahl)
antwort = "Das Quadrat von " + zahl + " ist " + str(quadrat) + "."
print(antwort)
Geben Sie eine Zahl ein: 2332.34
Das Quadrat von 2332.34 ist 5439809.8756.

Get Python 3 - Lernen und professionell anwenden now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.