
Practice: Function Practice
Practice making functions using inputs and generating outputs.
Practice: Compute the Mean of a List of Numbers
-
Define a function called
mean_numbersthat takes one parameter:numbers, a list of numbers. -
Inside the function, calculate the sum of the numbers in the list and then divide it by the length of the list to get the mean. Return the result.
-
After the function definition, call your
mean_numbersfunction with the list[2, 4, 6, 8, 10]as an argument. Assign the return value to a variable namedresult. -
Print
result. -
Run your code and confirm that the output is
6.
HINT*: Click here for a hint
> 6
SOLUTION*: Click here for an example solution
def mean_numbers(numbers):
return sum(numbers) / len(numbers)
result = mean_numbers([2, 4, 6, 8, 10])
print(result)
Practice: Median Finder
-
Define a function called
find_medianthat takes one parameter: a list namednumbers. -
Inside the function, sort the numbers in ascending order. Note: You can implement your own sorting algorithm like bubble sort or use Python's built-in
sort()function. -
After sorting the list, find the median. If the length of the list is odd, the median is the number at the center of the list. If the length is even, the median is the average of the two center numbers.
-
Return the median.
-
After the function definition, call your
find_medianfunction with the list[6, 1, 2, 4, 5, 3]as an argument. Assign the return value to a variable namedmedian. -
Print
median. -
Run your code and confirm that the output is
3.5, which is the median of the list[6, 1, 2, 4, 5, 3].
HINT*: Click here for a hint
> 4
SOLUTION*: Click here for an example solution
def find_median(numbers):
numbers.sort()
n = len(numbers)
median = 0
if n % 2 == 0: # if the length of the list is even
median = (numbers[n//2 - 1] + numbers[n//2]) / 2
else: # if the length of the list is odd
median = numbers[n//2]
return median
median = find_median([6, 1, 2, 4, 5, 3])
print(median)
Practice: Categorize Numbers
-
Define a function called
categorize_numbersthat takes one parameter:numbers, a list of numbers. -
Inside the function, create two empty lists:
positive_numbersandnegative_numbers. Loop throughnumbersand append positive numbers topositive_numbersand negative numbers tonegative_numberswith the.append()function. Return a tuple containing both lists. -
After the function definition, call your
categorize_numbersfunction with the list[-1, 5, -3, 2, -8, 0, 7]as an argument. Assign the return value to a variable namedresult. -
Print
result. -
Run your code and confirm that the output is
([5, 2, 7], [-1, -3, -8]).
HINT*: Click here for a hint
> ([5, 2, 7], [-1, -3, -8])
SOLUTION*: Click here for an example solution
def categorize_numbers(numbers):
positive_numbers = []
negative_numbers = []
for num in numbers:
if num >= 0:
positive_numbers.append(num)
else:
negative_numbers.append(num)
return positive_numbers, negative_numbers
result = categorize_numbers([-1, 5, -3, 2, -8, 0, 7])
print(result)
Practice: Number Classification
-
Define a function called
classify_numberthat takes one parameter:number. -
Inside the function, if the
numberis greater than 0, return"Positive". If thenumberis less than 0, return"Negative". If thenumberequals 0, return"Zero". -
After the function definition, call your
classify_numberfunction with the number5as an argument. Assign the return value to a variable namedresult. -
Print
result. -
Run your code and confirm that the output is
"Positive". -
Repeat the previous steps, but this time with the number
-5. Confirm that the output is"Negative". -
Repeat once more, this time with the number
0. Confirm that the output is"Zero".
HINT*: Click here for a hint
> "Positive"
> "Negative"
> "Zero"
SOLUTION*: Click here for an example solution
def classify_number(number):
if number > 0:
return "Positive"
elif number < 0:
return "Negative"
else:
return "Zero"
result = classify_number(5)
print(result)
result = classify_number(-5)
print(result)
result = classify_number(0)
print(result)