In this article, we will show you, How to write a Python Program to find Largest and Smallest Number in a List with practical example. Before you start, please refer List article to understand everything about Lists.

## Python Program to find Largest and Smallest Number in a List Example 1

This python program allow user to enter the length of a List. Next, we used For Loop to add numbers to the list.

Here, the min, max functions in python will return the minimum and maximum values in a List.

# Python Program to find Largest and Smallest Number in a List NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) print("The Smallest Element in this List is : ", min(NumList)) print("The Largest Element in this List is : ", max(NumList))

**OUTPUT**

## Python Program to find Largest and Smallest Number in a List Example 2

The Sort function in python will sort List elements in ascending order. Next, we are using Index position 0 to print first element, and last index position to print last element in a list.

# Python Program to find Largest and Smallest Number in a List NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) NumList.sort() print("The Smallest Element in this List is : ", NumList[0]) print("The Largest Element in this List is : ", NumList[Number - 1])

**OUTPUT**

## Python Program to find Largest and Smallest Number in a List Example 3

In this program we are not using any built-in function such as sort, max, or min function

# Python Program to find Largest and Smallest Number in a List NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) smallest = largest = NumList[0] for j in range(1, Number): if(smallest > NumList[j]): smallest = NumList[j] min_position = j if(largest < NumList[j]): largest = NumList[j] max_position = j print("The Smallest Element in this List is : ", smallest) print("The Index position of Smallest Element in this List is : ", min_position) print("The Largest Element in this List is : ", largest) print("The Index position of Largest Element in this List is : ", max_position)

**OUTPUT**

**ANALYSIS**

From the above screenshot you can observe that, User inserted values are

NumList[5] = {40, 60, 20, 11, 50}

smallest = largest = NumList[0] = 40

**First Iteration – **for 1 in range(1, 5) – Condition is true

So, it will start executing If statement inside the loop until the condition fails.

If (smallest > NumList[j]) inside the for loop is False because (40 > 60)

smallest = 40

position = 1

If (largest < NumList[j]) inside the for loop is True because (40 < 60)

largest = 60

position = 1

**Second Iteration: **for 2 in range(1, 5) – Condition is true

If (40 > 20) – Condition True

smallest = 20

Position = 2

If (60 < 20) – Condition False

largest = 60 ==> unchanged

Position = 1 ==> unchanged

**Third Iteration: **for 3 in range(1, 5) – Condition is true

If (20 > 11) – Condition True

smallest = 11

Position = 3

If (60 < 11) – Condition False

largest = 60

Position = 1

**Fourth Iteration: **for 4 in range(1, 5) – Condition is true

If (11 > 50) – Condition False

smallest = 11

Position = 3

If (60 < 11) – Condition False

largest = 60

Position = 1

**Fifth Iteration:** for 5 in range(1, 5) – Condition is False

So it will exit from the loop.