# Python NumPy: Math, Statistics, Trigonometry and Linear algebra Exercises, Practice, Solution

## Python NumPy Math, Statistics, Trigonometry and Linear algebra [21 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a Python program to add, subtract, multiply, divide arguments element-wise. Go to the editor
Expected Output:
5.0
Subtract:
-3.0
Multiply:
4.0
Divide:
0.25
Click me to see the sample solution

2. Write a Python program to compute logarithm of the sum of exponentiations of the inputs, sum of exponentiations of the inputs in base-2. Go to the editor
Expected Output:
Logarithm of the sum of exponentiations:
-113.876491681
Logarithm of the sum of exponentiations of the inputs in base-2:
-113.599555228
Click me to see the sample solution

3. Write a Python program to get true division of the element-wise array inputs. Go to the editor
Expected Output:
Original array:
[0 1 2 3 4 5 6 7 8 9]
Division of the array inputs, element-wise:
[ 0. 0.33333333 0.66666667 1. 1.33333333 1.6666666
7 2. 2.33333333 2.66666667 3. ]
Click me to see the sample solution

4. Write a Python program to get the largest integer smaller or equal to the division of the inputs. Go to the editor
Expected Output:
Original array:
[1.0, 2.0, 3.0, 4.0]
Largest integer smaller or equal to the division of the inputs:
[ 0. 1. 2. 2.]
Click me to see the sample solution

5. Write a Python program to get the powers of an array values element-wise. Go to the editor
Note: First array elements raised to powers from second array
Expected Output:
Original array
[0 1 2 3 4 5 6]
First array elements raised to powers from second array, element-wise:
[ 0 1 8 27 64 125 216]
Click me to see the sample solution

6. Write a Python program to get the element-wise remainder of an array of division. Go to the editor
Sample Output:
Original array:
[0 1 2 3 4 5 6]
Element-wise remainder of division:
[0 1 2 3 4 0 1]
Click me to see the sample solution

7. Write a Python program to calculate the absolute value element-wise. Go to the editor
Sample output:
Original array:
[ -10.2 122.2 0.2]
Element-wise absolute value:
[ 10.2 122.2 0.2]
Click me to see the sample solution

8. Write a Python program to round array elements to the given number of decimals. Go to the editor
Sample Output:
[ 1. 2. 2.]
[ 0.3 0.5 0.6]
[ 0. 2. 2. 4. 4.]
Click me to see the sample solution

9. Write a Python program to round elements of the array to the nearest integer. Go to the editor
Sample Output:
Original array:
[-0.7 -1.5 -1.7 0.3 1.5 1.8 2. ]
Round elements of the array to the nearest integer:
[-1. -2. -2. 0. 2. 2. 2.]
Click me to see the sample solution

10. Write a Python program to get the floor, ceiling and truncated values of the elements of an numpy array. Go to the editor
Sample Output:
Original array:
[-1.6 -1.5 -0.3 0.1 1.4 1.8 2. ]
Floor values of the above array elements:
[-2. -2. -1. 0. 1. 1. 2.]
Ceil values of the above array elements:
[-1. -1. -0. 1. 2. 2. 2.]
Truncated values of the above array elements:
[-1. -1. -0. 0. 1. 1. 2.]
Click me to see the sample solution

11. Write a Python program to multiply a 5x3 matrix by a 3x2 matrix and create a real matrix product. Go to the editor
Sample output:
First array:
[[ 0.44349753 0.81043761 0.00771825]
[ 0.64004088 0.86774612 0.19944667]
[ 0.61520091 0.24796788 0.93798297]
[ 0.22156999 0.61318856 0.82348994]
[ 0.91324026 0.13411297 0.00622696]]
Second array:
[[ 0.73873542 0.06448186]
[ 0.90974982 0.06409165]
[ 0.22321268 0.39147412]]
Dot product of two arrays:
[[ 1.06664562 0.08356133]
[ 1.30677176 0.17496452]
[ 0.88942914 0.42275803]
[ 0.90534318 0.37596252]
[ 0.79804212 0.06992065]]
Click me to see the sample solution

12. Write a Python program to multiply a matrix by another matrix of complex numbers and create a new matrix of complex numbers. Go to the editor
Sample output:
First array:
[ 1.+2.j 3.+4.j]
Second array:
[ 5.+6.j 7.+8.j]
Product of above two arrays:
(70-8j)
Click me to see the sample solution

13. Write a Python program to create an inner product of two arrays. Go to the editor
Sample Output:
Array x:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
Array y:
[0 1 2 3]
Inner of x and y arrays:
[[ 14 38 62]
[ 86 110 134]]
Click me to see the sample solution

14. Write a Python program to generate inner, outer, and cross products of matrices and vectors. Go to the editor
Expected Output:
Matrices and vectors.
x:
[ 1. 4. 0.]
y:
[ 2. 2. 1.]
Inner product of x and y:
10.0
Outer product of x and y:
[[ 2. 2. 1.]
[ 8. 8. 4.]
[ 0. 0. 0.]]
Cross product of x and y:
[ 4. -1. -6.]
Click me to see the sample solution

15. Write a Python program to generate a matrix product of two arrays. Go to the editor
Sample Output:
Matrices and vectors.
x:
[[1, 0], [1, 1]]
y:
[[3, 1], [2, 2]]
Matrix product of above two arrays:
[[3 1]
[5 3]]
Click me to see the sample solution

16. Write a Python program to find the roots of the following polynomials. Go to the editor
a) x2 - 4x + 7.
b) x4 - 11x3 + 9x2 + 11x ? 10
Sample output:
Roots of the first polynomial:
[ 1. 1.]
Roots of the second polynomial:
[ 11.04461946+0.j -0.87114210+0.j 0.91326132+0.4531004j
0.91326132-0.4531004j]
Click me to see the sample solution

17. Write a Python program to compute the following polynomial values. Go to the editor
Sample output:
Polynomial value when x = 2:
1
Polynomial value when x = 3:
-142
Click me to see the sample solution

18. Write a Python program to add one polynomial to another, subtract one polynomial from another, multiply one polynomial by another and divide one polynomial by another. Go to the editor
Sample output:
[ 40. 60. 80.]
Subtract one polynomial from another:
[-20. -20. -20.]
Multiply one polynomial by another:
[ 300. 1000. 2200. 2200. 1500.]
Divide one polynomial by another:
(array([ 0.6]), array([-8., -4.]))
Click me to see the sample solution

19. Write a Python program to calculate mean across dimension, in a 2D numpy array. Go to the editor
Sample output:
Original array:
[[10 30]
[20 60]]
Mean of each row:
[ 15. 45.]
Mean of each column:
[ 20. 40.]
Click me to see the sample solution

20. Write a Python program to create a random array with 1000 elements and compute the average, variance, standard deviation of the array elements. Go to the editor
Sample output:
Average of the array elements:
-0.0255137240796
Standard deviation of the array elements:
0.984398282476
Variance of the array elements:
0.969039978542
Click me to see the sample solution

21. Write a Python program to compute the trigonometric sine, cosine and tangent array of angles given in degrees. Go to the editor
Sample output:
sine: array of angles given in degrees
[ 0. 0.5 0.70710678 0.8660254 1. ]
cosine: array of angles given in degrees
[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
tangent: array of angles given in degrees
[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
Click me to see the sample solution

Python Code Editor: