In this Python tutorial, we will learn the Python function to find the maximum of three numbers. We will cover two different ways to find out the maximum of three numbers.
A function in Python is a block of reusable code that performs a specific task. Functions help break down our code into bite-sized pieces that we can reuse across our program.
In Python, a function is defined using the
def keyword followed by a unique function name, parentheses
(), and a colon
:. The code block within every function is indented for readability and consistency.
Python Function to find max of three numbers using max()
Our goal is to write a Python function that can take three numbers (like player scores) as input and return the maximum of those three numbers (the highest score).
To accomplish this, we will use Python’s built-in max() function, a handy tool that returns the largest item in an iterable or the largest of two or more arguments.
Here’s the Python function code:
def find_max(a, b, c): return max(a, b, c)
In the function find_max(), a, b, and c are our inputs, like player scores in a basketball game. When we call this Python function with three arguments, it will return the largest number, akin to identifying the highest-scoring player.
Now, let’s test this function and look how its works.
print(find_max(23, 34, 29)) print(find_max(45, 33, 47)) print(find_max(12, 12, 11))
Here, the numbers within the parentheses are our arguments, the player scores. In the first line, 23, 34, and 29 are the arguments. The find_max() function then returns 34, which is the highest score, and this result is printed on the console. Similarly for other input its work.
Maximum of three numbers in Python without using max() Function
While Python’s built-in max() function provides an easy way to find the maximum of three numbers, it’s also important to understand how to manually determine this. It’s like calculating the highest score without a calculator.
Here’s how you might write this function without using the max() function:
def find_max(a, b, c): if (a >= b) and (a >= c): # If 'a' is greater than or equal to both 'b' and 'c' return a elif (b >= a) and (b >= c): # If 'b' is greater than or equal to both 'a' and 'c' return b else: return c print(find_max(10, 15, 12)) print(find_max(-5, -3, -7)) print(find_max(0, 0, 0))
In this function, we use Python’s comparison operators and control flow (specifically, if and elif statements) to find the maximum of the three numbers.
With this, we have understood how to write a Python function to find the maximum of three numbers, just like identifying the highest-scoring player in a basketball game.
We looked at the direct approach using Python’s built-in max() function and a manual way that uses comparison and control flow.
You may like the following Python tutorials:
- Python convert dictionary to list
- Python while loop continue
- Python sort list of tuples
- The max() function in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.