π Python Pattern Challenge — Day 23
Pattern printing is a simple and effective way to improve your Python loops, repetition, and logical thinking. For Day 23, let's practice a clean number pattern where each new row contains one more number than the previous row.
π― Today's Challenge
Write a Python program to print exactly this pattern:
Best and cleanest code will be rewarded! π
Solution 1 — Using Nested for Loops
for i in range(1, 7): for j in range(1, i + 1): print(j, end=" ") print()
How it works
The outer loop controls the rows:
for i in range(1, 7):So the loop runs 6 times.
The inner loop controls the numbers printed in each row:
for j in range(1, i + 1):For the first row, only 1 is printed.
For the second row:
1 2For the third row:
1 2 3The number of elements increases by one with every new row.
Solution 2 — Using range()
We can make the code even cleaner:
for i in range(1, 7): print(*range(1, i + 1))
Output
1Here, * unpacks the values returned by range() before printing them.
Solution 3 — Using a Function
number_pattern(6)def number_pattern(n): for i in range(1, n + 1): print(*range(1, i + 1))
The function makes the pattern reusable.
For example:
number_pattern(10)will generate the pattern up to 1 2 3 4 5 6 7 8 9 10.
⚡ Short & Clean Code
for i in range(1, 7): print(*range(1, i + 1))
π₯ Just two lines are enough to create the complete Day 23 pattern.
π Challenge Yourself
Can you modify this pattern:
- Make the pattern go up to 10?
- Print the numbers in reverse order?
- Create the same pattern using a while loop?
- Start every row from a different number?
- Replace the numbers with alphabets?
Drop your solution below! π
23 Days. 23 Patterns. Stronger Python Logic. ππ₯
Learn • Practice • Grow with CLCODING π

0 Comments:
Post a Comment