एक फंक्शन को बाँटें
आपकी टीम के एक अन्य इंजीनियर ने एक sorted लिस्ट का mean और median निकालने के लिए यह फंक्शन लिखा है. आप उन्हें दिखाना चाहते हैं कि इसे दो सरल फंक्शनों में कैसे बाँटा जाए: mean() और median()
def mean_and_median(values):
"""Get the mean and median of a sorted list of `values`
Args:
values (iterable of float): A list of numbers
Returns:
tuple (float, float): The mean and median
"""
mean = sum(values) / len(values)
values = sorted(values)
midpoint = int(len(values) / 2)
if len(values) % 2 == 0:
median = (values[midpoint - 1] + values[midpoint]) / 2
else:
median = values[midpoint]
return mean, median
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Functions लिखना
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
def mean(values):
"""Get the mean of a sorted list of values
Args:
values (iterable of float): A list of numbers
Returns:
float
"""
# Write the mean() function
____ = ____
return mean