Bird
Raised Fist0
NumPydata~10 mins

Trigonometric functions (sin, cos, tan) in NumPy - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the sine of 0 radians using numpy.

NumPy
import numpy as np
result = np.[1](0)
print(result)
Drag options to blanks, or click blank then click option'
Acos
Barcsin
Ctan
Dsin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cos' instead of 'sin' will calculate cosine, not sine.
Using 'tan' will calculate tangent, which is different.
Using 'arcsin' is the inverse sine function, not sine.
2fill in blank
medium

Complete the code to calculate the cosine of pi radians using numpy.

NumPy
import numpy as np
result = np.[1](np.pi)
print(result)
Drag options to blanks, or click blank then click option'
Asin
Btan
Ccos
Darccos
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sin' will calculate sine, which is 0 at pi radians.
Using 'tan' will calculate tangent, which is 0 at pi radians.
Using 'arccos' is the inverse cosine function, not cosine.
3fill in blank
hard

Fix the error in the code to calculate the tangent of 45 degrees using numpy. (Hint: numpy expects radians.)

NumPy
import numpy as np
angle_degrees = 45
angle_radians = angle_degrees [1] np.pi / 180
result = np.tan(angle_radians)
print(result)
Drag options to blanks, or click blank then click option'
A*
B/
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '/' changes the formula incorrectly.
Using '+' or '-' will cause syntax or logic errors.
4fill in blank
hard

Fill both blanks to create a dictionary that maps angles in degrees to their sine values using numpy.

NumPy
import numpy as np
angles = [0, 30, 45, 60, 90]
sine_values = {angle: np.sin(angle [1] np.pi / 180) for angle in [2]
print(sine_values)
Drag options to blanks, or click blank then click option'
A*
B+
Cangles
Drange(5)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' in the conversion formula.
Using 'range(5)' instead of the actual list of angles.
5fill in blank
hard

Fill both blanks to create a dictionary that maps angles in degrees to their cosine values, but only include angles where cosine is greater than 0.5.

NumPy
import numpy as np
angles = [0, 30, 45, 60, 90]
cosine_values = {angle: np.[1](angle * np.pi / 180) for angle in angles if np.cos(angle * np.pi / 180) [2] 0.5}
print(cosine_values)
Drag options to blanks, or click blank then click option'
Bcos
C>
Dsin
Attempts:
3 left
💡 Hint
Common Mistakes
Adding ':' after angle in the key (should be empty).
Using 'sin' instead of 'cos' for cosine calculation.
Using '<' instead of '>' in the condition.

Practice

(1/5)
1. What does the numpy function np.sin() calculate when given an angle in radians?
easy
A. The angle converted from radians to degrees.
B. The cosine of the angle, which is the ratio of the adjacent side to the hypotenuse.
C. The tangent of the angle, which is the ratio of the opposite side to the adjacent side.
D. The sine of the angle, which is the ratio of the opposite side to the hypotenuse in a right triangle.

Solution

  1. Step 1: Understand the function purpose

    The function np.sin() calculates the sine value of an angle given in radians.
  2. Step 2: Recall sine definition in triangles

    Sine of an angle is the ratio of the length of the opposite side to the hypotenuse in a right triangle.
  3. Final Answer:

    The sine of the angle, which is the ratio of the opposite side to the hypotenuse in a right triangle. -> Option D
  4. Quick Check:

    np.sin() gives sine ratio [OK]
Hint: Remember sin = opposite/hypotenuse in triangles [OK]
Common Mistakes:
  • Confusing sine with cosine or tangent
  • Thinking np.sin() converts radians to degrees
  • Using degrees directly without conversion
2. Which of the following is the correct way to calculate the cosine of 60 degrees using numpy?
easy
A. np.cos(np.radians(60))
B. np.cos(np.sin(60))
C. np.cos(np.degrees(60))
D. np.cos(60)

Solution

  1. Step 1: Recognize angle units for numpy trig functions

    numpy trigonometric functions expect angles in radians, not degrees.
  2. Step 2: Convert degrees to radians before using np.cos()

    Use np.radians(60) to convert 60 degrees to radians, then apply np.cos().
  3. Final Answer:

    np.cos(np.radians(60)) -> Option A
  4. Quick Check:

    Convert degrees to radians before trig functions [OK]
Hint: Always convert degrees to radians with np.radians() [OK]
Common Mistakes:
  • Passing degrees directly to np.cos()
  • Using np.degrees() instead of np.radians()
  • Passing a trig function inside np.cos()
3. What is the output of the following code?
import numpy as np
angles = np.array([0, 90, 180])
radians = np.radians(angles)
sin_values = np.sin(radians)
print(np.round(sin_values, 2))
medium
A. [0.00 0.00 0.00]
B. [1.00 0.00 -1.00]
C. [0.00 1.00 0.00]
D. [0.00 -1.00 0.00]

Solution

  1. Step 1: Convert angles to radians

    Angles 0, 90, 180 degrees are converted to radians: 0, π/2, π.
  2. Step 2: Calculate sine values and round

    sin(0) = 0, sin(π/2) = 1, sin(π) = 0. Rounded to 2 decimals: [0.00, 1.00, 0.00].
  3. Final Answer:

    [0.00 1.00 0.00] -> Option C
  4. Quick Check:

    sin(0, 90, 180) = [0, 1, 0] [OK]
Hint: Recall sin(0)=0, sin(90°)=1, sin(180°)=0 [OK]
Common Mistakes:
  • Not converting degrees to radians
  • Confusing sine values at 90 and 180 degrees
  • Forgetting to round output
4. The following code is intended to calculate the tangent of 45 degrees but gives an incorrect result. What is the error?
import numpy as np
tan_45 = np.tan(45)
print(tan_45)
medium
A. np.tan() cannot calculate tangent for 45 degrees.
B. The angle 45 should be converted to radians before using np.tan().
C. The code should use np.tan(np.degrees(45)) instead.
D. The print statement is missing parentheses.

Solution

  1. Step 1: Identify input units for np.tan()

    numpy trigonometric functions require angles in radians, not degrees.
  2. Step 2: Correct the input by converting degrees to radians

    Use np.radians(45) to convert 45 degrees before passing to np.tan().
  3. Final Answer:

    The angle 45 should be converted to radians before using np.tan(). -> Option B
  4. Quick Check:

    Convert degrees to radians before np.tan() [OK]
Hint: Always convert degrees to radians before trig functions [OK]
Common Mistakes:
  • Passing degrees directly to np.tan()
  • Using np.degrees() instead of np.radians()
  • Assuming np.tan() works with degrees
5. You have an array of angles in degrees: angles = np.array([30, 45, 60]). You want to create a new array that contains the tangent values of these angles but only include values where the tangent is less than 2. Which code correctly does this?
hard
A. tan_vals = np.tan(np.radians(angles)); filtered = tan_vals[tan_vals < 2]
B. tan_vals = np.tan(angles); filtered = tan_vals[tan_vals < 2]
C. tan_vals = np.tan(np.degrees(angles)); filtered = tan_vals[tan_vals < 2]
D. tan_vals = np.tan(np.radians(angles)); filtered = tan_vals[tan_vals > 2]

Solution

  1. Step 1: Convert degrees to radians for tangent calculation

    Use np.radians(angles) to convert the array of degrees to radians before applying np.tan().
  2. Step 2: Filter tangent values less than 2

    Use boolean indexing tan_vals[tan_vals < 2] to select values less than 2.
  3. Final Answer:

    tan_vals = np.tan(np.radians(angles)); filtered = tan_vals[tan_vals < 2] -> Option A
  4. Quick Check:

    Convert degrees, then filter tan values less than 2 [OK]
Hint: Convert degrees first, then filter with boolean indexing [OK]
Common Mistakes:
  • Not converting degrees to radians
  • Filtering with wrong comparison operator
  • Using np.degrees() instead of np.radians()