Write a Python Function to Find the Maximum of Three Numbers 

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.

write a python function to find the maximum of three numbers

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.

maximum of three numbers in python

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.

max of three numbers in python

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.

find the maximum of three numbers

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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.