#๐Ÿ”’ Howbto prevent this code redundation

6 messages ยท Page 1 of 1 (latest)

opaque garnet
#
import pandas as pd
import math
import matplotlib.pyplot as plt

world_population_data = pd.read_csv('world_population.csv')

def standard_deviation(data: list):
    
    if len(data) < 2:
        raise ValueError('Standard deviation requires atleast 2 data points')
    
    # s = sqrt((actual_value - mean_value)ยฒ / total values)
    mean_of_data = (sum(data)/len(data))
    
    deviation = sum((x - mean_of_data)**2 for x in data)
    
    std = math.sqrt(deviation/len(data))
    return std

years = [1970, 1980, 1990, 2000, 2010, 2015, 2020, 2022]

std_years = [standard_deviation(list(world_population_data['1970 Population'])),

standard_deviation(list(world_population_data['1980 Population'])),

standard_deviation(list(world_population_data['1990 Population'])),

standard_deviation(list(world_population_data['2000 Population'])),

standard_deviation(list(world_population_data['2010 Population'])),

standard_deviation(list(world_population_data['2015 Population'])),

standard_deviation(list(world_population_data['2020 Population'])),

standard_deviation(list(world_population_data['2022 Population']))]

plt.plot(years, std_years)
plt.grid(True)
plt.savefig('Standard deviation in World Population(1970-2022).png')
plt.show()

i want to prevent the code redundation in the std_years

naive onyxBOT
#

@opaque garnet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

opaque garnet
#

i fixed it ๐Ÿ™‚

#

!close

naive onyxBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.