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
Print One Line of Stars
for number in range(5):
print("*")
Output
*
*
*
*
*
This prints one star five times.
Print Stars on One Line
for number in range(5):
print("*", end="")
Output
*****
end="" tells Python not to move to a new line.
Square Pattern
Use nested loops to print rows and columns.
for row in range(3):
for star in range(4):
print("*", end="")
print()
Output
****
****
****
Number Pattern
for number in range(1, 6):
print(number)
Output
1
2
3
4
5
Growing Pattern
for row in range(1, 6):
print("*" * row)
Output
*
**
***
****
*****
Another Growing Number Pattern
for row in range(1, 6):
print(row * str(row))
Output
1
22
333
4444
55555
Code Along
for row in range(1, 4):
print("#" * row)
Output
#
##
###
Mini Challenge
Build a triangle pattern.
Steps:
- Use a loop
- Print stars like this:
*
**
***
****
Real World Use Case
Patterns help programmers understand loops. Similar ideas are used in grids, game boards, reports, and layouts.
Quiz
- What is a pattern?
- What does
end=""do? - Why are nested loops useful for patterns?
- What does
"*" * 3produce?
Assignment
Create a program that prints:
5
55
555
5555
55555
Summary
You learned how loops can create patterns using repetition, nested loops, and string multiplication.