# Python – math.cos() function

## Python | math.cos() function

In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. `math.cos()` function returns the cosine of value passed as argument. The value passed in this function should be in radians.

Syntax: math.cos(x)

Parameter:
x : value to be passed to cos()

Returns: Returns the cosine of value passed as argument

Code #1:

 `# Python code to demonstrate the working of cos()` `    ` `# importing "math" for mathematical operations ` `import` `math ` `   ` `a ``=` `math.pi ``/` `6` `    ` `# returning the value of cosine of pi / 6 ` `print` `(``"The value of cosine of pi / 6 is : "``, end ``=``"") ` `print` `(math.cos(a)) `

Output:

```The value of cosine of pi/6 is : 0.8660254037844387
```

Code #2:

 `# Python program showing ` `# Graphical representation of ` `# cos() function ` `import` `math` `import` `numpy as np` `import` `matplotlib.pyplot as plt ` ` ` `in_array ``=` `np.linspace(``-``(``2` `*` `np.pi), ``2` `*` `np.pi, ``20``)` ` ` `out_array ``=` `[]` ` ` `for` `i ``in` `range``(``len``(in_array)):` `    ``out_array.append(math.cos(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.cos()"``) ` `plt.xlabel(``"X"``) ` `plt.ylabel(``"Y"``) ` `plt.show() `

Output:

in_array : [-6.28318531 -5.62179738 -4.96040945 -4.29902153 -3.6376336 -2.97624567
-2.31485774 -1.65346982 -0.99208189 -0.33069396 0.33069396 0.99208189
1.65346982 2.31485774 2.97624567 3.6376336 4.29902153 4.96040945
5.62179738 6.28318531]

out_array : [1.0, 0.7891405093963934, 0.2454854871407988, -0.40169542465296987, -0.8794737512064891, -0.9863613034027223, -0.6772815716257412, -0.08257934547233249, 0.5469481581224268, 0.9458172417006346, 0.9458172417006346, 0.5469481581224268, -0.0825793454723316, -0.6772815716257405, -0.9863613034027223, -0.8794737512064893, -0.40169542465296987, 0.2454854871407988, 0.7891405093963934, 1.0]

Last Updated on November 1, 2021 by admin

## Numpy Meshgrid functionNumpy Meshgrid function

Numpy Meshgrid function The numpy.meshgrid function is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexing or Matrix indexing. Meshgrid function is somewhat inspired from MATLAB. Consider the above figure with X-axis ranging

## Multiplication of two Matrices in Single line using Numpy in PythonMultiplication of two Matrices in Single line using Numpy in Python

Multiplication of two Matrices in Single line using Numpy in Python Matrix multiplication is an operation that takes two matrices as input and produces single matrix by multiplying rows of the first matrix to the column of the second matrix.In

## numpy.argmax() in Pythonnumpy.argmax() in Python

numpy.argmax() in Python The numpy.argmax() function returns indices of the max element of the array in a particular axis. Syntax :  numpy.argmax(array, axis = None, out = None) Parameters :  array : Input array to work on axis : [int, optional]Along a

## Calculate the Euclidean distance using NumPyCalculate the Euclidean distance using NumPy

Calculate the Euclidean distance using NumPy In simple terms, Euclidean distance is the shortest between the 2 points irrespective of the dimensions. In this article to find the Euclidean distance, we will use the NumPy library. This library used for manipulating multidimensional

## numpy.linspace() in Pythonnumpy.linspace() in Python

numpy.linspace() in Python The numpy.linspace() function returns number spaces evenly w.r.t interval. Similar to numpy.arrange() function but instead of step it uses sample number. Syntax :  numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None) Parameters :  -> start

## How to select rows from a dataframe based on column values ?How to select rows from a dataframe based on column values ?

How to select rows from a dataframe based on column values ? The rows of a dataframe can be selected based on conditions as we do use the SQL queries. The various methods to achieve this is explained in this

## numpy.mean() in Pythonnumpy.mean() in Python

numpy.mean() in Python numpy.mean(arr, axis = None) : Compute the arithmetic mean (average) of the given data (array elements) along the specified axis. Parameters : arr : [array_like]input array. axis : [int or tuples of int]axis along which we want to calculate the arithmetic

## numpy.log() in Pythonnumpy.log() in Python

numpy.log() in Python The numpy.log() is a mathematical function that helps user to calculate Natural logarithm of x where x belongs to all the input array elements. Natural logarithm log is the inverse of the exp(), so that log(exp(x)) = x. The natural logarithm is log