Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 45

Pattern Programs in Python

In this lesson: Star, pyramid, number and alphabet pattern programs in Python with nested loops — code and output for practice. In Hindi & English.

Why pattern programs

Pattern programs print shapes made of stars or numbers — triangles, pyramids, and so on. They are the classic way to master nested loops (a loop inside a loop). Getting good at them trains you to think in rows and columns, which helps with tables, grids and matrices later.

💡 Outer loop = rows (how many lines). Inner loop = what to print in each line.

Star triangle

Python can repeat a string by multiplying, so a simple triangle needs just one loop.

for i in range(1, 6):
    print("* " * i)
*
* *
* * *
* * * *
* * * * *

"* " * i repeats the star i times. As i grows from 1 to 5, each row gets one more star.

Dry run

Dry Run — star triangle: print("* " * i)
i"* " * iPrint
1"* " once*
2"* " twice* *
3"* " three times* * *
4"* " four times* * * *
5"* " five times* * * * *
💡 Understand it this way

In Python, multiplying a string by a number repeats it — "* " * 3 = "* * * ". As i grows in the loop, each row gets more stars, forming a triangle.

Right-angled with nested loops

To really use nested loops, print each star one at a time.

for i in range(1, 6):
    for j in range(i):
        print("*", end=" ")
    print()          # new line after each row
*
* *
* * *
* * * *
* * * * *

The outer loop (i) is the row; the inner loop (j) prints i stars using end=" " to stay on the line. The empty print() moves to the next row.

Number pattern

for i in range(1, 6):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Same nested-loop structure, but we print the value of j instead of a star, giving a number triangle. Change what the inner loop prints to make different patterns.

Common mistakes

  • Forgetting the empty print(). Without it, all rows run into one line.
  • Wrong inner range. Row i usually needs range(i) or range(1, i+1) items.
  • Missing end=" ". Then each star goes on its own line.
  • Confusing rows and columns. Outer loop is rows; inner loop is the content of a row.

Summary

  • Pattern programs are the best practice for nested loops.
  • Outer loop makes rows; inner loop fills each row.
  • "* " * i repeats a string for quick triangles.
  • Use end=" " to stay on a line and print() for a new row.
  • Print numbers instead of stars for number patterns.

Next, strengthen loops with Python loops, or try the prime number program.

Frequently Asked Questions

Why are pattern programs important to learn?
Pattern programs are the best way to master nested loops — a loop inside a loop. They train you to think about rows and columns, which is a skill used in tables, grids, matrices and many real problems.
How do nested loops make a pattern?
The outer loop controls the rows, and the inner loop controls what is printed in each row. As the outer loop moves down a row, the inner loop draws that row's characters.
How does Python repeat a character easily?
Multiplying a string by a number repeats it: "* " * 3 gives "* * * ". This lets you print a whole row without an inner loop for simple triangles.
What is the difference between print() with and without end?
Normal print() moves to a new line each time. print(x, end="") stays on the same line. In patterns you often print on the same line, then use an empty print() to move to the next row.
How do I make a pyramid instead of a triangle?
Add leading spaces before the stars so each row is centred, and increase the stars by two each row. Combining spaces and stars in nested loops produces the pyramid shape.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicCalculator Program Your progress: 45 / 66 (68%) · Lesson 46 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free