# Program to Find Maximum and Minimum in Python

In this brief article, we are going to see **program to find maximum and minimum in Python**. We will discuss finding maximum and minimum numbers in a list saperately using multiple different approaches.

- Python Program To Find Maximum
- Python Program To Find Minimum
- Conclusion

Table of Contents

## Python Program To Find Maximum

We are going to find the maximum number from a list of numbers using 3 different approaches.

First, we will find the maximum number using the **max()** function. Second, we will use some logic to find the maximum number, and third, we will use the **sort()** method.

### 1. Find Maximum max() Function

The max() is a built-in function in Python. It returns the largest item in a list.

The method accepts a list of numbers as an argument, finds the largest number in the list, and returns it.

The following example shows how to use the max() function to find the maximum number in a list.

```
num = [12, 5, 30, 8, 2, 17, 55, 27]
# using max() function
max_num = max(num)
print("Maximum number in the list is: ", max_num)
```

Output

Maximum number in the list is: 55

### 2. Find Maximum Using Logic

Another way to find the maximum number is by looping through the list and comparing each number with the previous number.

Algorithm:

- Initialize a variable max_num with the first number in the list.
- Loop through the list and compare each number with the previous number.
- If the current number is greater than the maximum number we created above, then update the max_num variable with the current number.
- If the current number is less than the previous number, then do nothing.

Here is the program for above algorithm:

```
num = [12, 5, 30, 8, 2, 17, 55, 27]
max_num = num[0]
# loop through the list
for i in range(1, len(num)):
# compare each number with max_num
# make current number as max_num if it is greater than the previous number
if num[i] > max_num:
max_num = num[i]
print("Maximum number in the list is: ", max_num)
```

Output

Maximum number in the list is: 55

### 3. Find Maximum Using Sort() Method

The sort() method is a built-in method for list-objects. It sorts the elements of the list in ascending order.

Once we have sorted list of numbers, we can find the maximum number in the list using the index of the last element in the list.

Since the list is sorted in ascending order, the last element in the list will be the maximum number. i.e num[-1].

```
num = [12, 5, 30, 8, 2, 17, 55, 27]
# sort the list
num.sort()
# get the last element (maximum)
max_num = num[-1]
print("Maximum number in the list is: ", max_num)
```

Output

Maximum number in the list is: 55

## Python Program To Find Minimum

Just like above now we are going to find the minimum number from a list of numbers using 3 different approaches.

First, we will find the minimum number using the **min()** function. Second, we will loop through the list and compare each number with the previous number, and third, we will use the **sort()** method.

### 1. Find Minimum min() Function

The min() is a built-in function in Python. It returns the smallest item in a list.

The method accepts a list of numbers as an argument, finds the smallest number in the list, and returns it.

The following example shows how to use the min() function to find the minimum number in a list.

```
num = [12, 5, 30, 8, 2, 17, 55, 27]
# using min() function
min_num = min(num)
print("Minimum number in the list is: ", min_num)
```

Output

Minimum number in the list is: 2

### 2. Find Minimum Using Loop

You have seen above how we found the maximum number by taking the first element as the biggest element and comparing it with another element. We can do the same thing here.

Algorithm:

- Initialize a variable min_num with the first number in the list.
- Use for loop to loop through the list and check if the current number is less than our minimum number.
- If the current number is less than the minimum number, then update the min_num variable with the current number.

Here is the program for the above algorithm:

```
num = [12, 5, 30, 8, 2, 17, 55, 27]
min_num = num[0]
# loop through the list
for i in range(1, len(num)):
# compare each number with min_num
if num[i] < min_num:
min_num = num[i]
print("Minimum number in the list is: ", min_num)
```

Output

Minimum number in the list is: 2

### 3. Find Minimum Using sort() Method

We have used the sort() method above to find the maximum number in the list. We can use it in the same way to find the minimum one.

Since the list is sorted in ascending order, the first element in the list will be the minimum number. i.e num[0].

```
num = [12, 5, 30, 8, 2, 17, 55, 27]
# sort the list
num.sort()
# get the first element (minimum)
min_num = num[0]
print("Minimum number in the list is: ", min_num)
```

Output

Minimum number in the list is: 2

## Conclusion

Finding maximum or minimum from a list of numbers is a very frequent task in programming. We have covered 3 different approaches to finding a maximum or minimum number in a list.

You can come up with your own approach to find the maximum or minimum number in a list.

Check out the count substring in string in python.

Happy Coding!😇