Related Articles

# Maximum of two numbers in Python

• Difficulty Level : Easy
• Last Updated : 25 Aug, 2021

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```

Method #1: This is the naive approach where we will compare tow 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
```

Method #2: 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
```

Method #3: Using Ternary Operator

This operator is also known as conditional expressions 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)` `# This code is contributed by AnkThon`
Output
```4
```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up