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

Printing Number Patterns

Cs

Uploaded by

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

Printing Number Patterns

Cs

Uploaded by

Pooja Kulkarni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

R

L
N
EE
PYTHON TIPS and TRICKS:
H
PRINTING VARIOUS NUMBER
PATTERNS
AT
M
ED
M
M
A
H
O
M

August 15, 2024


PYTHON TIPS and TRICKS: PRINTING VARIOUS NUMBER PATTERNS August 15, 2024

Contents

PYTHON TIPS AND TRICKS: PRINTING VARIOUS NUMBER PATTERNS 2


Code for generating Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Explanation of Number Patterns: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
number_triangle: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
reverse_number_triangle: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
number_pyramid: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

R
reverse_number_pyramid: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
continuous_number_triangle: . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

L
mirrored_number_triangle: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
centered_number_pyramid: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

N
reverse_centered_number_pyramid: . . . . . . . . . . . . . . . . . . . . . . . . 5

EE
H
AT
M
ED
M
M
A
H
O
M

1
PYTHON TIPS and TRICKS: PRINTING VARIOUS NUMBER PATTERNS August 15, 2024

August 15, 2024

PYTHON TIPS AND TRICKS: PRINTING VARIOUS NUMBER


PATTERNS

Code for generating Patterns

This function accepts pattern-type and number of rows as parameters to produce various number patterns.

R
Output has been demonstrated below:

L
def print_pattern(pattern_type, rows):
for i in range(1, rows + 1):

N
if pattern_type == 'number_triangle':

EE
# Print numbers from 1 to i
for num in range(1, i + 1):
print(num, end=" ")
print()
H
AT
elif pattern_type == 'reverse_number_triangle':
# Print numbers in reverse from i to 1
M

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


print(num, end=" ")
print()
ED

elif pattern_type == 'number_pyramid':


# Print spaces followed by numbers from 1 to i
M

for space in range(rows - i):


print(' ', end="")
M

for num in range(1, i + 1):


print(num, end=" ")
A

print()
H

elif pattern_type == 'reverse_number_pyramid':


O

# Print spaces followed by numbers from 1 to rows - i + 1


M

for space in range(i - 1):


print(' ', end="")
for num in range(1, rows - i + 2):
print(num, end=" ")
print()

elif pattern_type == 'continuous_number_triangle':


# Print continuous numbers across the rows
count = 1

2
PYTHON TIPS and TRICKS: PRINTING VARIOUS NUMBER PATTERNS August 15, 2024

for j in range(1, i + 1):


print(count, end=" ")
count += 1
print()

elif pattern_type == 'mirrored_number_triangle':


# Print spaces followed by numbers from 1 to i
for space in range(rows - i):
print(' ', end="")
for num in range(1, i + 1):

R
print(num, end=" ")
print()

L
N
elif pattern_type == 'centered_number_pyramid':
# Print spaces followed by increasing and then decreasing numbers

EE
for space in range(rows - i):
print(' ', end="")
for num in range(1, i + 1):
print(num, end=" ")
H
AT
for num in range(i - 1, 0, -1):
print(num, end=" ")
print()
M

elif pattern_type == 'reverse_centered_number_pyramid':


# Print spaces followed by increasing and then decreasing numbers in reverse
ED

↪ pyramid
for space in range(i - 1):
M

print(' ', end="")


for num in range(1, rows - i + 2):
M

print(num, end=" ")


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

print(num, end=" ")


print()
H

# Example Usage
O

print("Number Triangle:")
M

print_pattern('number_triangle', 5)
print("\nReverse Number Triangle:")
print_pattern('reverse_number_triangle', 5)
print("\nNumber Pyramid:")
print_pattern('number_pyramid', 5)
print("\nReverse Number Pyramid:")
print_pattern('reverse_number_pyramid', 5)
print("\nContinuous Number Triangle:")
print_pattern('continuous_number_triangle', 5)

3
PYTHON TIPS and TRICKS: PRINTING VARIOUS NUMBER PATTERNS August 15, 2024

print("\nMirrored Number Triangle:")


print_pattern('mirrored_number_triangle', 5)
print("\nCentered Number Pyramid:")
print_pattern('centered_number_pyramid', 5)
print("\nReverse Centered Number Pyramid:")
print_pattern('reverse_centered_number_pyramid', 5)

Explanation of Number Patterns:

R
number_triangle: Numbers increase in a right-aligned triangle:

L
1
1 2

N
1 2 3

EE
1 2 3 4
1 2 3 4 5

H
AT
reverse_number_triangle: Numbers are printed in reverse order in a triangle:

1
M

2 1
3 2 1
ED

4 3 2 1
5 4 3 2 1
M

number_pyramid: Numbers increase in a centered pyramid:


M

1
A

1 2
H

1 2 3
1 2 3 4
O

1 2 3 4 5
M

reverse_number_pyramid: Reverse centered pyramid:

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

4
PYTHON TIPS and TRICKS: PRINTING VARIOUS NUMBER PATTERNS August 15, 2024

continuous_number_triangle: Numbers continuously increment across rows:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

R
mirrored_number_triangle: Right-aligned number triangle:

L
1

N
1 2
1 2 3

EE
1 2 3 4
1 2 3 4 5

H
AT
centered_number_pyramid: Numbers form a centered pyramid that mirrors on each side:

1
M

1 2 1
1 2 3 2 1
ED

1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
M

reverse_centered_number_pyramid: The reverse of the centered pyramid:


M
A

1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
H

1 2 3 2 1
O

1 2 1
1
M

This covers a wide range of number patterns and offers flexibility to generate various kinds of structures. You
can further extend this by adding more specific patterns based on your needs.

For more Python Tips and Tricks Visit:

https://matheenhere.blogspot.com

You might also like