In this article, we will see how to get the max of two numbers in Python. We have four possible ways to get a maximum of two numbers. We will learn all four methods one by one. I hope you all know that the built-in function is available to get a maximum of two numbers in python. But as python programmers, we have to know the possible ways to do a program without a built-in function. We will also learn about the built-in function and some general methods without using the built-in function. Let us start.
Maximum between two numbers is calculated in python using four different methods. The first one is using a conditional statement, if-else condition to check the maximum element. Secondly, using a built-in function, max(), to get the largest element. Thirdly, by using the ternary operator. And the last method is the lambda function.
Method 1: Using if-else statement to get max of two numbers in python
if-else is one of the decision-making statements in python. It will check the given condition. If the condition is satisfied, it will return the if part statement. Otherwise, it will return else part statement.
Code
a=int(input("Enter a number: "))
b=int(input("Enter a number: "))
if a>b:
print(f'{a} is a maximum number')
else:
print(f'{b} is a maximum number')
This program will get the two inputs from the user to get a maximum element from the given inputs. In an, if statement we are giving a condition like if a>b, print this statement. If not, print the else statement. So it will take the input and print the result after checking with a given condition.
Output
Enter a number: 3 Enter a number: 8 8 is a maximum number
Method 2: Using the max function to get max of two numbers in python
max is a built-in function in python, and that is useful to get the maximum element from the given elements.
Syntax
max(element1,element2)
Parameters
- element1: first element
- element2: second element
Returns
maximum element
Code
def maximum(a,b):
num=max(a,b)
return num
a=50
b=30
print(f'{maximum(a,b)} is a maximum number')
Creating a function named maximum, and the parameters are a and b. Using a max function to get the larger element. Giving a value of a and b. I always prefer the formatted function to print the result to understand what the program performs without seeing the code.
Output
50 is a maximum number
Method 3: Using the ternary operator to get max of two numbers in python
The ternary operator is also known as the conditional operator. This operator is useful to check the condition in a single line.
Syntax
[on_true] if [expression] else [on_false]
Code
a=int(input("Enter a number: "))
b=int(input("Enter a number: "))
print(f'{a if a >= b else b} is a maximum number')
First, get the inputs from the user. Using the ternary operator to get the maximum element from the given numbers.
Output
Enter a number: 40 Enter a number: 75 75 is a maximum number
Method 4: Using lambda function to get max of two numbers in python
A lambda is an anonymous function, and it takes any number of arguments. But the expression is only one.
Syntax
lambda arguments : expression
Code
a=int(input("Enter a number: "))
b=int(input("Enter a number: "))
maximum = lambda a,b:a if a > b else b
print(f'{maximum(a,b)} is a maximum number')
First, get the inputs from the user. Using the lambda function to get the maximum element from the given numbers.
Output
Enter a number: 34 Enter a number: 98 98 is a maximum number
Bonus: How to get a maximum between three numbers
def maximum(a,b,c):
num=max(a,b,c)
return num
a=50
b=30
c=80
print(f'{maximum(a,b,c)} is a maximum number')
Creating a function named maximum and the parameters are a, b, and c. Using a max function to get the larger element. Giving a value of a, b, and c. I always prefer the formatted function to print the result to understand what the program performs without seeing the code.
Output
80 is a maximum number
FAQs Related to get Max of two Numbers in Python
The conditional operator is introduced in a version of 2.5
Lambda function takes only one expression.
Conclusion
Here we came to the end of the article. Now we have completely learned about how to get the maximum elements from the given two numbers. We learned all the possible ways to get the maximum element in python.
We hope this article is easy to understand. In case of any queries, feel free to ask in the comment section.
There is a 5th method:
a=50;b=30;max=[a,b][b>a] ;print(f'{max} is a maximum number’)
Great, that’s one way of doing it!