Python Program to Find the Largest and Smallest Number in a List

A Python Program to Find the Largest and Smallest Number in a List

Code Explanation:

Function Definition:
A function find_largest_smallest(numbers) is defined to find the largest and smallest values in the list.

Find Largest and Smallest:
Uses built-in Python functions max() to get the largest number and min() to get the smallest number.

Return Values:
Returns both the largest and smallest values as a tuple.

Create List:
A sample list num_list is created with integer values.

Function Call and Output:
The function is called with the list, and the result is printed showing both the largest and smallest numbers.

 

Program:

def find_largest_smallest(numbers):

    largest = max(numbers)

    smallest = min(numbers)

    return largest, smallest

num_list = [10, 25, 3, 76, 42, 5, 99, 1]

largest, smallest = find_largest_smallest(num_list)

print("Largest number:", largest)

print("Smallest number:", smallest)