[Go to site: main page, start]

0% found this document useful (0 votes)
3 views5 pages

Pattern Python Code

The document provides a series of code snippets that generate various patterns using asterisks, numbers, and letters in Python. Patterns include a square, right triangle, reverse triangle, number triangle, same number pattern, pyramid, inverted pyramid, Floyd's triangle, alphabet triangle, and a hollow square. Each pattern is accompanied by its corresponding code and output for clarity.

Uploaded by

lavanyaya6696
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)
3 views5 pages

Pattern Python Code

The document provides a series of code snippets that generate various patterns using asterisks, numbers, and letters in Python. Patterns include a square, right triangle, reverse triangle, number triangle, same number pattern, pyramid, inverted pyramid, Floyd's triangle, alphabet triangle, and a hollow square. Each pattern is accompanied by its corresponding code and output for clarity.

Uploaded by

lavanyaya6696
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

1.

Square Pattern

Code:

for i in range(5):
print("* " * 5)

Output:

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

2. Right Triangle

Code:

for i in range(1, 6):


print("* " * i)

Output:

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

3. Reverse Triangle

Code:

for i in range(5, 0, -1):


print("* " * i)

Output:

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

4. Number Triangle

Code:

for i in range(1, 6):


for j in range(1, i + 1):
print(j, end=" ")
print()

Output:

1
12
123
1234
12345

5. Same Number Pattern

Code:

for i in range(1, 6):


print((str(i) + " ") * i)

Output:

1
22
333
4444
55555

6. Pyramid Pattern

Code:

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

Output:

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

7. Inverted Pyramid

Code:

rows = 5

for i in range(rows, 0, -1):


print(" " * (rows - i) + "* " * i)

Output:

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

8. Floyd’s Triangle

Code:

num = 1

for i in range(1, 6):


for j in range(i):
print(num, end=" ")
num += 1
print()

Output:
1
23
456
7 8 9 10
11 12 13 14 15

9. Alphabet Triangle

Code:

for i in range(65, 70):


for j in range(65, i + 1):
print(chr(j), end=" ")
print()

Output:

A
AB
ABC
ABCD
ABCDE

10. Hollow Square

Code:

size = 5

for i in range(size):
for j in range(size):
if i == 0 or i == size - 1 or j == 0 or j == size - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print()
Output:

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

You might also like