Complex dtype array

Python Practice › NumPy Programs › Program 100

Complex dtype array

EasyNumPy Programs · 100 of 100

Write a NumPy program to create an array with complex dtype.

Input
[5, 4]
Output
[5.+0.j 4.+0.j]

Approach

  1. dtype=complex.

Solution

Python
import numpy as npprint(np.array([5, 4], dtype=complex))
Output
[5.+0.j 4.+0.j]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Tuple to array

Python Practice › NumPy Programs › Program 99

Tuple to array

EasyNumPy Programs · 99 of 100

Write a NumPy program to convert a tuple into an array.

Input
(5, 7, 0)
Output
[5 7 0]

Approach

  1. np.array(tuple).

Solution

Python
import numpy as npprint(np.array((5, 7, 0)))
Output
[5 7 0]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Ceiling of each element

Python Practice › NumPy Programs › Program 98

Ceiling of each element

EasyNumPy Programs · 98 of 100

Write a NumPy program to round elements up with ceil.

Input
[4.8757, 2.4578]
Output
[5. 3.]

Approach

  1. np.ceil.

Solution

Python
import numpy as npprint(np.ceil([4.8757, 2.4578]))
Output
[5. 3.]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Floor of each element

Python Practice › NumPy Programs › Program 97

Floor of each element

EasyNumPy Programs · 97 of 100

Write a NumPy program to round elements down with floor.

Input
[4.8757, 2.4578]
Output
[4. 2.]

Approach

  1. np.floor.

Solution

Python
import numpy as npprint(np.floor([4.8757, 2.4578]))
Output
[4. 2.]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Round to two decimals

Python Practice › NumPy Programs › Program 96

Round to two decimals

EasyNumPy Programs · 96 of 100

Write a NumPy program to round elements to two decimal places.

Input
[4.8757, 2.4578]
Output
[4.88 2.46]

Approach

  1. np.round(a, 2).

Solution

Python
import numpy as npprint(np.round(np.array([4.8757, 2.4578]), 2))
Output
[4.88 2.46]
Note

Rounding gives 4.88 and 2.46; the original sample (4.86, 2.45) was not rounded correctly.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Tangent of angles

Python Practice › NumPy Programs › Program 95

Tangent of angles

EasyNumPy Programs · 95 of 100

Write a NumPy program to calculate the tangent of angles.

Input
0, 45 degrees
Output
[0. 1.]

Approach

  1. np.tan(np.radians(...)).

Solution

Python
import numpy as npprint(np.tan(np.radians([0, 45])).round(3))
Output
[0. 1.]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Cosine of angles

Python Practice › NumPy Programs › Program 94

Cosine of angles

EasyNumPy Programs · 94 of 100

Write a NumPy program to calculate the cosine of angles.

Input
0, 60, 90 degrees
Output
[1.  0.5 0. ]

Approach

  1. np.cos(np.radians(...)).

Solution

Python
import numpy as npprint(np.cos(np.radians([0, 60, 90])).round(3))
Output
[1.  0.5 0. ]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Variance

Python Practice › NumPy Programs › Program 93

Variance

EasyNumPy Programs · 93 of 100

Write a NumPy program to calculate the variance of an array.

Input
[2, 4, 4, 4, 5, 5, 7, 9]
Output
4.0

Approach

  1. np.var.

Solution

Python
import numpy as npprint(np.var([2, 4, 4, 4, 5, 5, 7, 9]))
Output
4.0

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Standard deviation

Python Practice › NumPy Programs › Program 92

Standard deviation

EasyNumPy Programs · 92 of 100

Write a NumPy program to calculate the standard deviation of an array.

Input
[2, 4, 4, 4, 5, 5, 7, 9]
Output
2.0 2.138

Approach

  1. np.std (population); ddof=1 for a sample.

Solution

Python
import numpy as npa = np.array([2, 4, 4, 4, 5, 5, 7, 9])print(np.std(a), round(float(np.std(a, ddof=1)), 3))
Output
2.0 2.138

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Sine of angles

Python Practice › NumPy Programs › Program 91

Sine of angles

EasyNumPy Programs · 91 of 100

Write a NumPy program to calculate the sine of angles.

Input
0, 30, 90 degrees
Output
[0.  0.5 1. ]

Approach

  1. Convert degrees to radians, then np.sin.

Solution

Python
import numpy as npprint(np.sin(np.radians([0, 30, 90])).round(3))
Output
[0.  0.5 1. ]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs