How to find the maximum of two numbers in Python? You can create a program that takes two integers as input from the user and returns the maximum of the two numbers. The easiest way to find the maximum of two numbers is by using the built-in `max()` function.

Alternatively, you can find the maximum of two numbers by using `if` statement, `ternary` operator, `list` comprehension, `lambda`, and `sort()` functions. In this article, I will explain how to find the maximum of two numbers by using all these functions with examples.

## 1. Quick Examples of Finding Maximum of Two Numbers

If you are in a hurry, below are some quick examples of how to find the maximum of two numbers.

``````
# Quick examples of finding maximum of two numbers

# Example 1: Find the maximum value
# Using max() function
x = 5
y = 10
max_value = max(x, y)

# Example 2: Find the maximum value
# Using If Statement
if x > y:
max_value = x
else:
max_value = y

# Example 3: Using If statement
def maximum(x, y):
if x >= y:
return x
else:
return y

# Example 4: Using ternary operator
max_value = x if x >= y else y

# Example 5: Using list comprehension
max_value = x if x > y else y

# Example 6: Using list comprehension
max_value = [x if x>y else y]

# Example 7: Using the lambda function
max_number = lambda x, y: x if x > y else y
maximum = max_number(x, y)

# Example 8: Using sort() method
max_value = [x, y]
max_value.sort()
maximum = max_value[-1]
``````

## 2. Find a Maximum of Two Numbers Using the max() Function

You can get a maximum of two numbers using `max()` a function. For example, first, initialize the two variables, `x` and `y`, with values of `5` and `10`, respectively. Then, the `max()` function is used to find the maximum value between `x` and `y`. The resulting maximum value is stored in the max_value variable.

``````
# Initialize the two variables
x = 5
y = 10

# Find maximum value
# Using max() function
print("First number:", x)
print("Second number:", y)
max_value = max(x, y)
print("The maximum value: ", max_value)
``````

Yields below output.

## 3. Find a Maximum of Two Numbers Using If Statement

To find the maximum of two numbers using an if statement, compare the numbers and assign the larger value to a variable. For example, you can compare `x` and `y` using an if statement. If `x` is greater than `y`, assign the value of `x` to the `max_value` variable. however, if `y` is greater than or equal to `x`, `y `assign the value of `y` to `max_value`.

``````
# Initialize two variables
x = 5
y = 10

# Find maximum value
# Using If Statement
print("First number:", x)
print("Second number:", y)
if x > y:
max_value = x
else:
max_value = y
print("The maximum value: ", max_value)
``````

Yields the same output as above.

Similarly, You can also find the maximum of two numbers using an `if` statement in Python, For instance, first initializes two variables, `x` and `y`, with values of `5` and `10`, respectively. Then, a function named maximum is defined that takes two arguments `x` and `y`. Within the function, an if statement is used to compare `x` and `y` and return the maximum value. If `x` is greater than or equal to `y`, the function returns `x`; otherwise, it returns `y`.

``````
# Initialize two variables
x = 5
y = 10

# Using If statement
def maximum(x, y):

if x >= y:
return x
else:
return y
print("The maximum value: ", maximum(x, y))
``````

Yields the same output as above.

## 4. Find a Maximum of Two Numbers Using Ternary Operator

You can also find the maximum of two numbers using the ternary operator. This operator allows you to write concise conditional expressions in a single line.

This program uses the ternary operator to compare `x` and `y`. If `x` is greater than or equal to `y`, `x` is assigned to `max_value`; otherwise, `y` is assigned to `max_value`. Finally, the maximum value is printed with a descriptive message.

``````
# Initialize two variables
x = 5
y = 10

# Using ternary operator
max_value = x if x >= y else y
print("The maximum value: ", max_value)
``````

Yields the same output as above.

## 5. Using List Comprehension

In Python, you can use list comprehension to find the maximum of two numbers. For example, you can use a conditional expression `[x if x > y else y]` to determine the maximum value. If `x` is greater than `y`, then `x` is assigned to `max_value`. Otherwise, `y` is assigned to `max_value`.

``````
# Initialize two variables
x = 5
y = 10

# Using list comprehension to find max value
max_value = x if x > y else y
print("The maximum value:", max_value)

# Output:
# The maximum value: 10

# Using list comprehension
x=5; y=10
max_value = [x if x>y else y]
print("The maximum value: ", max_value)

# Output:
# The maximum value:  [10]
``````

## 6. Find a Maximum of Two Numbers Using the Lambda Function

Alternatively, you can use the lambda function to find the maximum of two numbers in Python. For example, this function takes two parameters `x` and `y` and compares them using a ternary operator (`x if x > y else y`). If the `x` is greater than `y`, the function returns `x`. Otherwise, it returns `y`. The result is assigned to the variable `maximum` and then printed.

``````
# Initialize two variables
x = 5
y = 10

# Using the lambda function to find the max value
max_number = lambda x, y: x if x > y else y
maximum = max_number(x, y)
print("The Maximum Value:", maximum)
``````

Yields the same output as above.

## 7. Using sort() Method

To find the maximum of two numbers you can use the `sort()` method. For example, first, initialize the variables `x` and `y` with the given values. Then, create a list `max_value` that contains `x` and `y`. Sort the given list using sort() method, and access the maximum value using `max_value[-1]`.

``````
# Initialize two variables
x = 5
y = 10

# Using sort() method to find the max value
max_value = [x, y]
max_value.sort()
maximum = max_value[-1]
print("The maximum value:", maximum)
``````

Yields the same output as above.

## Conclusion

In this article, I have explained how to find the maximum of two numbers in Python by using the `max()`, `if` statement, `ternary` operator, `list` comprehension, `lambda`, and `sort()` functions with examples.

Happy Learning !!