# Write a Python Function to Find the Max of Three Numbers

In this Python tutorial, you will learn how to write a Python function to find the max of three numbers using different approaches with practical examples.

While working on a project, there was a quiz where 3 people could compete at a time, so I needed to display the winner who scored more. So, in this case, I have to find the highest score from all the users.

In this situation, you can use all our approaches to find the maximum of three numbers in Python.

## How to Find Max of Three Numbers in Python

Let’s understand the scenario with a practical example. Suppose there is a competition, and these three people scored like this,

``````peter = 89
john = 78
jack = 92``````

Now, I need to find the highest score from these three players and need to show output like this,

``Highest Score : 92``

Let’s see how we can create a function to get this type of output.

### Write a Python Function to Find the Max of Three Numbers using if-elif-else

First, we will use if-elif-else to find the max value from three numbers in Python. These are conditional statements in Python, which execute a block of code based on the condition.

Here, we need to find the max value from only three numbers, so we can use this approach; otherwise, you can take another approach.

READ:  dot product and cross product in Python

Syntax

``````if <condition1>:
<code to be execute>
elif <condition2>:
<code>
else:
<code>``````
• if <conditions>: If the first condition gets true, it will execute that code block and not check for other conditions.
• elif <condition2>: You can give multiple elifs in Python for multiple conditions in Python.
• else: If none of the conditions are satisfied, then the code block will execute.

Let’s see how we can use conditional statements in the Python function to find the max of three numbers.

``````def find_max(n1, n2, n3):
if n1 > n2 and n1 > n3:
print(f"Highest score is: {n1}")
elif n2 > n3:
print(f"Highest score is: {n2}")
else:
print(f"Highest score is: {n3}")

peter = 89
john = 78
jack = 92

find_max(peter, john, jack)``````

In the above code, we have the scores of three players and we need to display the highest score. So we created a function that takes three parameters, “def find_max(n1, n2, n3)“, and then we start comparing the value using comparison operators like this: “ if n1 > n2 and n1 > n3“.

Also, we used an operator in the program, so if both conditions are true, only the if statement block will be executed; otherwise, it will check the other conditions.

### Write a Python Function to Find the Maximum of Three Numbers Using the max() Function

We can also use the max() method, a built-in method in Python, to find the largest number from the collection. So, we will return the max() method in our user-defined function.

Syntax

``max(collection)``

Let’s see how we can create a Python function to find the max of three numbers

``````def find_max(n1, n2, n3):
return max(n1, n2, n3)

day_1 = 5000
day_2 = 8000
day_3 = 4500

highest_sales = find_max(day_1, day_2, day_3)

print("Maximum Sales is:", highest_sales)``````

In the above code, first, we created a function named find_max(), and it will take three values as a parameter. And it will directly return the max() method like this
return max(n1, n2, n3).

READ:  How to Use Switch Case in Python with User Input[5 ways]

### Python Program to Find Largest of 3 Numbers Using Function with For Loop

Here, we will use a loop and comparison operator to get the largest value from the collection. The for loop will iterate over every value individually and compare it with the max value using the greater than operator ( > ).

``````def find_max(n1, n2, n3):
li = [n1, n2, n3]
max = 0
for i in li:
if i > max:
max = i
return max

sale_1 = 1200
sale_2 = 1800
sale_3 = 2500

highest_sale = find_max(sale_1, sale_2, sale_3)

print("Highest Sale:", highest_sale)``````

In the above code, first, we created a list of three parameters and initialized max = 0. Then, for loop will iterate values of the list like this: “for i in li“, and using a conditional statement like this: if i > max which means, if the “i” is greater than “max” then change the value of max = i.

## Conclusion

In this Python tutorial, you learned how to use Python function to find the maximum of three numbers in different ways, such as using a conditional statement, the max() method, and the for loop to get the expected output.