How to print Sine series in Python
In this tutorial, we will learn how to print a sine series in Python. To do this we need the math module.
This is a very simple program to print the sine series.
In this program, since we need to perform a mathematical operation specifically a trigonometric function that is to find the sine value for a range of degrees.
Simply import the math module which contains definitions to perform all the mathematical functions like finding the Square root, Ceil, Floor, angle conversions, etc.
To learn more about the functions in the math module, click on the below link.
Now, let’s begin with the code after importing the most important math module. Use the print function to print a text on the output screen.
print("sine series is as shown below\n")
Find the sine series for values provided in the range method in Python
Since we need to print a series, let’s use a for loop and a range() method to control the repetition of the block that we shall write within in the for loop.
The range method takes 3 parameters, the 1st value will be the beginning value, 2nd is ending value, last is the incremental step value.
In our program, we will be printing a sine series from 0 to 180, therefore the 1st value in range method is 0 since we need to even find sine(180) we should mention 2nd value as 181, as the range method takes 1 less than the ending value.
Let’s see the code for the above statements.
for i in range(0,181,15):
In the above code, variable i is used for iteration that takes values provided in the range method.
To calculate the sine value, we need to call the sine function as math.sine() that takes only one parameter that is the degree value and to store its value let’s take another variable y.
Finally use another print statement to print the series stored in the variable y.
Let’s see the entire code.
import math print("Sine series is as shown below\n") for i in range(0,181,15): y=math.sin(i) print("sine", i ,":", y) print("Finished printing the sine series for a given range")
The above code outputs the sine values taking the values provided in the range method in terms of radians as shown below :
OUTPUT : sine series is as shown below : sine 0: 0.0 sine 15: 0.650287..... and so on until sine 180.
To print sine values in terms of degrees, we need to call math.radians() method
for the angle conversion as math.radians().
So, the final code that prints sine series is as follows :
import math print("Sine series is as shown below\n") for i in range(0,181,15): x=math.radians(i) y=math.sin(x) print("sine", i ,":", y) print("Finished printing the sine series for a given range")
The required output will be as shown below :
OUTPUT : Sine series is as shown below : sine 0 : 0.0 sine 15: 0.25881904510252074 sine 30: 0.4999999999999994 sine 45: 0.7071067811865475 sine 60: 0.8660254037844386 sine 75: 0.9659258262890683 sine 90: 1.0 sine 105: 0.9659258262890683 sine 120: 0.8660254037844387 sine 135: 0.7071067811865476 sine 150: 0.4999999999999994 sine 165: 0.258819045102521 sine 180 : 1.2246467991473532e-16 Finished printing the sine series for a given range.