Bird
Raised Fist0
NumPydata~10 mins

Complex number type in NumPy - Interactive Code Practice

Choose your learning style10 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

Practice

(1/5)
1. What does the numpy.complex128 data type represent?
easy
A. A string representing a complex number
B. A real number with 128 bits precision
C. An integer number with 128 bits
D. A complex number with double precision for real and imaginary parts

Solution

  1. Step 1: Understand the data type name

    The name complex128 indicates a complex number type with 128 bits total.
  2. Step 2: Recall complex number structure in numpy

    In numpy, complex128 stores two 64-bit floats: one for real and one for imaginary parts.
  3. Final Answer:

    A complex number with double precision for real and imaginary parts -> Option D
  4. Quick Check:

    complex128 = complex number with 128 bits [OK]
Hint: Look for 'complex' and bit size meaning real+imag parts [OK]
Common Mistakes:
  • Confusing complex128 with real number types
  • Thinking complex128 stores integers
  • Assuming complex128 is a string type
2. Which of the following is the correct way to create a numpy array of complex numbers with type complex64?
easy
A. np.array([1+2j, 3+4j], dtype='complex64')
B. np.array([1+2j, 3+4j], dtype='int64')
C. np.array([1, 2, 3], dtype='complex64')
D. np.array([1+2, 3+4], dtype='complex64')

Solution

  1. Step 1: Check the array elements

    The elements must be complex numbers, so they need an imaginary part like 1+2j.
  2. Step 2: Check the dtype argument

    Using dtype='complex64' correctly sets the array to complex numbers with 64-bit precision.
  3. Final Answer:

    np.array([1+2j, 3+4j], dtype='complex64') -> Option A
  4. Quick Check:

    Complex elements + complex64 dtype = correct array [OK]
Hint: Use complex literals with 'j' and set dtype to complex64 [OK]
Common Mistakes:
  • Using integer dtype with complex numbers
  • Missing imaginary part in elements
  • Using invalid dtype strings
3. What is the output of this code?
import numpy as np
z = np.array([3+4j, 1-1j], dtype=np.complex128)
magnitudes = np.abs(z)
print(magnitudes)
medium
A. [5. 1.41421356]
B. [7.+0.j 0.+0.j]
C. [3.+4.j 1.-1.j]
D. [0.6 0.8]

Solution

  1. Step 1: Understand np.abs on complex array

    np.abs returns the magnitude (distance from origin) of each complex number.
  2. Step 2: Calculate magnitudes manually

    For 3+4j, magnitude = sqrt(3^2 + 4^2) = 5. For 1-1j, magnitude = sqrt(1^2 + (-1)^2) = sqrt(2) ≈ 1.41421356.
  3. Final Answer:

    [5. 1.41421356] -> Option A
  4. Quick Check:

    Magnitude = sqrt(real² + imag²) [OK]
Hint: Magnitude = sqrt(real² + imag²) using np.abs [OK]
Common Mistakes:
  • Confusing magnitude with real or imaginary parts
  • Expecting complex output instead of float magnitudes
  • Using np.angle instead of np.abs
4. The following code throws an error. What is the cause?
import numpy as np
z = np.array([1+2j, 3+4j], dtype='complex64')
angle = np.angle(z, deg=True)
print(angle)
medium
A. np.angle does not support degree output
B. No error; code runs fine
C. Missing import for np.angle function
D. dtype 'complex64' is invalid for np.angle

Solution

  1. Step 1: Check dtype compatibility

    np.angle works with complex arrays of any valid complex dtype including complex64.
  2. Step 2: Check np.angle parameters

    The parameter deg=True is valid and returns angle in degrees.
  3. Final Answer:

    No error; code runs fine -> Option B
  4. Quick Check:

    np.angle supports complex64 and deg=True [OK]
Hint: np.angle supports complex64 and degree output [OK]
Common Mistakes:
  • Assuming complex64 is unsupported by np.angle
  • Thinking deg=True is invalid
  • Expecting import error for np.angle
5. You have a numpy array z = np.array([1+1j, 0+0j, -1-1j], dtype=np.complex128). You want to create a new array that contains the magnitudes but replaces zeros with the string 'zero'. Which code correctly does this?
hard
A. np.array([str(m) if m == 0 else m for m in np.abs(z)])
B. np.abs(z).replace(0, 'zero')
C. np.where(np.abs(z) == 0, 'zero', np.abs(z))
D. np.abs(z).astype(str).replace('0.0', 'zero')

Solution

  1. Step 1: Calculate magnitudes with np.abs

    np.abs(z) returns an array of magnitudes as floats.
  2. Step 2: Replace zeros with 'zero' using np.where

    np.where(condition, value_if_true, value_if_false) replaces elements where magnitude is 0 with string 'zero', else keeps magnitude.
  3. Final Answer:

    np.where(np.abs(z) == 0, 'zero', np.abs(z)) -> Option C
  4. Quick Check:

    Use np.where for conditional replacement in arrays [OK]
Hint: Use np.where(condition, 'zero', values) for replacements [OK]
Common Mistakes:
  • Trying to use .replace on numpy arrays (not supported)
  • Converting all to strings unnecessarily
  • Using list comprehension without numpy benefits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard