A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 1 — Rectangular Star Pattern

Easy

A solid block of stars: n rows and n columns forming a perfect square of '*'. It is the simplest pattern of all — the outer loop and the inner work both repeat exactly n times — and it teaches the one idea every pattern in this series is built on: one loop controls the rows, the other controls what each row contains.

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Output for n = 5

How to think about it

Number the rows i = 0, 1, ..., n-1. Every row is identical: it contains exactly n stars and no leading spaces, so the outer loop simply repeats n times and the inner work for each row is 'print n stars'. If you build the row as a list of n '*' tokens and join them with single spaces, every row comes out as '* * * ... *' with n stars and n-1 gaps between them. There is no per-row formula to vary — the constancy is the point: the row content does not depend on i at all. Total output size is n rows x n stars = n*n stars.

Code

The function builds each row by repeating the '*' token n times in a list and joining with spaces, then prints it. The loop variable i only counts rows — it plays no role in the content. Building the row string first (instead of printing token by token) makes the row-by-row structure visible and avoids stray trailing spaces.

def pattern(n): for i in range(n): row = " ".join(["*"] * n) print(row)

Key points

  • Outer loop = rows (always n of them); inner work = the contents of one row.
  • Row content here is independent of the row index i — the same n stars every time.
  • Joining a token list with ' ' gives clean spacing with no trailing space.
  • Total stars printed is n * n — this is the 'full grid' every hollow pattern later carves into.
  • Off-by-one check: range(n) gives exactly n rows, and ['*'] * n gives exactly n stars.

Suggested questions

Pattern 2 — Right-Angled Triangle Pattern The rectangle's rows start changing — each row gains one star, introducing the i-dependent row formula.
Pattern 21 — Hollow Rectangle Pattern The same outer rectangle with the interior carved out — a direct variation of this grid.
Pattern 5 — Inverted Right Pyramid Rows shrink instead of staying constant; the inner count becomes n - i.
Sum of Array Elements ↗ Loops over a fixed-size collection, exactly like printing a fixed-width row — a good loop warm-up.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.