π Day 44/150 – Find Minimum in a List in Python
Finding the minimum element in a list is a common operation in Python and helps you understand lists, loops, and comparisons.
Example:
[5, 2, 9, 1, 7] → Minimum = 1
Let’s explore different ways to find the minimum value π
πΉ Method 1 – Using min() Function
numbers = [5, 2, 9, 1, 7] print("Minimum:", min(numbers))✅ Easiest and most recommended method.
πΉ Method 2 – Using for Loop
numbers = [5, 2, 9, 1, 7] min_val = numbers[0] for num in numbers: if num < min_val: min_val = num print("Minimum:", min_val)✅ Good for understanding logic.
πΉ Method 3 – Taking User Input
numbers = list(map(int, input("Enter numbers: ").split())) print("Minimum:", min(numbers))✅ Dynamic input from user.
πΉ Method 4 – Using while Loop
numbers = [5, 2, 9, 1, 7] i = 0 min_val = numbers[0] while i < len(numbers): if numbers[i] < min_val: min_val = numbers[i] i += 1 print("Minimum:", min_val)
✅ Alternative looping approach.
πΉ Method 5 – Using Sorting
numbers = [5, 2, 9, 1, 7] numbers.sort() print("Minimum:", numbers[0])✅ Works but not efficient for large lists.
πΉ Output
Minimum: 1
π₯ Key Takeaways
✔️ min() is the fastest and simplest
✔️ Loops help build logic
✔️ Sorting works but is slower
✔️ Always check for empty list in real apps


0 Comments:
Post a Comment