Elements Greater Than K
Print list values strictly greater than a threshold with Python. Students use a filtering traversal, preserve input order, and test equality at the cutoff.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67k = int(input("Enter k: "))89# Print elements > k10print(f"Elements greater than {k}:")11for element in arr:12 if element > k:13 print(element, end=" ")14print()
Output
Enter array size: 5 Element 1: 10 Element 2: 5 Element 3: 20 Element 4: 15 Element 5: 8 Enter k: 10 Elements greater than 10: 20 15
In this one
- Filter by a threshold
- Exclude equality
- Preserve result order
How it works
- 1
Collect the list, then read k.
- 2
Visit each element and test element > k.
- 3
Print each match without modifying arr.
Edge cases
Does a value equal to k print?
Does a value equal to k print?
No. The comparison is strict.
What if nothing qualifies?
What if nothing qualifies?
Only the heading and final newline appear.
What's going on
After building arr, the program reads a separate threshold k. It then visits every element without modifying the stored list.
Only element > k prints. Values equal to k are excluded, while repeated qualifying values appear repeatedly in their original order. end creates a shared output line and the last print ends it, even when no values qualify.
Does a value equal to k print? No. The comparison is strict.
What if nothing qualifies? Only the heading and final newline appear.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Does this sort matching values?
Pick an answer to check it.
Question 2
Does a value equal to k print?
Pick an answer to check it.
Questions people ask
Does a value equal to k print?
No. The comparison is strict.
What if nothing qualifies?
Only the heading and final newline appear.
Does this sort matching values?
No, it preserves input order.
๐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