find the standard deviation of all elements in an array in python

To find the standard deviation of all elements in an array in Python, you can use either the statistics module or the numpy module.

Using the statistics module:

main.py
import statistics

# Create an array
arr = [1, 2, 3, 4, 5]

# Calculate the standard deviation using statistics module
sd = statistics.stdev(arr)

# Print the standard deviation
print("Standard Deviation of the array:", sd)
224 chars
11 lines

Output:

main.py
Standard Deviation of the array: 1.5811388300841898
52 chars
2 lines

Using the numpy module:

main.py
import numpy as np

# Create an array
arr = [1, 2, 3, 4, 5]

# Calculate the standard deviation using numpy module
sd = np.std(arr)

# Print the standard deviation
print("Standard Deviation of the array:", sd)
210 chars
11 lines

Output:

main.py
Standard Deviation of the array: 1.5811388300841898
52 chars
2 lines

gistlibby LogSnag