Python Math: Calculate the standard deviation
Python Math: Exercise-57 with Solution
Write a Python program to calculate the standard deviation of the following data.
import math import sys def sd_calc(data): n = len(data) if n <= 1: return 0.0 mean, sd = avg_calc(data), 0.0 # calculate stan. dev. for el in data: sd += (float(el) - mean)**2 sd = math.sqrt(sd / float(n-1)) return sd def avg_calc(ls): n, mean = len(ls), 0.0 if n <= 1: return ls # calculate average for el in ls: mean = mean + float(el) mean = mean / float(n) return mean data = [4, 2, 5, 8, 6] print("Sample Data: ",data) print("Standard Deviation : ",sd_calc(data))
Sample Data: [4, 2, 5, 8, 6] Standard Deviation : 2.23606797749979
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Given each iterable to construct a tuple by adding an index:
>>> a = ['Hello', 'world', '!'] >>> list(enumerate(a)) [(0, 'Hello'), (1, 'world'), (2, '!')]
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook