Programs · Program 6-5
Q.Program 6-5: Write a program to print the first five natural numbers.
Tripura TbseTextbookSubjective· 2mImportance★★★★★est
23% · 9/39 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Counting a fixed number of times is exactly what for + range() is for: range(1, 6) yields 1, 2, 3, 4, 5, and the loop body prints each one.
The idea. When the number of repetitions is known in advance (here: exactly five), a counting loop is the natural tool. range(start, stop) produces the integers from start up to but not including stop, and the for loop takes them one at a time.
# Program 6-5: print the first five natural numbers
for num in range(1, 6):
print(num)
Expected output
1
2
3
4
5
How it works
range(1, 6)generates the sequence 1, 2, 3, 4, 5. The stop value (6) is never produced — this "half-open" behaviour is a deliberate Python convention.- In each pass of the loop,
numis bound to the next value of the sequence and the bodyprint(num)executes, so five lines are printed. - After the value 5 is processed, the sequence is exhausted and the loop ends. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.