0% found this document useful (0 votes)
12 views

14. Star Pattern in Python

The document provides an introduction to creating star patterns in Python using loops, emphasizing the use of nested loops for generating various designs. It outlines the steps to define the pattern's size, choose the loop structure, and set up the loop logic for printing characters. Several examples illustrate different star patterns, including a multiplication table and a diamond star pattern.

Uploaded by

prr technologies
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

14. Star Pattern in Python

The document provides an introduction to creating star patterns in Python using loops, emphasizing the use of nested loops for generating various designs. It outlines the steps to define the pattern's size, choose the loop structure, and set up the loop logic for printing characters. Several examples illustrate different star patterns, including a multiplication table and a diamond star pattern.

Uploaded by

prr technologies
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Star Pattern in Python

By Mr.PRR
Introduction
• Patterns are a fundamental concept in programming that allow you to create visually
appealing designs using code. In Python, you can use loops & other basic
programming constructs to generate various types of patterns, including star
patterns.

• Star patterns are a popular choice for beginners learning Python because they
provide an engaging way to practice basic coding skills while creating something
interesting.
Approach to Solve a Pattern

• Creating a patterns in Python mainly involves using loops—for or while. A


loop allows us to repeat a set of commands multiple times, which is essential
for printing rows of patterns.

• We need to employ two loops, also known as nested loops, to print any
pattern. While the outer loop supplies the number of rows, the inner loop
tells us the column needed to print the pattern.
Steps to Print Pattern in Python

When tackling patterns in Python, following a systematic approach can simplify the process.
Here are the general steps you might consider:

• Define the Pattern's Size: Decide how large the pattern should be. This usually involves
determining the number of rows, which will affect all other dimensions of the pattern.

• Choose the Loop Structure: Decide whether to use a for loop or a while loop. For
loops are often easier to manage because they are ideal for executing a block of code a
specific number of times, which is typical in pattern problems.
Set Up the Loop Logic

• Outer Loop: This loop runs once for each row of the pattern. It controls
the number of lines your pattern will have.

• Inner Loop(s): These loops are nested within the outer loop and are
responsible for printing the stars (*) and spaces ( ) on each line. The
conditions in these loops will vary depending on the pattern's shape (like
aligning stars to the left or right).
• Print Characters: Inside the inner loop(s), use the print() function to output the
correct character (star or space). Use end='' in the print() function to avoid moving
to a new line after each character.

• New Line After Each Row: After each iteration of the outer loop (each row is
completed), print a new line character to move the cursor to the next line.
Example-1
rows = 6
for i in range(rows):
for j in range(i):
print(i, end=' ')
print('')
Example-2

rows = 5
b=0
for i in range(rows, 0, -1):
b += 1
for j in range(1, i + 1):
print(b, end=' ')
print('\r')
Example-3(Multiplication table pattern)

rows = 8
for i in range(1, rows + 1):
for j in range(1, i + 1):
square = i * j
print(i * j, end=' ')
print()
Example- 4 (Downward star pyramid pattern)

rows = 5
k = 2 * rows - 2
for i in range(rows, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k=k+1
for j in range(0, i + 1):
print("*", end=" ")
print("")
Example-5(Diamond Star Pattern Program)

for i in range(5):
for j in range(5):
if i + j == 2 or i - j == 2 or i + j
== 6 or j - i == 2:
print("*", end="")
else:
print(end=" ")
print()

You might also like