Certainly! I'd be happy to create a small course on Python functions for high school freshmen. Let's break it down into manageable sections with examples and exercises.
Course: Introduction to Python Functions
Lesson 1: What is a Function?
A function is a reusable block of code that performs a specific task. It helps us organize our code and avoid repetition.
Example:
def greet():
print("Hello, world!")
greet() # Calling the functionExercise 1: Create a function called introduce_yourself that prints your name and age.
Lesson 2: Functions with Parameters
Functions can accept input values called parameters, which allow them to work with different data.
Example:
def greet_person(name):
print(f"Hello, {name}!")
greet_person("Alice")
greet_person("Bob")Exercise 2: Create a function called calculate_area that takes the length and width of a rectangle and prints its area.
Lesson 3: Return Values
Functions can send back results using the return statement.
Example:
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3)
print(result) # Output: 8Exercise 3: Create a function called celsius_to_fahrenheit that converts a temperature from Celsius to Fahrenheit and returns the result.
Lesson 4: Default Parameters
You can set default values for parameters, which are used when no argument is provided.
Example:
def greet_with_title(name, title="Mr."):
print(f"Hello, {title} {name}!")
greet_with_title("Smith")
greet_with_title("Johnson", "Dr.")Exercise 4: Create a function called power that takes two parameters: a number and an exponent (with a default value of 2). The function should return the number raised to the given exponent.
Lesson 5: Multiple Parameters and Return Values
Functions can have multiple parameters and return multiple values.
Example:
def calculate_stats(numbers):
total = sum(numbers)
average = total / len(numbers)
return total, average
sum_result, avg_result = calculate_stats([1, 2, 3, 4, 5])
print(f"Sum: {sum_result}, Average: {avg_result}")Exercise 5: Create a function called min_max that takes a list of numbers and returns both the minimum and maximum values from the list.
Final Project: Create a simple calculator program that uses functions for addition, subtraction, multiplication, and division. The program should:
- Ask the user to choose an operation
- Ask for two numbers
- Call the appropriate function
- Display the result
This course provides a basic introduction to Python functions with examples and exercises. Each lesson builds upon the previous one, gradually introducing more complex concepts. The final project allows s tudents to apply what they've learned in a practical scenario.