Complex Numbers
What are complex numbers and how are they represented in Python? A complex number is a number that is the sum of the real and imaginary parts. In Python, the real part is represented by an ordinary int or float, and the imaginary part is an int or float with a suffix j
. Let's look at examples, so that it becomes clearer for us.
Input:
# Creating complex numbers
x = 35+2j
y = 2.5j
z = -58j
# Check the type
print(f'It`s x: {x}, the type of x is {type(x)}')
print(f'It`s y: {y}, the type of y is {type(y)}')
print(f'It`s z: {z}, the type of z is {type(z)}')
Output:
It`s x: (35+2j), the type of x is <class 'complex'>
It`s y: 2.5j, the type of y is <class 'complex'>
It`s z: (-0-58j), the type of z is <class 'complex'>
Using .real
, we can get the real part of any complex number, using .imag
method, we can get the imaginary part.
Input:
# Creating complex numbers
x = 54 + 7j
# Access to the real part
print(f'The real part of {x} is {x.real}')
# Access to the imaginary part
print(f'The imaginary part of {x} is {x.imag}')
Output:
The real part of (54+7j) is 54.0
The imaginary part of (54+7j) is 7.0
Swipe to start coding
Create one variable: a
with the following value: -7+3j
. Check the type of this object.
And display the real and imaginary parts of this number.
Løsning
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 3.85
Complex Numbers
Stryg for at vise menuen
What are complex numbers and how are they represented in Python? A complex number is a number that is the sum of the real and imaginary parts. In Python, the real part is represented by an ordinary int or float, and the imaginary part is an int or float with a suffix j
. Let's look at examples, so that it becomes clearer for us.
Input:
# Creating complex numbers
x = 35+2j
y = 2.5j
z = -58j
# Check the type
print(f'It`s x: {x}, the type of x is {type(x)}')
print(f'It`s y: {y}, the type of y is {type(y)}')
print(f'It`s z: {z}, the type of z is {type(z)}')
Output:
It`s x: (35+2j), the type of x is <class 'complex'>
It`s y: 2.5j, the type of y is <class 'complex'>
It`s z: (-0-58j), the type of z is <class 'complex'>
Using .real
, we can get the real part of any complex number, using .imag
method, we can get the imaginary part.
Input:
# Creating complex numbers
x = 54 + 7j
# Access to the real part
print(f'The real part of {x} is {x.real}')
# Access to the imaginary part
print(f'The imaginary part of {x} is {x.imag}')
Output:
The real part of (54+7j) is 54.0
The imaginary part of (54+7j) is 7.0
Swipe to start coding
Create one variable: a
with the following value: -7+3j
. Check the type of this object.
And display the real and imaginary parts of this number.
Løsning
Tak for dine kommentarer!
Awesome!
Completion rate improved to 3.85single