Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

BeginnerPhase 4 - Level 1: Basic ArraysExample 9 of 10
elements-greater-than-k.py
1# Take array
2n = int(input("Enter array size: "))
3arr = []
4for i in range(n):
5 arr.append(int(input(f"Element {i+1}: ")))
6
7k = int(input("Enter k: "))
8
9# Print elements > k
10print(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. 1

    Collect the list, then read k.

  2. 2

    Visit each element and test element > k.

  3. 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.