CodingNic

Loops and Iteration

Loop Patterns

Loops and Iteration 22 min read

Loop Patterns

Loop Patterns

Loops can do more than count numbers.

They can also create shapes and patterns.

Patterns are a great way to practice loops because they help you understand repetition clearly.

What Is a Pattern?

A pattern is something that repeats in a clear order.

For example:

  • Stars
  • Numbers
  • Letters
python
for number in range(5):
    print("*")

Output

text
*
*
*
*
*

This prints one star five times.

python
for number in range(5):
    print("*", end="")

Output

text
*****

end="" tells Python not to move to a new line.

Square Pattern

Use nested loops to print rows and columns.

python
for row in range(3):
    for star in range(4):
        print("*", end="")
    print()

Output

text
****
****
****

Number Pattern

python
for number in range(1, 6):
    print(number)

Output

text
1
2
3
4
5

Growing Pattern

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

Output

text
*
**
***
****
*****

Another Growing Number Pattern

python
for row in range(1, 6):
    print(row * str(row))

Output

text
1
22
333
4444
55555

Code Along

python
for row in range(1, 4):
    print("#" * row)

Output

text
#
##
###

Mini Challenge

Build a triangle pattern.

Steps:

  • Use a loop
  • Print stars like this:
text
*
**
***
****

Real World Use Case

Patterns help programmers understand loops. Similar ideas are used in grids, game boards, reports, and layouts.

Quiz

  1. What is a pattern?
  2. What does end="" do?
  3. Why are nested loops useful for patterns?
  4. What does "*" * 3 produce?

Assignment

Create a program that prints:

text
5
55
555
5555
55555

Summary

You learned how loops can create patterns using repetition, nested loops, and string multiplication.