0
0
NumPydata~10 mins

Complex number type in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a complex number using numpy.

NumPy
import numpy as np
z = np.[1](3 + 4j)
print(z)
Drag options to blanks, or click blank then click option'
Acomplex
Barray
Cfloat
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.array instead of np.complex
Using np.float or np.int which are for real numbers only
2fill in blank
medium

Complete the code to get the imaginary part of a numpy complex number.

NumPy
import numpy as np
z = np.complex(2 + 5j)
imag_part = z[1]
print(imag_part)
Drag options to blanks, or click blank then click option'
A.imag
B.real
C.conjugate()
D.abs()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .real instead of .imag
Calling .abs() which returns magnitude
3fill in blank
hard

Fix the error in the code to correctly create a numpy complex number from real and imaginary parts.

NumPy
import numpy as np
real = 1
imag = 2
z = np.complex([1] + [2]j)
print(z)
Drag options to blanks, or click blank then click option'
Areal, imag
Breal, real
Creal, 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable twice
Not multiplying the imaginary part by j
4fill in blank
hard

Fill both blanks to create a numpy array of complex numbers from two lists of real and imaginary parts.

NumPy
import numpy as np
real_parts = [1, 2, 3]
imag_parts = [4, 5, 6]
complex_array = np.array([[1] + [2]j for [3] in range(3)])
print(complex_array)
Drag options to blanks, or click blank then click option'
Areal_parts
Bimag_parts
Ci
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names
Forgetting to multiply imaginary parts by j
5fill in blank
hard

Fill all three blanks to create a dictionary mapping complex numbers to their magnitudes.

NumPy
import numpy as np
complex_nums = [1+1j, 2+2j, 3+3j]
magnitudes = [1]: np.[2]([3]) for [4] in complex_nums}}
print(magnitudes)
Drag options to blanks, or click blank then click option'
Anum
Babs
Dmagnitude
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names
Using np.abs incorrectly