The math.sin() method is used to return sine of a number in radians.
Syntax
import math
math.sin(x)
Parameters
x(required): The number you want to calculate the sine, expressed in radians.
It throws a TypeError if the x is not a number.
Return Value
Returns a float value ranging from -1 to 1, representing the sine of a number.
Visual Representation
Example 1: How to Use math.sin() Method
import math
print (math.sin(0))
print (math.sin(5))
print (math.sin(-5))
print (math.sin(12.35))
Output
0.0
-0.9589242746631385
0.9589242746631385
-0.21468628536052867
Example 2
import math
print(math.sin(math.pi))
print(math.sin(math.pi/2))
print(math.sin('app'))
Output
1.2246467991473532e-16
1.0
TypeError: must be real number, not str
Example 3: Plot the math.sin() Method
import math
import matplotlib.pyplot as plt
# Create an array of x values from 0 to 4*pi
x = [0.01*i for i in range(0, 1257)] # 4*pi ~ 12.57, and we step by 0.01
# Create an array of y values using math.sin
y = [math.sin(val) for val in x]
# Generate the plot
plt.figure(figsize=(10, 6))
plt.plot(x, y)
# Set the title and labels
plt.title('Plot of the math.sin() function')
plt.xlabel('x')
plt.ylabel('sin(x)')
# Display the plot
plt.grid(True)
plt.show()
Output
That’s it.