セクション 1. 章 4
single
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
タスク
スワイプしてコーディングを開始
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.
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 4
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください