# How to calculate the cosine of an angle in python ?

Published: August 31, 2022

Updated: December 09, 2022

Tags: Python; Math;

Examples of how to get the cosine of an angle in python

### Using python math module

To calculate the cosine of an angle in python, a solution is to use the python math module:

````import math`

`angle = 0.7 # angle in radians`

`math.cos(angle)`
```

returns

````0.7648421872844885`
```

Another example

````angle = 0. # angle in radians`

`math.cos(angle)`
```

returns

````1.0`
```

Note to get pi number value:

````math.pi`
```

returns

````3.141592653589793`
```

Example:

````angle = math.pi`

`math.cos(angle)`
```

returns

````-1.0`
```

Or

````angle = math.pi / 2.0 # angle in radians`

`math.cos(angle)`
```

returns

````6.123233995736766e-17`
```

If angles are in degrees, it is possible to convert them in radians using math

````angle = 90`
`angle = math.radians(angle)`
```

returns

````1.5707963267948966`
```

and

````math.cos(angle)`
```

returns

````6.123233995736766e-17`
```

### Cosine for a list of angles

````angles = [0,math.pi/3.0,math.pi/2.0,math.pi]`
```

returns

````[0, 1.0471975511965976, 1.5707963267948966, 3.141592653589793]`
```

A solution is to use a list comprehension

````[math.cos(angle) for angle in angles]`
```

returns

````[1.0, 0.5000000000000001, 6.123233995736766e-17, -1.0]`
```

### Cosine for an array of angles

import numpy as np

````A = np.array([0, 1.0471975511965976, 1.5707963267948966, 3.141592653589793])`
```

returns

````array([0.        , 1.04719755, 1.57079633, 3.14159265])`
```

then

````np.cos(A)`
```

returns

````array([ 1.000000e+00,  5.000000e-01,  6.123234e-17, -1.000000e+00])`
```