- CodeCraft by Dr. Christine Lee
- Posts
- How Python Functions Can Return Multiple Values
How Python Functions Can Return Multiple Values
Fun with a Multi-Calculator!
Ever wondered how a magic box would work if you put in two numbers and it gave you all sorts of answers, like sum, difference, and even the product? Well, in Python, you can create such a magic box using functions that return multiple values. Let's dive into an easy-to-understand example with our multi-calculator function which does just that!
What is a Multi-Calculator Function?
Imagine you have a super calculator that can do many calculations at once. You give it two numbers, and it tells you their sum, difference, product, quotient, and more. In Python, we can create such a calculator using a function that returns multiple results.
Creating the multi_calculator
Function
Here’s how we can make a function named multi_calculator
that takes two numbers (`a` and b
) and returns their:
Sum (`+`)
Difference (`-`)
Product (`*`)
Quotient (`/`)
Power (`**`)
Integer division (`//`)
Remainder (`%`)
def multi_calculator(a, b):
return (a + b, a - b, a * b, a / b, a ** b, a // b, a % b)
This function uses Python’s ability to return a tuple (a type of container that holds multiple items) of results.
Using the Multi-Calculator
Let's see what happens when we input some numbers:
results = multi_calculator(10, 5)
print("Sum: ", results[0])
print("Difference: ", results[1])
print("Product: ", results[2])
print("Quotient: ", results[3])
print("Power: ", results[4])
print("Integer Division: ", results[5])
print("Remainder: ", results[6])
Here’s what you get:
- Sum: 15
- Difference: 5
- Product: 50
- Quotient: 2.0
- Power: 100000
- Integer Division: 2
- Remainder: 0
Multi Calculator Function
More Fun Examples
1. Weather Stats Function
Imagine a function that takes in temperatures for a day and gives you the highest, lowest, and average temperatures.
def weather_stats(temps):
max_temp = max(temps)
min_temp = min(temps)
avg_temp = sum(temps) / len(temps)
return max_temp, min_temp, avg_temp
daily_temps = [68, 70, 74, 69, 72]
stats = weather_stats(daily_temps)
print("High: ", stats[0], "Low: ", stats[1], "Average: ", round(stats[2], 1))
Weather Stats Function
2. School Grades Calculator
A function where you input scores and it tells you the highest score, lowest score, and the average.
def grade_stats(scores):
highest = max(scores)
lowest = min(scores)
average = sum(scores) / len(scores)
return highest, lowest, average
scores = [88, 92, 79, 85, 94]
results = grade_stats(scores)
print("Highest: ", results[0], "Lowest: ", results[1], "Average: ", round(results[2], 1))
School Grades Calculator Function
These examples show how versatile Python functions can be when returning multiple values. They make it easy to manage and use multiple pieces of data that come out of a single operation.
Get Started on Your Python Journey!
Want to become a Python wizard? Subscribe to our newsletter and get a free Python cheat sheet that will help you master Python faster and with more fun!
🌟 Subscribe Now and Get Your Free Python Cheat Sheet!
Join our community today, and let's make coding fun and easy to learn for everyone!