Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Practice: Function Practice

check

Practice making functions using inputs and generating outputs.


Practice: Compute the Mean of a List of Numbers

  1. Define a function called mean_numbers that takes one parameter: numbers, a list of numbers.

  2. 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.

  3. After the function definition, call your mean_numbers function with the list [2, 4, 6, 8, 10] as an argument. Assign the return value to a variable named result.

  4. Print result.

  5. Run your code and confirm that the output is 6.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> 6
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
def mean_numbers(numbers):
    return sum(numbers) / len(numbers)
result = mean_numbers([2, 4, 6, 8, 10])
print(result)

Practice: Median Finder

  1. Define a function called find_median that takes one parameter: a list named numbers.

  2. 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.

  3. 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.

  4. Return the median.

  5. After the function definition, call your find_median function with the list [6, 1, 2, 4, 5, 3] as an argument. Assign the return value to a variable named median.

  6. Print median.

  7. 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
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> 4
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
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

  1. Define a function called categorize_numbers that takes one parameter: numbers, a list of numbers.

  2. Inside the function, create two empty lists: positive_numbers and negative_numbers. Loop through numbers and append positive numbers to positive_numbers and negative numbers to negative_numbers with the .append() function. Return a tuple containing both lists.

  3. After the function definition, call your categorize_numbers function with the list [-1, 5, -3, 2, -8, 0, 7] as an argument. Assign the return value to a variable named result.

  4. Print result.

  5. Run your code and confirm that the output is ([5, 2, 7], [-1, -3, -8]).

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> ([5, 2, 7], [-1, -3, -8])
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
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

  1. Define a function called classify_number that takes one parameter: number.

  2. Inside the function, if the number is greater than 0, return "Positive". If the number is less than 0, return "Negative". If the number equals 0, return "Zero".

  3. After the function definition, call your classify_number function with the number 5 as an argument. Assign the return value to a variable named result.

  4. Print result.

  5. Run your code and confirm that the output is "Positive".

  6. Repeat the previous steps, but this time with the number -5. Confirm that the output is "Negative".

  7. Repeat once more, this time with the number 0. Confirm that the output is "Zero".

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> "Positive"
> "Negative"
> "Zero"
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
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)