Number Pattern 2
Print rows of repeated numbers with Python nested loops. Students compare row and column roles and see why the same row value appears several times per line.
1# Take n2n = int(input("Enter number of rows: "))34# Print pattern5for i in range(1, n + 1):6 for j in range(1, i + 1):7 print(i, end="")8 print()
Output
Enter number of rows: 4 1 22 333 4444
In this one
- Print a row counter
- Repeat a value by width
- Separate content from repetition
How it works
- 1
Choose row number i.
- 2
Print i once for each of i columns.
- 3
End the repeated-number row.
Edge cases
What is the fourth row?
What is the fourth row?
4444.
Does the inner counter appear in the output?
Does the inner counter appear in the output?
No. The code prints i.
What's going on
The outer i is both the value and the number of repetitions. The inner loop's j counts how many copies to print but is not displayed.
For three rows, the output is 1, 22, and 333. The newline runs only after each repetition loop ends. Once i reaches two digits, character width no longer equals the number of loop iterations.
What is the fourth row? 4444.
Does the inner counter appear in the output? No. The code prints i.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why does row three contain three copies?
Pick an answer to check it.
Question 2
What is the fourth row?
Pick an answer to check it.
Questions people ask
What is the fourth row?
4444.
Does the inner counter appear in the output?
No. The code prints i.
Why does row three contain three copies?
The inner range has three values.
๐Related Content
- ๐
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- ๐
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- ๐ป
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum