Cara menggunakan math.sin python

Python is a high-level programming language that is not just user-friendly but also very easy to learn. It is recommended that the new programmers should start their development lessons with Python programming language. Most of the functions which are complex in C++, Java, or any other programming language are very simple and easy with Python. It provides built-in functionalities for every purpose. The Python libraries and modules contain several functions that come in handy for every type of functionality. The math module of Python programming language contains a variety of mathematical operations and one of which is a sin() function. In this article, you will learn about the syntax of the sin() function and how to calculate the sin() of a number with a Python program.

What Is the Sin() Function in Python

The sin() function in Python is a built-in math function that provides the functionality of calculating the sine value of the argument passed to it in radian form. The sin() function can be easily used by simply importing the math module of Python into the program. First, import the math library and call the sin() function by the math.sin() statement. The syntax of the sin() function is math.sin(arg), where arg is the number of which you need to find the sine value.

How to Find Sin() in Python

The sin() function in Python calculates the sin() of a given number and returns the sin() value of that number. A number is passed as an argument in the sin() function. It simply calculates the sin() value of that argument and returns the calculated value. In simple words, it takes an “x” argument as a parameter and returns the sine value of the argument as a result in radian. The sin() function only takes the numeric data for any other data type it throws a TypeError. And it returns the sine value of the passed argument in the float data type. Now let’s see some examples to learn the implementation of the sin() function in Python.

Example 1:

In the first example, we will show you how to import the math function and how to call the sin() function and then calculate the sin() value of one numeric number. It is a quick start with the sin() function in Python. For further examples, we will deal with some complex numbers. See the following code:

import math

print(math.sin(1))

Cara menggunakan math.sin python

When you execute the previous code, you will get to see the following output:

Cara menggunakan math.sin python

Example 2:

Now, let’s explore some more functionality of the sin() function by giving it different numbers. You can also make your program fancy by adding some print commands, assigning the

numbers to variables and passing those variables as arguments to the sin() function.

import math

a = 0.4

b = -5

c = 2

d = -0.93

print("The sine value of ", a, "is = ", math.sin(a))

print("The sine value of ", b, " is ", math.sin(b))

print("The sine value of ", c, " is ", math.sin(c))

print("The sine value of ", d, " is ", math.sin(d))

Cara menggunakan math.sin python

Here is the output of the previous code:

Cara menggunakan math.sin python

Example 3:

In the previous two examples, we passed only the valid arguments. Now, let’s see what happens if we pass an invalid argument to the sin() function.

import math

a = 'Hi!'

print(math.cos(a))

Cara menggunakan math.sin python

Here is the output. Please note that the sin() function has thrown a TypeError as “Hi!”. It is not a valid numeric. As discussed previously, the sin() function only deals with numerics and throws a TypeError in case of non-numeric value.

Cara menggunakan math.sin python

Example 4:

The results in the previous examples are different from the ones which you find in the calculator. This is because the sin() function in Python returns the value in radian and the calculator returns the value in degrees. The math module of Python also provides the radian() function that converts the radian value of the sin() function into the degree. Let us see how to get the sine values in degrees. We use the same numbers as the input given in the previous examples so that you can see the difference:

import math

a = 0.4

b = -5

c = 2

d = -0.93

print("The sine value of ", a, "is = ", math.sin(math.radians(a)))

print("The sine value of ", b, " is ", math.sin(math.radians(b)))

print("The sine value of ", c, " is ", math.sin(math.radians(c)))

print("The sine value of ", d, " is ", math.sin(math.radians(d)))

Cara menggunakan math.sin python

Now, see the following output and observe the difference in the resultant values. You can compare the outputs side by side to see the difference in the resultant values.

Cara menggunakan math.sin python

Example 5:

As you can see in the previous examples, the output value is very long. You can round the number to the desired digit. For example, if you want to get the output limited to only 2 digits which can be achieved using the round() function.

import math

a = 0.4

b = -5

c = 2

d = -0.93

print("The sine value of ", a, "is = ", round(math.sin(math.radians(a)),2))

print("The sine value of ", b, " is ", round(math.sin(math.radians(b)),2))

print("The sine value of ", c, " is ", round(math.sin(math.radians(c)),2))

print("The sine value of ", d, " is ", round(math.sin(math.radians(d)),2))

Cara menggunakan math.sin python

Now, if you see the following output, the output number is returned in only 2 digits.

Cara menggunakan math.sin python

Example 6:

In all the previous examples, we found the sine values of arguments. Did you know that Python also provides the functionality of finding the inverse sine of a number? Well, the Python math module offers its user multiple trigonometric functions to find the inverse of the sin() function. For that matter, it provides an asin() function which calculates the inverse of the sine of a number. In the following illustration, we showed the code on how to find the inverse of the sin() function using the asin() function. See the following code:

import math

a = math.pi/3

print("The value of sine inverse is = ",math.asin(math.sin(a)))

print("The value of pi is = ",a)

Cara menggunakan math.sin python

The output of the previous code is given in the following illustration. To clearly show the function of the asin() function, we provided the original value of “pi” so that you can see the difference.

Cara menggunakan math.sin python

Conclusion

This article is a quick overview of the sin() function in Python. The math library in Python provides a variety of trigonometry functions that allows the user to perform any mathematical function in Python with ease. In this article, we explained the function of sin(), its syntax, it’s working, and how it can be easily implemented in Python using the math library. With the help of examples, we explained the use of the sin() function in Python. Make sure that you practice these examples to understand how the sin() function works in Python.

About the author

Hello, I am a freelance writer and usually write for Linux and other technology related content