Class 11 Practical Programs
Find Smallest and Largest Number from List
AIM
To write a Python program that inputs a list of numbers and finds the smallest and largest number from the list using different methods.
Note: This program demonstrates multiple approaches: built-in functions (max/min) and sorting method for finding extremes in a list.
ALGORITHM
- Start
- Initialize an empty list
- Input the number of elements from user
- Use a loop to input numbers and append them to the list
- Method 1: Use built-in max() and min() functions
- Method 2: Sort the list and access first and last elements
- Display the maximum and minimum elements
- Stop
PROGRAM
# Input a list of numbers and find the smallest and largest number
# Method 1: Using built-in functions
lst = []
num = int(input("How many numbers: "))
for n in range(num):
numbers = int(input("Enter Number: "))
lst.append(numbers)
print("The entered list is:", lst)
print("Maximum element in the list is:", max(lst))
print("Minimum element in the list is:", min(lst))
# Method 2: Using sorting
print("\nBy Method 2 (Sorting):")
lst.sort()
print("Maximum element in the list is:", lst[num - 1])
print("Minimum element in the list is:", lst[0])
print("\n" + "=" * 50)
print("METHOD 3: Manual Search (Without Built-in Functions)")
print("=" * 50)
# Method 3: Manual search without built-in functions
lst3 = []
num3 = int(input("\nHow many numbers: "))
for n in range(num3):
numbers = int(input("Enter Number: "))
lst3.append(numbers)
print("The entered list is:", lst3)
# Find maximum manually
maximum = lst3[0]
for i in range(1, len(lst3)):
if lst3[i] > maximum:
maximum = lst3[i]
# Find minimum manually
minimum = lst3[0]
for i in range(1, len(lst3)):
if lst3[i] < minimum:
minimum = lst3[i]
print("Maximum element (manual search):", maximum)
print("Minimum element (manual search):", minimum)
print("\n" + "=" * 50)
print("METHOD 4: Using Tuple and Multiple Data Types")
print("=" * 50)
# Method 4: Working with mixed data (numbers only)
mixed_numbers = [45, 12, 78, 23, 67, 89, 34, 56]
print("Predefined list:", mixed_numbers)
# Find second largest and second smallest
sorted_list = sorted(mixed_numbers)
print("Sorted list:", sorted_list)
print("Largest:", sorted_list[-1])
print("Second largest:", sorted_list[-2])
print("Smallest:", sorted_list[0])
print("Second smallest:", sorted_list[1])
# Remove duplicates and find extremes
unique_numbers = list(set(mixed_numbers))
print("\nUnique numbers:", unique_numbers)
print("Max from unique:", max(unique_numbers))
print("Min from unique:", min(unique_numbers))
print("\n" + "=" * 50)
print("METHOD 5: Function-based Approach")
print("=" * 50)
def find_extremes(data_list):
"""Function to find min and max from a list"""
if not data_list:
return None, None
minimum = maximum = data_list[0]
for num in data_list[1:]:
if num > maximum:
maximum = num
if num < minimum:
minimum = num
return minimum, maximum
def find_nth_largest(data_list, n):
"""Function to find nth largest element"""
if len(data_list) < n:
return None
sorted_unique = sorted(set(data_list), reverse=True)
return sorted_unique[n-1] if len(sorted_unique) >= n else None
# Demo with function
demo_list = [64, 25, 12, 22, 11, 90, 88, 76, 50, 42]
print(f"\nDemo list: {demo_list}")
min_val, max_val = find_extremes(demo_list)
print(f"Minimum: {min_val}")
print(f"Maximum: {max_val}")
# Find nth largest
print(f"2nd largest: {find_nth_largest(demo_list, 2)}")
print(f"3rd largest: {find_nth_largest(demo_list, 3)}")
print("\n" + "=" * 50)
print("PERFORMANCE COMPARISON")
print("=" * 50)
import time
# Large list for performance testing
large_list = list(range(10000, 0, -1)) # Descending order
print(f"\nTesting with {len(large_list)} elements...")
# Time built-in functions
start_time = time.time()
max_builtin = max(large_list)
min_builtin = min(large_list)
builtin_time = time.time() - start_time
# Time manual search
start_time = time.time()
manual_min, manual_max = find_extremes(large_list)
manual_time = time.time() - start_time
# Time sorting method
start_time = time.time()
sorted_list = sorted(large_list)
sort_max = sorted_list[-1]
sort_min = sorted_list[0]
sort_time = time.time() - start_time
print(f"Built-in functions time: {builtin_time:.6f} seconds")
print(f"Manual search time: {manual_time:.6f} seconds")
print(f"Sorting method time: {sort_time:.6f} seconds")
print(f"\nAll methods give same result: {max_builtin == manual_max == sort_max}")OUTPUT
How many numbers: 5
Enter Number: 45
Enter Number: 12
Enter Number: 78
Enter Number: 23
Enter Number: 67
The entered list is: [45, 12, 78, 23, 67]
Maximum element in the list is: 78
Minimum element in the list is: 12
By Method 2 (Sorting):
Maximum element in the list is: 78
Minimum element in the list is: 12
==================================================
METHOD 4: Using Tuple and Multiple Data Types
==================================================
Predefined list: [45, 12, 78, 23, 67, 89, 34, 56]
Sorted list: [12, 23, 34, 45, 56, 67, 78, 89]
Largest: 89
Second largest: 78
Smallest: 12
Second smallest: 23
==================================================
METHOD 5: Function-based Approach
==================================================
Demo list: [64, 25, 12, 22, 11, 90, 88, 76, 50, 42]
Minimum: 11
Maximum: 90
2nd largest: 88
3rd largest: 76
CONCLUSION
Thus, the given program was successfully executed and the output was verified as per the expected result.
VIVA QUESTIONS
What is the time complexity of max() and min() functions?
Both max() and min() have O(n) time complexity as they need to examine each element once to find the extreme values.
Which method is most efficient for finding both max and min?
Using built-in max() and min() functions is most efficient. Manual search in single loop is also good. Sorting is least efficient with O(n log n) complexity.
What happens if the list is empty?
max() and min() functions raise ValueError on empty lists. Always check if list is empty before calling these functions.
How do you find the second largest element?
Sort the list and access second last element, or use set() to remove duplicates first, then sort and access appropriate index.
What is the difference between sort() and sorted()?
sort() modifies the original list in-place and returns None. sorted() returns a new sorted list without modifying the original.
How can you find extremes without using built-in functions?
Initialize max and min with first element, then iterate through remaining elements comparing and updating max/min values as needed.
Can these methods work with floating-point numbers?
Yes, all methods work with float numbers. Just change int(input()) to float(input()) for decimal number inputs.
How do you handle duplicate values when finding nth largest?
Use set() to remove duplicates first, then sort. This ensures you get truly distinct nth largest value, not just nth position in sorted list.
Related Resources
- List Manipulation Python
- Sorting Algorithms Tutorial
- Python Built-in Functions PDF
- List Operations MCQs
- Previous Program: List Search
Performance Tips
- • Use max()/min() for single pass efficiency
- • Avoid sorting for simple min/max finding
- • Handle empty lists with proper checks
- • Consider memory usage with large datasets