Python Math sin() Method
Python Math sin() The sine of the x radians returned.
Python Math sin() Syntax
Here is the syntax of the sin() method:
import math
math.sin(x)
Note: sin() is not directly accessible, you need to import the math module and then call the method through the math static object.
Python Math sin() Parameters
- x : A value.
Python Math sin() Return Value
The sine of the returned x radians, with values between -1 and 1.
Python Math sin() Example#1
The following shows an example of using the sin() method.
#!/usr/bin/python3
import math
print ("sin(9) : ", math.sin(9))
print ("sin(-9) : ", math.sin(-9))
print ("sin(0) : ", math.sin(0))
print ("sin(math.pi) : ", math.sin(math.pi))
print ("sin(math.pi/4) : ", math.sin(math.pi/4))
Output:
Python Math sin() Example#2
Graphical representation of the sin() function.
import math
import matplotlib.pyplot as plt
in_array = [-3.14159265, -2.57039399, -0.28559933,
0.28559933, 2.57039399, 3.14159265]
out_array = []
for i in range(len(in_array)):
out_array.append(math.sin(in_array[i]))
i += 1
print("in_array : ", in_array)
print("\nout_array : ", out_array)
# red for numpy.sin()
plt.plot(in_array, out_array, color = 'red', marker = "o")
plt.title("math.sin() in apidemos.com")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
Output:
deepinout@ubuntu:~/apidemos.com$ python3 demos.py
in_array : [-3.14159265, -2.57039399, -0.28559933, 0.28559933, 2.57039399, 3.14159265]
out_array : [-3.5897930298416118e-09, -0.5406408168673427, -0.2817325547837714, 0.2817325547837714, 0.5406408168673427, 3.5897930298416118e-09]