Range (Max - Min)
Calculate a list's numerical range as maximum minus minimum. Students guard empty input and distinguish this statistical range from the range iterator.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Calculate range8if len(arr) > 0:9 max_val = max(arr)10 min_val = min(arr)11 range_val = max_val - min_val12 print(f"Range: {range_val}")13else:14 print("Array is empty")
Output
Enter array size: 5 Element 1: 10 Element 2: 5 Element 3: 20 Element 4: 3 Element 5: 15 Range: 17
In this one
- Measure numerical spread
- Guard empty collections
- Distinguish range meanings
How it works
- 1
Check arr is nonempty.
- 2
Find maximum and minimum.
- 3
Subtract minimum from maximum and print the spread.
Edge cases
What if all entries are equal?
What if all entries are equal?
The range is zero.
What happens for an empty list?
What happens for an empty list?
Array is empty is printed.
What's going on
The nonempty branch finds max(arr) and min(arr). Their difference measures the spread between the two extreme values.
For [2, 9, 5], the range is seven. This result is a number, unrelated to creating a Python range object. Equal values produce zero spread. Empty input receives a message instead of calling max or min without data.
What if all entries are equal? The range is zero.
What happens for an empty list? Array is empty is printed.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which calculation measures this range?
Pick an answer to check it.
Question 2
What if all entries are equal?
Pick an answer to check it.
Questions people ask
What if all entries are equal?
The range is zero.
What happens for an empty list?
Array is empty is printed.
Which calculation measures this range?
max_val - min_val.
๐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