Python Print 1 to N
for + rangePractice

Print every floor number

Loop syntax highlight: for i in range(1, n + 1): then run print(i) inside the loop body.

Picture this: count 1, 2, 3… N

range(1, n+1) is the list of numbers to visit.

range(1, n+1)1 … n
→
for i in …one at a time
→
print(i)show i

Diagram — for i in range(1, n+1)

i = 1 i <= n? print(i) i += 1

In easy words: What is this loop doing?

This loop starts at 1 and prints every number until n. It is the most basic example of repeating one action many times.

Where this is used

Used for counting pages, floors, turns, attempts, IDs, and any task where you need to process every number in a fixed range.

Real world: elevator voice

Going up 5 floors? You hear five announcements. The loop variable i is the current floor; the body is “say the number”.

n = ? → print each i

🛗
Floor 1
1for i in range(1, n + 1):
2print(i)
1\n2\n3

range(1, n+1)

  • Stops before n+1, so last value is n.
  • Each print(i) adds a new line in output.

Example Program

1n = int(input())
2for i in range(1, n + 1):
3print(i)