Here ,we are going to Write a Python Program to find the Largest and Smallest Number in a List with a practical example.

Read full article to find the below programs:

**Python Program to find Largest and Smallest Number in a List using nested if-else and for loop.****Python Program to find Largest and Smallest Number in a Tuple using nested if-else and for loop.**

## Program source code

```
# Python Program to find Largest and Smallest Number in a List
# Creating list
nlist = []
# Reading numbers from user
n = int(input("Enter the Total Number of List Elements: "))
# Using for loop to add numbers in list
for i in range(1, n + 1):
value = int(input("Enter the Value of %d Element : " %i))
nlist.append(value)
# Sort list elements
nlist.sort()
# Displaying smallest and largest element in list
print("The Smallest Element in this List is : ", nlist[0])
print("The Largest Element in this List is : ", nlist[n - 1])
```

## Explanation:

In this code, we first ask the user to enter the total number of elements they want in list and store it in the variable ‘n’. We then use a ‘for’ loop to iterate ‘n+1’ times and ask user to enter list element each time. We convert the user input to an integer using ‘int()’ function and append it to the ‘nlist’ list. We then use sort() method to arrange list elements in ascending order and we know that first element of sorted list will become smallest element and last element of list will be Largest element of list.

## Output

Enter the Total Number of List Elements: 5 Enter the Value of 1 Element : 23 Enter the Value of 2 Element : 44 Enter the Value of 3 Element : 12 Enter the Value of 4 Element : 78 Enter the Value of 5 Element : 55 The Smallest Element in this List is : 12 The Largest Element in this List is : 78

## Python Program to find Largest and Smallest Number in a List using nested if-else and for loop.

Here is an example python code that allows the user to enter a list of numbers and finds the smallest and largest numbers in that list.

```
nlist = []
n = int(input("Enter the Total number of elements in list :"))
for i in range(n):
number = int(input("Enter element:"))
nlist.append(number)
smallest = nlist[0]
largest = nlist[0]
for number in nlist:
if number < smallest:
smallest = number
elif number > largest:
largest = number
print("Smallest number in list :",smallest)
print("Largest number in list :", largest)
```

### Explanation :

In this code, we first ask user to enter the number of elements they want in the list and store it in variable ‘n’. We then use a for loop to iterate ‘n’ times and ask user to enter a list element each time. we then convert the user input to integer using ‘int()’ function and append it to the ‘nlist’ list.

We then use another for loop to iterate through the list and find the smallest and largest number. For each number in the list, we check if it is smaller than the current value of ‘smallest’, we update ‘smallest’. Similarly, if we find a number that is larger than the current value of ‘largest’ ,we update ‘largest’. After iterating through the entire list , we print out the final values of ‘smallest’ and ‘largest’. The output of the code would depend upon the list of numbers entered by the user.

### Output:

Enter the Total number of elements in list :5 Enter element:11 Enter element:34 Enter element:23 Enter element:56 Enter element:09 Smallest number in list : 9 Largest number in list : 56

## Python Program to find Largest and Smallest Number in a Tuple using nested if-else and for loop.

Here is an example python code that finds the smallest and largest numbers in tuple.

```
numbers = (4,5,6,3,2)
smallest = numbers[0]
largest = numbers[0]
for number in numbers:
if number < smallest:
smallest = number
elif number > largest:
largest = number
print("Smallest number in tuple :",smallest)
print("Largest number in tuple :", largest)
```

### Explanation :

In this code, we first define a tuple of numbers. We then initialize two variables, ‘smallest’ and ‘largest’ , with the first element of the tuple. We then iterate through the tuple using for loop, comparing each element to the current values of ‘smallest’ and ‘largest’ . if we find a number that is smaller than the current value of ‘smallest’, we update ‘smallest’. Similarly, if we find a number that is larger than current value of ‘largest’, we update ‘largest’.

After iterating through the entire tuple, we print out the final values of ‘smallest’ and ‘largest’. The output of the code for the example tuple would be:

### Output :

Smallest number in tuple : 2 Largest number in tuple : 6

### Latest Post :

- 20 Essential MySQL Queries for Managing Your Database
- Python Web Development: Frameworks and Applications
- A Beginner’s Guide to Machine Learning Algorithms and Their Applications in Python
- Python Game Development Libraries: A Beginner’s Guide to Pygame, PyOpenGL, and Panda3D
- How to convert Infix expression to Postfix expression using stack in python