How to print pattern in Python
Printing patterns in Python involves using loops (primarily nested loops) to create structured outputs, such as triangles, diamonds, or other shapes. Let’s break it down step by step.
Steps to Print Patterns
1. Understand the Pattern Structure
First, observe the pattern. For example:
*
**
***
****
- It has 4 rows.
- The number of stars in each row equals the row number.
2. Divide the Pattern into Rows and Columns
- Rows are the count of lines in the pattern.
- Columns are how many characters-say,
*
or space-will be present in each row.
3. Use Nested Loops
- The outer loop manages rows.
- The inner loop manages columns or prints the characters for each row.
4. Print Characters Without Ending
Use print(item, end="")
to stay on the same line while printing characters.
5. Add a Newline After Each Row
Once the inner loop is completed, use print()
to move to the next line.
Example Patterns
1. Right-Angle Triangle of Stars
rows = 5 # Number of rows
for i in range(1, rows + 1): # Outer loop for rows
for j in range(1, i + 1): # Inner loop for columns
print("*", end="") # Print star without newline
print() # Move to the next line after each row
Explanation:
- Outer loop
for i in range(1, rows + 1)
) runs 5 times; rows = 5. Each iteration is one row. - Inner loop (
for j in range(1, i + 1)
) printsi
stars for thei
-th row. - The
print()
statement at the end of the outer loop prints to the next line after it has printed all the stars for a row.
Output:
*
**
***
****
*****
Explanation of Output:
- Row 1: 1 star.
- Row 2: 2 stars.
- Row 3: 3 stars.
- Row 4: 4 stars.
- Row 5: 5 stars.
2. Inverted Right-Angle Triangle
rows = 5
for i in range(rows, 0, -1): # Outer loop from rows to 1
for j in range(1, i + 1): # Inner loop for columns
print("*", end="")
print() # Move to the next line
Explanation:
- Outer loop (
for i in range(rows, 0, -1)
) begins at 5 and counts down to 1, which fixes the row count. - Inner loop (
for j in range(1, i + 1)
) printsi
stars in each row. - The
print()
after the inner loop moves to the next line.
Output:
*****
****
***
**
*
Explanation of Output:
- Row 1: 5 stars.
- Row 2: 4 stars.
- Row 3: 3 stars.
- Row 4: 2 stars.
- Row 5: 1 star.
3. Pyramid Pattern
rows = 5
for i in range(1, rows + 1):
# Print spaces
for j in range(rows - i):
print(" ", end="")
# Print stars
for j in range(2 * i - 1):
print("*", end="")
print() # Move to the next line
Explanation:
- Outer loop (
for i in range(1, rows + 1)
) runs for each row from 1 to 5. - First inner loop (
for j in range(rows - i)
) draws spaces. The number of spaces decreases as the row number increases. - Second inner loop (
for j in range(2 * i - 1)
) is used to print stars. At each row the number of printed stars increases in accordance with formula2 * i - 1
. - After each row,
print()
advances to the next line.
Output:
*
***
*****
*******
*********
Explanation of Output:
- Row 1: 4 spaces + 1 star.
- Row 2: 3 spaces + 3 stars.
- Row 3: 2 spaces + 5 stars.
- Row 4: 1 space + 7 stars.
- Row 5: 0 spaces + 9 stars.
4. Diamond Pattern
rows = 5
# Upper part
for i in range(1, rows + 1):
for j in range(rows - i):
print(" ", end="")
for j in range(2 * i - 1):
print("*", end="")
print()
# Lower part
for i in range(rows - 1, 0, -1):
for j in range(rows - i):
print(" ", end="")
for j in range(2 * i - 1):
print("*", end="")
print()
Explanation:
- Upper Part:
- The outer loop (
for i in range(1, rows + 1)
) runs for each row from 1 to 5. - The first inner loop prints spaces before stars.
- The second inner loop prints stars. The number of stars increases following the formula
2 * i - 1
.
- The outer loop (
- Lower Part:
- The outer loop (
for i in range(rows - 1, 0, -1)
) runs in reverse, starting from 4 down to 1. - Similar inner loops for spaces and stars, but both with decreasing values.
- The outer loop (
Output:
*
***
*****
*******
*********
*******
*****
***
*
Explanation of Output:
- Upper Part:
- Row 1: 4 spaces + 1 star.
- Row 2: 3 spaces + 3 stars.
- Row 3: 2 spaces + 5 stars.
- Row 4: 1 space + 7 stars.
- Row 5: 0 spaces + 9 stars.
- Lower Part:
- Row 6: 1 space + 7 stars.
- Row 7: 2 spaces + 5 stars.
- Row 8: 3 spaces + 3 stars.
- Row 9: 4 spaces + 1 star.
Tips for Writing Patterns
- Start Simple: Use simple patterns, like triangles or squares.
- Plan the Rows and Columns: Write down the logic for how the number of characters changes row by row.
- Understand Space Management: Patterns like pyramids or diamonds often have spaces. Use inner loops to handle spaces.
- Use Variables for Flexibility: Use variables like
rows
to control the size of the pattern easily.