Increasing Integer Pattern in Python

What You’ll Learn
How to print the following increasing integer pattern in Python using nested loops and a small step-based update:
12 63 7 104 8 11 135 9 12 14 15
This pattern is a great exercise for understanding how values can be generated using a controlled increment inside a loop.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15Complete Python Program
Each row starts by printing i. Then it computes the next values using a decreasing step m and an accumulator k.
for i in range(1, 6):
print(i, end=" ")
m = 4
k = i + m
for j in range(1, i):
print(k, end=" ")
m -= 1
k = k + m
print()🧠 How It Works
Outer loop controls the row number
for i in range(1, 6) creates 5 rows and prints i as the first value in each row.
Initialize step and next value
m = 4 (which is rows - 1) and k = i + m sets the next number to print after i.
Inner loop prints remaining values
for j in range(1, i) runs i - 1 times. Each time it prints k, then decreases m and updates k = k + m.
New line after each row
print() moves to the next line for the next row.
Increasing integer pattern
Total printed numbers are 1+2+…+n = n(n+1)/2, so this runs in O(n²) time for \(n\) rows.
Variation — User Input Version
Print the same pattern for any number of rows by using rows - 1 as the initial step size.
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
print(i, end=" ")
m = rows - 1
k = i + m
for j in range(1, i):
print(k, end=" ")
m -= 1
k = k + m
print()💡 Tips for Enhancement
Try These
- Validate input (reject
rows < 1) before printing - Remove trailing spaces by building a row list then
" ".join(...) - Change the starting step (
rows - 1) to experiment with different growth - Right-align the rows for a different visual effect
- Print the pattern using formatted columns for better readability
Avoid
- Forgetting to decrease
m(the row values won’t match the pattern) - Resetting
minside the inner loop (it must shrink each print) - Assuming user input is always valid (wrap
int()conversion if needed) - Mixing spacing rules without updating the output section
Key Takeaways
Each row begins with i, then generates additional values using k and a shrinking step m.
m starts at rows - 1 and decreases after each printed value in the row.
The inner loop runs i - 1 times, so total prints are \(n(n+1)/2\).
This pattern reinforces how loop state (like m) can shape generated sequences.
❓ Frequently Asked Questions
m starts at 4. That initial step creates the correct jump to the next value in the row, then the step shrinks each time to match the pattern.k will increase by a constant amount and the row values will not match the intended sequence." ".join(map(str, row)) at the end of each row.Explore More Python Number Patterns!
Practice nested loops and sequence generation with progressively richer number patterns.
This pattern is a good example of using a shrinking step size (4, 3, 2, 1, ...) to generate non-linear sequences inside loops—useful in many grid and diagonal traversal problems.
12 people found this page helpful
