Recently, while working on a small Python project for a quiz app, I needed to determine which of three players had the highest score.
It sounded simple, but I wanted a clean, reusable Python function that could find the maximum of three numbers, efficiently and clearly.
After more than a decade of working with Python, Iāve realized that even basic problems like this can be solved in multiple elegant ways. In this tutorial, Iāll walk you through different methods to find the maximum of three numbers in Python, from the simplest built-in function to more customized logic using conditionals.
Understand the Problem
Before writing any Python code, letās understand what we need to achieve. We have three numbers, letās call them a, b, and c.
Our goal is to determine which one is the largest. This is a basic comparison problem, but in Python, there are several ways to handle it.
Method 1 ā Use the Built-in max() Function in Python
When it comes to finding the maximum value, Python already provides a built-in function called max(). Itās simple, efficient, and works perfectly for this task.
Hereās how I use it in a Python function:
def find_max_of_three(a, b, c):
return max(a, b, c)
# Example usage
num1 = 45
num2 = 67
num3 = 23
result = find_max_of_three(num1, num2, num3)
print(f"The maximum of {num1}, {num2}, and {num3} is {result}")
I executed the above example code and added the screenshot below.
In this example, the max() function automatically compares all three numbers and returns the largest one. Itās the most efficient way to handle this problem, clean, readable, and efficient.
If youāre working on a real-world Python project (like comparing sales data or test scores), this method is often the best choice. However, understanding the logic behind it helps you write more flexible code.
Method 2 ā Use Conditional Statements in Python
Sometimes, you may want to avoid built-in functions, for example, in coding interviews or when teaching Python fundamentals. In such cases, you can use simple if-elif-else statements to compare numbers manually.
Hereās how I do it:
def find_max_of_three(a, b, c):
if (a >= b) and (a >= c):
return a
elif (b >= a) and (b >= c):
return b
else:
return c
# Example usage
num1 = 12
num2 = 89
num3 = 56
result = find_max_of_three(num1, num2, num3)
print(f"The maximum of {num1}, {num2}, and {num3} is {result}")
I executed the above example code and added the screenshot below.
This function checks each condition step by step to determine which number is the greatest. Itās a bit longer than the max() approach, but it gives you full control over the logic.
I often use this method in Python training sessions because it helps beginners understand how comparisons work internally. Itās also useful when you need to add extra conditions or logging later.
Method 3 ā Use Pythonās Ternary Operator (One-Liner)
If you love writing concise Python code, this method is for you.
Pythonās ternary operator allows you to perform conditional checks in a single line.
Hereās how you can use it:
def find_max_of_three(a, b, c):
return a if (a >= b and a >= c) else (b if b >= c else c)
# Example usage
num1 = 101
num2 = 99
num3 = 120
result = find_max_of_three(num1, num2, num3)
print(f"The maximum of {num1}, {num2}, and {num3} is {result}")
I executed the above example code and added the screenshot below.
This one-liner function is compact yet powerful. It works perfectly when you need a quick comparison without writing multiple if statements.
I often use this style in small Python scripts or when writing quick automation tools.
However, readability should always be your priority, so use it wisely.
Method 4 ā Take User Input in Python
In many Python applications, youāll want users to enter their own numbers. Letās modify our function to accept user input dynamically.
Hereās how I handle it:
def find_max_of_three(a, b, c):
return max(a, b, c)
# Taking input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
result = find_max_of_three(num1, num2, num3)
print(f"The maximum of {num1}, {num2}, and {num3} is {result}")
This version allows users to type three numbers directly into the console. Itās great for interactive Python programs or simple command-line utilities.
I often use this approach when building quick prototypes or testing logic interactively. It makes your Python function more flexible and user-friendly.
Method 5 ā Use Pythonās List and max() Together
Hereās another creative way to find the maximum of three numbers using a list. This method is especially useful when the number of inputs might change later.
def find_max_of_three(a, b, c):
numbers = [a, b, c]
return max(numbers)
# Example usage
num1 = 34
num2 = 78
num3 = 56
result = find_max_of_three(num1, num2, num3)
print(f"The maximum of {num1}, {num2}, and {num3} is {result}")
By storing the values in a list, you make your code more scalable. If later you want to compare more than three numbers, you can easily expand the list.
This approach is one I often use when dealing with Python data structures like lists or tuples. Itās clean, adaptable, and leverages Pythonās built-in power.
Bonus Tip ā Find the Maximum Without Using max() or Conditionals
For those who love mathematical tricks, hereās a fun one. You can use a combination of arithmetic and logic to find the maximum number.
def find_max_of_three(a, b, c):
max_ab = (a + b + abs(a - b)) / 2
max_abc = (max_ab + c + abs(max_ab - c)) / 2
return max_abc
# Example usage
num1 = 10
num2 = 25
num3 = 15
result = find_max_of_three(num1, num2, num3)
print(f"The maximum of {num1}, {num2}, and {num3} is {result}")
I executed the above example code and added the screenshot below.
This method uses absolute value (abs()) to compare numbers mathematically. Itās not the most practical for everyday Python coding, but itās a clever trick worth knowing.
Iāve used this approach in algorithmic challenges where built-in functions were restricted. Itās a great exercise in understanding how comparisons can be expressed mathematically.
Real-World Example ā Compare Sales Figures in Python
Letās apply what weāve learned to a real-world example. Imagine youāre analyzing monthly sales data for three U.S. stores, New York, Chicago, and Los Angeles.
Hereās how you can find which store performed the best:
def find_max_of_three(a, b, c):
return max(a, b, c)
ny_sales = 45200
chicago_sales = 38900
la_sales = 51200
top_sales = find_max_of_three(ny_sales, chicago_sales, la_sales)
if top_sales == ny_sales:
city = "New York"
elif top_sales == chicago_sales:
city = "Chicago"
else:
city = "Los Angeles"
print(f"The highest sales were in {city} with ${top_sales:,}")
This example shows how a simple Python function can make real business insights easier to extract. I often use similar logic when analyzing datasets or generating quick performance summaries.
Best Practices for Writing Python Functions
Here are a few quick tips from my experience to make your Python functions more effective:
- Keep them simple: Each function should do one thing well.
- Use descriptive names: Names like find_max_of_three clearly describe the purpose.
- Add comments: Explain your logic, especially when using conditionals or math tricks.
- Test your code: Try different combinations of numbers (including negatives and floats).
Following these habits helps you write cleaner, more maintainable Python code.
Itās something Iāve learned after years of debugging and refactoring real-world projects.
Conclusion
Finding the maximum of three numbers in Python may sound simple, but itās a great exercise in writing clean, reusable code. We explored multiple methods, from using Pythonās max() function to writing our own logic with conditionals and even mathematical formulas.
Each approach has its place, depending on your needs and coding style. If youāre working on real-world Python projects, the built-in max() function is usually the most efficient choice.
You may also like to read:
- Remove a Key Without an Error in the Python Dictionary
- Python Dictionary KeyError: None
- Python Dictionary Key Error
- Python Dictionary Comprehension
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay Ā· Microsoft MVP profile Ā· LinkedIn