0% found this document useful (0 votes)
4 views7 pages

Python Star Patterns

The document provides a series of Python code snippets for generating various patterns using asterisks and numbers, including right-angled triangles, pyramids, diamonds, and hollow shapes. Each pattern is accompanied by its corresponding output for clarity. These examples serve as a practical guide for understanding nested loops and string manipulation in Python.

Uploaded by

manjuan901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Python Star Patterns

The document provides a series of Python code snippets for generating various patterns using asterisks and numbers, including right-angled triangles, pyramids, diamonds, and hollow shapes. Each pattern is accompanied by its corresponding output for clarity. These examples serve as a practical guide for understanding nested loops and string manipulation in Python.

Uploaded by

manjuan901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Right-Angled Triangle

rows = 5
for i in range(1, rows + 1):
print("*" * i)

Output:

*
**
***
****
*****

2. Inverted Right-Angled Triangle

rows = 5
for i in range(rows, 0, -1):
print("*" * i)

Output:

*****
****
***
**
*
3. Pyramid Pattern

rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))

Output:

*
***
*****
*******
*********

4. Inverted Pyramid

rows = 5
for i in range(rows, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))

Output:

*********
*******
*****
***
*
5. Diamond Pattern

rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
for i in range(rows - 1, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))

Output:

*
***
*****
*******
*********
*******
*****
***
*

6. Right-Angled Triangle (Aligned to Right)

rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * i)
Output:

*
**
***
****
*****

7. Sandglass Pattern
rows = 5
for i in range(rows, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))
for i in range(2, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))

Output:

*********
*******
*****
***
*
***
*****
*******
*********
8. Hollow Pyramid

rows = 5
for i in range(1, rows + 1):
if i == 1 or i == rows:
print(" " * (rows - i) + "*" * (2 * i - 1))
else:
print(" " * (rows - i) + "*" + " " * (2 * i - 3) + "*")

Output:

*
* *
* *
* *
*********

9. Hollow Diamond

rows = 5
for i in range(1, rows + 1):
if i == 1:
print(" " * (rows - i) + "*")
else:
print(" " * (rows - i) + "*" + " " * (2 * i - 3) + "*")
for i in range(rows - 1, 0, -1):
if i == 1:
print(" " * (rows - i) + "*")
else:
print(" " * (rows - i) + "*" + " " * (2 * i - 3) + "*")
Output:

*
* *
* *
* *
* *
* *
* *
* *
*

10. Number Pyramid


rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + " ".join(str(j) for j in
range(1, i + 1)))

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
These patterns cover basic and advanced
levels. They are excellent for learning how
nested loops and string manipulations work in
Python.

For more such Content Repost in your network and


Follow

You might also like