Open In App

Find Maximum of two numbers in Python

Last Updated : 03 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

Python3




# 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))


Output

4

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: 

Python3




# Python program to find the
# maximum of two numbers
 
 
a = 2
b = 4
 
maximum = max(a, b)
print(maximum)


Output

4

Time complexity: O(1)
Auxiliary space: O(1)

Maximum of two numbers Using Ternary Operator

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:

Python3




# 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)


Output

4

Time complexity: O(1)
Auxiliary space: O(1)

Maximum of two numbers Using lambda function 

Python3




# 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

Python3




a=2;b=4
x=[a if a>b else b]
print("maximum number is:",x)


Output

maximum number is: [4]

Maximum of two numbers Using sort() method

Python3




# Python program to find the
# maximum of two numbers
a = 2
b = 4
x=[a,b]
x.sort()
print(x[-1])


Output

4

Time Complexity : O(NlogN)
Auxiliary Space : O(1)



Previous Article
Next Article

Similar Reads

Python - Find the Maximum of Similar Indices in two list of Tuples
Sometimes, while working with Python records, we can have a problem in which we need to perform cross maximization of list of tuples. This kind of application is popular in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + zip() The combination of above functionalities can
7 min read
Python Program to Find the Gcd of Two Numbers
Given two numbers, The task is to find the GCD of the two numbers. Python Program to Find the Gcd of Two Numbers Using STL In Python, the math module contains a number of mathematical operations, which can be performed with ease using the module. math.gcd() function compute the greatest common divisor of 2 numbers mentioned in its arguments. Syntax
4 min read
Python Program to find the Quotient and Remainder of two numbers
Given two numbers n and m. The task is to find the quotient and remainder of two numbers by dividing n by m. Examples: Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 4 Method 1: Naive approach The naive approach is to find the quotient using the double division (//) operator and remainder using
2 min read
Python Program to Find LCM of Two Numbers
We are given two numbers and our task is to find the LCM of two numbers in Python. In this article, we'll discuss different approaches to finding the LCM of two numbers in Python. Example: Input: a = 12, b = 15Output: 60Explanation: LCM of 12 and 15 is 60Python Program to Find LCM of Two NumbersBelow are some of the ways by which we can find the LC
3 min read
Find the sum and maximum value of the two column in excel file using Pandas
In these articles, we will discuss how to read data from excel and perform some mathematical operation and store it into a new column in DataFrame. Suppose our excel file looks like this. Then we have to compute the sum of two-column and find out the maximum value and store into a new DataFrame column. Approach : Import Pandas module.Read data from
2 min read
Return the maximum of an array along axis 0 or maximum ignoring any NaNs in Python
In this article, we will see how to return the maximum of an array along axis 0 or maximum ignoring any NaNs in Python. ExampleInput: [[ 1. 0. 3.] [10. nan 30.] [nan 10. 20.]] Output: [10. 10. 30.] Explanation: An array with maximum values.nanmax method in PythonPython provides a nanmax method that returns the maximum of an array along a specified
3 min read
Return the maximum of an array or maximum ignoring any NaNs in Python
In this article, we will cover how to return the maximum of an array or maximum by ignoring any NaNs in Python using NumPy. ExampleInput: [ -1. -2. nan 1000.] Output: 1000.0 Explanation: maximum value of the array ignoring nans. One approach to use the built-in Python function max(), along with the filter() function and the math.isnan() function fr
4 min read
Python | Maximum and Minimum value from two lists
The problem of finding maximum and minimum values in a list is quite common. But sometimes this problem can be extended in two lists and hence becomes a modified problem. This article discusses shorthands by which this task can be performed easily. Let's discuss certain ways in which this problem can be solved. Method #1: Using max() + min() + "+"
7 min read
Highlight the maximum value in last two columns in Pandas - Python
In this article, we will discuss how to highlight the maximum values in Pandas Dataframe. Let's first make a dataframe: Example: C/C++ Code # Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe dict = {'Name': ['Sumit Tyagi', 'Sukritin', 'Akriti Goel', 'Sanskriti', 'Abhishek Jain'], 'Age': [22, 2
2 min read
Python Program for Maximum difference between groups of size two
Given an array of even number of elements, form groups of 2 using these array elements such that the difference between the group with highest sum and the one with lowest sum is maximum.Note: An element can be a part of one group only and it has to be a part of at least 1 group. Examples: Input : arr[] = {1, 4, 9, 6} Output : 10 Groups formed will
3 min read
Article Tags :
Practice Tags :