Minimum Element
Read integers and find the minimum with a loop. Students select the first element as an initial candidate and trace smaller replacements through the list.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Find minimum8min_element = arr[0]9for element in arr:10 if element < min_element:11 min_element = element1213print(f"Minimum element: {min_element}")
Output
Enter array size: 5 Element 1: 10 Element 2: 5 Element 3: 20 Element 4: 3 Element 5: 15 Minimum element: 3
In this one
- Collect a nonempty list
- Track a smaller candidate
- Avoid invented sentinels
How it works
- 1
Read and append n integers.
- 2
Initialize min_element from arr[0].
- 3
Replace it on smaller values and print the result.
Edge cases
What happens for n = 0?
What happens for n = 0?
Initialization raises IndexError because the list has no first element.
Do negative values work?
Do negative values work?
Yes. Ordinary numeric comparisons select the smallest.
What's going on
After collecting n integers, min_element is initialized from arr[0]. Every value is compared with the current candidate.
Only a strictly smaller element replaces the minimum. Equal values do not change the result. This works for mixed signs and all-positive lists without an invented sentinel, but requires at least one stored element before indexing the first position.
What happens for n = 0? Initialization raises IndexError because the list has no first element.
Do negative values work? Yes. Ordinary numeric comparisons select the smallest.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why start from arr[0]?
Pick an answer to check it.
Question 2
What happens for n = 0?
Pick an answer to check it.
Questions people ask
What happens for n = 0?
Initialization raises IndexError because the list has no first element.
Do negative values work?
Yes. Ordinary numeric comparisons select the smallest.
Why start from arr[0]?
It is a real candidate from the data.
๐Related Content
- ๐
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- ๐
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- ๐ป
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum