This article covers the Python program for finding the smallest and largest numbers among the given list of numbers with explanations and examples.
Table of contents
1. Using min() and max() Functions
The min() and max() functions are built-in Python functions used to find the smallest and largest values from a given set of numbers, lists, or iterables.
min() and max() functions work for both integer and float numbers.
min() function:
Syntax:
min(iterable) # Returns the smallest value from an iterable
min(a, b, c, …) # Returns the smallest among multiple argumentsCode language: Python (python)
For Example:
max() function:
Syntax:
max(iterable) # Returns the largest value from an iterable
max(a, b, c, …) # Returns the largest among multiple argumentsCode language: Python (python)
For Example:
Code Example
Output:
Smallest: -20, Largest: 30.5
Smallest: 2, Largest: 225
2. Using Conditional Statements (if-else)
Conditional statements in Python control flow of execution based on given conditions. They allow the program to make decisions and execute different blocks of code depending on whether a condition is True or False.
Syntax:
if condition1:
# this Code executes if condition1 is True
elif condition2:
# this Code executes <em>if condition2 is True
</em>else:
# this Code executes if none of the above conditions are TrueCode language: Python (python)
Code Example
Explanation
- We have defined two functions,
find_smallest()function to find the minimum value andfind_largest()function to find the maximum value. find_smallest()function:- Compares
num1withnum2andnum3. - If
num1is the smallest, it is returned. - Otherwise, it checks if
num2is smaller thannum3, thennum2is returned. - If neither of those conditions is met,
num3is the smallest.
- Compares
find_largest()function:- Checks if
num1is greater than bothnum2andnum3, then it is the largest. - Otherwise, if
num2is greater thannum3,num2is the largest. - Otherwise,
num3is the largest.
- Checks if
3. Using sorting
Sorting is the process of arranging elements in a specific order, typically ascending (smallest to largest) or descending (largest to smallest). Sorting is done using sort() function to arrange the list of numbers in ascending order, allowing easy retrieval of the smallest (numbers[0]) and largest (numbers[-1]) values.
The sort() function is a built-in list method in Python that sorts the elements of a list in place, meaning it modifies the original list instead of creating a new one.
Syntax: list.sort(key=None, reverse=False)
key (Optional)→ A function that specifies sorting criteria (e.g., length, absolute value).reverse (Optional)→ If True, sorts in descending order; default is False (ascending).
Code Example
Explanation
- Here, we add the numbers to the list to find the smallest and largest among them.
- Then, we sort that list in ascending order using
sort()function on the list. - The first element after sorting (
numbers[0]) is the smallest, and the last element (numbers[-1]) is the largest. - This method is easy to understand but slightly slower due to the overhead of sorting.

Leave a Reply