Zugriff auf den Imaginärteil
Um auf den Imaginärteil einer komplexen Zahl in Python zuzugreifen, können Sie das .imag
-Attribut des complex
-Objekts verwenden.
## Create a complex number
z = 3 + 2j
## Access the imaginary part
imag_part = z.imag
print(f"The imaginary part of {z} is {imag_part}")
Ausgabe:
The imaginary part of (3+2j) is 2.0
Das .imag
-Attribut gibt den Imaginärteil der komplexen Zahl als float
-Wert zurück.
Sie können auch die imag()
-Funktion verwenden, um den Imaginärteil einer komplexen Zahl zu extrahieren:
## Create a complex number
z = 3 + 2j
## Access the imaginary part using the imag() function
imag_part = imag(z)
print(f"The imaginary part of {z} is {imag_part}")
Ausgabe:
The imaginary part of (3+2j) is 2.0
Sowohl das .imag
-Attribut als auch die imag()
-Funktion liefern dasselbe Ergebnis, aber das .imag
-Attribut wird im Allgemeinen bevorzugt, da es kompakter und intuitiver ist.
graph TD
A[Complex Number] --> E[Imaginary Part]
E --> F[.imag Attribute]
E --> G[imag() Function]
Indem Sie verstehen, wie Sie auf den Imaginärteil einer komplexen Zahl in Python zugreifen können, können Sie in Ihren Programmen effektiver mit komplexen Zahlen arbeiten.