Find Maximum of two numbers in Python
Last Updated :
25 Jul, 2024
Given two numbers, write a Python code to find the Maximum of these two numbers.
Examples:
Input: a = 2, b = 4
Output: 4
Input: a = -1, b = -4
Output: -1
Find Maximum of two numbers in Python
This is the naive approach where we will compare two numbers using if-else statement and will print the output accordingly.
Example:
Python
# Python program to find the
# maximum of two numbers
def maximum(a, b):
if a >= b:
return a
else:
return b
# Driver code
a = 2
b = 4
print(maximum(a, b))
Time complexity: O(1)
Auxiliary space: O(1)
Find Maximum of two numbers Using max() function
This function is used to find the maximum of the values passed as its arguments.
Example:
Python
# Python program to find the
# maximum of two numbers
a = 2
b = 4
maximum = max(a, b)
print(maximum)
Time complexity: O(1)
Auxiliary space: O(1)
This operator is also known as conditional expression are operators that evaluate something based on a condition being true or false. It simply allows testing a condition in a single line
Example:
Python
# Python program to find the
# maximum of two numbers
# Driver code
a = 2
b = 4
# Use of ternary operator
print(a if a >= b else b)
Time complexity: O(1)
Auxiliary space: O(1)
Maximum of two numbers Using lambda function
Python
# python code to find maximum of two numbers
a=2;b=4
maximum = lambda a,b:a if a > b else b
print(f'{maximum(a,b)} is a maximum number')
Output:
4 is a maximum number
Maximum of two numbers Using list comprehension
Python
a=2;b=4
x=[a if a>b else b]
print("maximum number is:",x)
Outputmaximum number is: [4]
Maximum of two numbers Using sort() method
Python
# Python program to find the
# maximum of two numbers
a = 2
b = 4
x=[a,b]
x.sort()
print(x[-1])
Time Complexity : O(NlogN)
Auxiliary Space : O(1)
Find Maximum of two numbers in Python – FAQs
How to Find the Maximum of Two Numbers in Python?
You can find the maximum of two numbers in Python using the built-in max() function, conditional statements, or lambda functions.
What is the max() Function in Python?
The max() function returns the largest of the input values.
Syntax:
max(a, b)
Example:
a = 5
b = 10
maximum = max(a, b)
print(maximum) # Output: 10
How to Use Conditional Statements to Find Maximum?
You can use if-else conditional statements to find the maximum of two numbers.
Example:
a = 5
b = 10
if a > b:
maximum = a
else:
maximum = b
print(maximum) # Output: 10
What are Python Lambda Functions?
Lambda functions in Python are small anonymous functions defined using the lambda keyword. They can have any number of arguments but only one expression. The expression is evaluated and returned.
Syntax:
lambda arguments: expression
Example:
# Lambda function to find maximum of two numbers
max_func = lambda a, b: a if a > b else b
a = 5
b = 10
maximum = max_func(a, b)
print(maximum) # Output: 10
How to Compare Two Numbers Using Ternary Operators?
Ternary operators provide a way to condense an if-else statement into a single line. In Python, it is done using the syntax:
Syntax:
a if condition else b
Example:
a = 5
b = 10
maximum = a if a > b else b
print(maximum) # Output: 10