AppDividend
Latest Code Tutorials

np.sin: How to Use numpy sin() Function in Python

The np.sin() is a numpy library function that generates the sine value for the angle passed inside the function.

Syntax

numpy.sin(x, /,out=None,*,where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'sin'>

Arguments

The np.sin() function takes one required arguments as a parameter:

  1. x: This is the angle to be passed to the function. This is given in radians. This is the required parameter for calculating the sine value.
  2. out: We can specify the location where we want to store the output value. By default, None is kept as the value for this argument.
  3. where: If this condition is True, then the ufunc result is stored; if the condition is False, then the original value is retained.

Return value

It returns a single value. It returns the sine value for the angle passed inside the function. 

Python program for finding the value of pi / 4 using np.sin()

# importing numpy as np
import numpy as np

# creating an numpy element
ele = np.pi / 4

# storing the sine value to the variable.
sine = np.sin(ele)
print(sine)

Output

0.7071067811865475

In this program, we imported the numpy library and created an element for storing the value of pi by 2 and using the numpy function called np.pi for using pi. Hence, the pi / 2 is stored in the variable named ele. This angle is passed inside the function called sin(). This sin function finds the appropriate sine value from the angle. Finally, we have printed the sine value.

Python program for finding the value of pi / 3 using np.sin()

# Importing numpy as np
import numpy as np

# Storing the sine value to the variable.
sine = np.sin(np.pi / 3)
print(sine)

Output

0.8660254037844386

In this program, we found the sine value for the angle of pi / 3. Then, we passed the angle inside the sin function to get the sine value.

Python program for finding the values of 0 to pi/2 using np.sin()

# Importing numpy as np
import numpy as np

# Creating an array for storing angles
arr = np.array((0, 30, 45, 60, 90)) 
np.pi/180

# Finding the sine values for the angles
sine = np.sin(arr)
print(sine)

Output

[ 0. -0.98803162 0.85090352 -0.30481062 0.89399666]

In this program, we imported numpy and created a numpy array for storing the angles. The numpy array consists of angles of 0, 30, 45, 60, and 90. We found the sine values of these angles using the sin function. We passed an array inside the sin() function in this program.

That’s it for np.sin() function in Python.

Related posts

np.log2

np.log10

np.square

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.