1 5 9 13 Pattern
In this lesson, I show you how to print patterns in Python. The following Python programs utilize for loop, while loop, and range() part to display various patterns.
This commodity let you lot know how to print the following patterns in Python.
- Number design
- Triangle pattern
- Star (*) or asterisk design
- Pyramid pattern
- Inverted pyramid pattern
- Half pyramid pattern
- Diamond Shaped pattern
- Characters or alphabets design
- Square pattern
By printing different patterns, you can build a solid understanding of loops in Python. After reading this article you can create various types of patterns.
Steps to Print Design in Python
Us the below steps to print pattern in Python
- Decide the number of rows and columns
There is a typical structure to print whatever pattern, i.e., the number of rows and columns. We need to use 2 loops to print whatever design, i.e., use nested loops.
The outer loop tells us the number of rows, and the inner loop tells u.s. the column needed to print the pattern.
Accept the number of rows from a user using the
input()
function to decide the size of a pattern. - Iterate rows
Side by side, write an outer loop to Iterate the number of rows using a for loop and
range()
office. - Iterate columns
Next, write the inner loop or nested loop to handle the number of columns. The internal loop iteration depends on the values of the outer loop.
- Print star or number
Use the
print()
function in each iteration of nestedfor
loop to display the symbol or number of a blueprint (like a star (asterisk*
) or number). - Add new line after each iteration of outer loop
Add a new line using the
print()
role after each iteration of the outer loop so that the pattern display accordingly
Table of contents
- Steps to Impress Blueprint in Python
- Programs to impress number pattern
- Pyramid pattern of numbers
- Inverted pyramid pattern of numbers
- Inverted Pyramid pattern with the same digit
- Another inverted one-half pyramid blueprint with number
- Alternate numbers design using while loop
- Reverse number design
- Reverse Pyramid of Numbers
- Another contrary number blueprint
- Impress reverse number from 10 to 1
- Number triangle pattern
- Pascal's triangle pattern using numbers
- Foursquare pattern with numbers
- Multiplication table blueprint
- Pyramid pattern of stars in python
- Right triangle pyramid of Stars
- Downward half-Pyramid Pattern of Star
- Downward full Pyramid Design of star
- Right down mirror star Design
- Equilateral triangle pattern of star
- Impress two pyramids of stars
- Correct start blueprint of star
- Left triangle pascal's pattern
- Sandglass pattern of star
- Pant manner pattern of stars
- Diamond-shaped pattern of stars
- Another diamond pattern of star
- Alphabets and letters blueprint
- Pattern to display letter of the give-and-take
- Equilateral triangle pattern of characters/alphabets
- Design of same grapheme
- More miscellaneous Patterns
- Pyramid of horizontal number tables
- Double the number design
- Random number pattern
- Pyramid of numbers less than 10
- Pyramid of numbers up to ten
- Even number blueprint
- Unique pyramid pattern of digits
- Pattern double number on each column
- Number reduction pattern
- Pant fashion pattern of numbers
- Pattern with a combination of numbers and stars
- Practice Trouble
- Next Steps
Programs to impress number pattern
I have created various programs that print different styles of number patterns. Permit see them one by one.
Let's see the Python program to print the following simple number pattern using a for loop.
ane 2 2 3 3 3 4 4 4 iv 5 5 v 5 5
Program:
rows = 6 # if you desire user to enter a number, uncomment the below line # rows = int(input('Enter the number of rows')) # outer loop for i in range(rows): # nested loop for j in range(i): # display number print(i, terminate=' ') # new line subsequently each row print('')
In this number pattern, we displayed a single digit on the first row, the side by side two digits of the 2nd row, And the following three numbers on the 3rd row and this process will repeat till the number of rows reached.
Annotation:
- The count of numbers on each row is equal to the electric current row number.
- Also, each number is separated by space.
- We used a nested loop to print pattern
Pyramid design of numbers
Allow'southward come across how to print the post-obit one-half pyramid pattern of numbers
1 i two one ii 3 1 2 3 4 1 two 3 4 5
Note: In each row, every adjacent number is incremented by 1.
Program:
rows = five for i in range(1, rows + 1): for j in range(1, i + 1): impress(j, end=' ') print('')
Inverted pyramid design of numbers
An inverted pyramid is a downward design where numbers become reduced in each iteration, and on the last row, it shows but one number. Employ reverse for loop to impress this pattern.
Pattern
1 1 one 1 1 2 2 2 ii three 3 3 iv 4 five
Programme
rows = 5 b = 0 # opposite for loop from 5 to 0 for i in range(rows, 0, -1): b += 1 for j in range(1, i + 1): print(b, stop=' ') print('\r')
Inverted Pyramid pattern with the aforementioned digit
Design: –
v five five 5 5 5 5 five five 5 5 v 5 5 5
Programme: –
rows = 5 num = rows # opposite for loop for i in range(rows, 0, -i): for j in range(0, i): print(num, terminate=' ') impress("\r")
Another inverted half pyramid pattern with number
Pattern: –
0 1 two 3 four five 0 1 ii three four 0 one 2 3 0 1 2 0 one
Plan
rows = v for i in range(rows, 0, -1): for j in range(0, i + 1): impress(j, cease=' ') print("\r")
Alternate numbers pattern using while loop
Allow's come across how to utilize the while loop to impress the number pattern.
Pattern: –
1 3 iii 5 5 5 7 vii 7 seven 9 9 ix ix ix
Program: –
rows = v i = 1 while i <= rows: j = 1 while j <= i: impress((i * 2 - 1), end=" ") j = j + 1 i = i + 1 print('')
Reverse number blueprint
Permit's run across how to brandish the pattern of descending gild of numbers
Pattern one: –
five 5 5 5 5 4 four 4 iv three iii 3 2 2 ane
This design is likewise called as a inverted pyramid of descending numbers.
Program: –
rows = five # reverse loop for i in range(rows, 0, -1): num = i for j in range(0, i): print(num, terminate=' ') print("\r")
Reverse Pyramid of Numbers
Pattern 2: –
1 ii 1 3 2 i 4 3 2 1 v 4 3 2 ane
Note: It is a downwardly increment pattern where numbers get increased in each iteration. At each row, the amount of number is equal to the electric current row number.
Plan
rows = 6 for i in range(ane, rows): for j in range(i, 0, -1): impress(j, end=' ') print("")
Some other reverse number pattern
Pattern: –
v 4 3 2 ane 4 3 ii 1 3 two 1 2 1 1
Program: –
rows = 5 for i in range(0, rows + i): for j in range(rows - i, 0, -one): print(j, end=' ') print()
Print opposite number from ten to i
Design: –
1 3 2 six 5 4 x 9 8 7
Programme: –
commencement = 1 stop = two current_num = terminate for row in range(2, 6): for col in range(start, stop): current_num -= 1 print(current_num, end=' ') print("") showtime = stop stop += row current_num = terminate
Number triangle design
Let's meet how to print the right-angled triangle pattern of numbers
Pattern: –
1 1 2 1 2 iii ane ii 3 4 1 2 iii 4 v
Program: –
rows = 6 for i in range(1, rows): num = ane for j in range(rows, 0, -1): if j > i: print(" ", cease=' ') else: impress(num, finish=' ') num += ane print("")
Pascal'south triangle pattern using numbers
To build the pascal triangle, outset with "ane" at the pinnacle, then continue placing numbers below information technology in a triangular design.
Each number is the numbers straight above it added together.
Pattern:
i 1 1 one 2 ane i 3 3 1 1 4 half dozen iv 1 1 5 10 10 5 ane 1 6 15 xx 15 6 1
Program: –
def print_pascal_triangle(size): for i in range(0, size): for j in range(0, i + one): print(decide_number(i, j), cease=" ") print() def decide_number(n, k): num = ane if chiliad > n - grand: k = northward - k for i in range(0, k): num = num * (n - i) num = num // (i + 1) return num # set rows rows = 7 print_pascal_triangle(rows)
Square pattern with numbers
Pattern: –
1 two 3 four 5 2 ii 3 iv 5 3 three 3 4 5 4 4 4 4 5 5 v 5 v 5
Program: –
rows = 5 for i in range(ane, rows + i): for j in range(one, rows + one): if j <= i: print(i, end=' ') else: print(j, end=' ') print()
Multiplication table pattern
Pattern: –
1 two iv 3 6 9 iv viii 12 16 5 10 15 20 25 six 12 18 24 30 36 7 14 21 28 35 42 49 eight 16 24 32 40 48 56 64
Program: –
rows = viii # rows = int(input("Enter the number of rows ")) for i in range(1, rows + 1): for j in range(1, i + 1): # multiplication current column and row square = i * j print(i * j, end=' ') print()
Pyramid pattern of stars in python
This department will run across how to impress pyramid and Star (asterisk) patterns in Python. Here we will impress the post-obit pyramid design with Star (asterisk).
- Half pyramid pattern with stars(*)
- Full pyramid pattern with stars
- Inverted pyramid Pattern with stars
- Triangle blueprint with stars
- Correct-angled triangle pattern with stars
Simple half pyramid pattern: –
* * * * * * * * * * * * * * *
This pattern is also known every bit a right angle triangle pyramid.
Program: –
# number of rows rows = 5 for i in range(0, rows): # nested loop for each column for j in range(0, i + 1): # print star print("*", stop=' ') # new line after each row impress("\r")
Right triangle pyramid of Stars
Pattern: –
* * * * * * * * * * * * * * *
This blueprint is also called as mirrored right triangle
Program: –
# number of rows rows = 5 thousand = 2 * rows - 2 for i in range(0, rows): # process each column for j in range(0, k): # print infinite in pyramid print(end=" ") thousand = m - 2 for j in range(0, i + 1): # display star print("* ", terminate="") print("")
Culling Solution:
rows = v for j in range(1, rows+1): print("* " * j)
Downwards one-half-Pyramid Design of Star
Pattern: –
* * * * * * * * * * * * * * *
Notation: Nosotros demand to apply the reverse nested loop to impress the downwardly pyramid blueprint of stars
Program: –
rows = 5 for i in range(rows + 1, 0, -i): # nested reverse loop for j in range(0, i - 1): # brandish star print("*", end=' ') impress(" ")
Downward full Pyramid Pattern of star
Let's see how to print reversed pyramid design in Python.
Pattern: –
* * * * * * * * * * * * * * * * * * * * *
Program:
rows = v k = ii * rows - 2 for i in range(rows, -1, -1): for j in range(chiliad, 0, -i): print(stop=" ") k = one thousand + 1 for j in range(0, i + 1): print("*", terminate=" ") print("")
Right downward mirror star Pattern
Pattern: –
***** **** *** ** *
In this pattern, we demand to use two nested while
loops.
Program: –
rows = 5 i = rows while i >= ane: j = rows while j > i: # brandish infinite impress(' ', terminate=' ') j -= 1 k = i while k <= i: print('*', end=' ') g += 1 impress() i -= ane
Equilateral triangle pattern of star
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Plan: –
print("Print equilateral triangle Pyramid using asterisk symbol ") # printing full Triangle pyramid using stars size = 7 m = (2 * size) - 2 for i in range(0, size): for j in range(0, m): print(end=" ") # decrementing one thousand subsequently each loop m = m - 1 for j in range(0, i + 1): print("* ", end=' ') print(" ")
Print two pyramids of stars
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Plan: –
rows = six for i in range(0, rows): for j in range(0, i + one): impress("*", end=' ') print(" ") print(" ") for i in range(rows + i, 0, -1): for j in range(0, i - 1): impress("*", end=' ') impress(" ")
Right start blueprint of star
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * *
Nosotros also telephone call this blueprint as a right pascal's triangle.
Program: –
rows = 5 for i in range(0, rows): for j in range(0, i + 1): print("*", end=' ') print("\r") for i in range(rows, 0, -1): for j in range(0, i - i): print("*", end=' ') impress("\r")
Left triangle pascal's design
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * *
Program: –
rows = 5 i = 1 while i <= rows: j = i while j < rows: # display infinite print(' ', end=' ') j += i k = i while chiliad <= i: print('*', stop=' ') k += 1 print() i += 1 i = rows while i >= 1: j = i while j <= rows: print(' ', terminate=' ') j += 1 k = 1 while k < i: impress('*', end=' ') k += 1 print('') i -= 1
Sandglass pattern of star
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
To print this design nosotros need to apply two set of 3 while loops.
Program: –
rows = v i = 0 while i <= rows - one: j = 0 while j < i: # display space print('', terminate=' ') j += 1 k = i while 1000 <= rows - 1: print('*', end=' ') yard += 1 print() i += 1 i = rows - 1 while i >= 0: j = 0 while j < i: print('', end=' ') j += 1 k = i while chiliad <= rows - 1: impress('*', cease=' ') one thousand += 1 impress('') i -= 1
Pant fashion pattern of stars
Pattern: –
**************** *******__******* ******____****** *****______***** ****________**** ***__________*** **____________** *______________*
Programme: –
rows = xiv print("*" * rows, end="\due north") i = (rows // ii) - 1 j = 2 while i != 0: while j <= (rows - 2): print("*" * i, end="") impress("_" * j, end="") print("*" * i, end="\n") i = i - 1 j = j + 2
Diamond-shaped pattern of stars
Blueprint: –
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Programme: –
rows = 5 one thousand = ii * rows - 2 for i in range(0, rows): for j in range(0, k): print(end=" ") k = k - i for j in range(0, i + ane): print("* ", end="") print("") k = rows - ii for i in range(rows, -1, -1): for j in range(thousand, 0, -i): impress(end=" ") k = k + 1 for j in range(0, i + 1): print("* ", end="") print("")
Another diamond blueprint of star
Pattern: –
* * * * * * * * * * * * * * * *
Program: –
rows = 5 i = i while i <= rows: j = rows while j > i: # display space print(' ', end=' ') j -= 1 print('*', end=' ') k = one while yard < 2 * (i - one): impress(' ', terminate=' ') yard += 1 if i == i: print() else: impress('*') i += one i = rows - 1 while i >= 1: j = rows while j > i: print(' ', cease=' ') j -= 1 print('*', end=' ') k = ane while k <= two * (i - ane): print(' ', terminate=' ') k += 1 if i == i: impress() else: print('*') i -= 1
Alphabets and messages design
In Python, there are ASCII values for each letter of the alphabet. To impress the patterns of letters and alphabets, we need to convert them to their ASCII values.
- Decide the number of rows
- Commencement with ASCII number 65 ( 'A')
- Iterate a loop and in nested
for
loop use thechar
function to catechumen ASCII number to its equivalent letter.
Let' run into now how to impress alphabets and messages patterns in Python.
Pattern: –
A B C D E F G H I J K L 1000 N O P Q R S T U V W Ten Y Z [ \
This design is knows equally right-angled pattern with characters.
Program: –
# ASCII number of 'A' ascii_number = 65 rows = 7 for i in range(0, rows): for j in range(0, i + i): grapheme = chr(ascii_number) impress(character, finish=' ') ascii_number += ane print(" ")
Pattern to display letter of the give-and-take
Let'due south see how to print word 'Python' in Design: –
P Py Pyt Pyth Pytho Python
Program: –
word = "Python" x = "" for i in give-and-take: x += i print(ten)
Equilateral triangle design of characters/alphabets
Pattern: –
A B C D Due east F Chiliad H I J Yard L G Northward O P Q R S T U V West Ten Y Z [ \
Program: –
print("Impress equilateral triangle Pyramid with characters ") size = 7 asciiNumber = 65 chiliad = (2 * size) - 2 for i in range(0, size): for j in range(0, m): print(finish=" ") m = m - 1 for j in range(0, i + ane): character = chr(asciiNumber) print(character, end=' ') asciiNumber += one impress(" ")
Pattern of aforementioned character
Design: –
Five Five V V 5 V V V V Five V V V V V
Program: –
# Same character blueprint grapheme = 'V' # catechumen char to ASCII char_ascii_no = ord(character) for i in range(0, v): for j in range(0, i + 1): # Convert the ASCII value to the grapheme user_char = chr(char_ascii_no) impress(user_char, stop=' ') print()
Let's see some more miscellaneous patterns
More miscellaneous Patterns
Pyramid of horizontal number tables
Pattern: –
1 two 4 3 vi 9 iv 8 12 16 v x 15 20 25 half dozen 12 18 24 30 36 vii 14 21 28 35 42 49 8 xvi 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 thirty 40 50 60 70 80 90 100
Plan: –
# Pyramid of horizontal tables of numbers rows = 10 for i in range(1, rows + 1): for j in range(1, i + one): print(i * j, finish=' ') print()
Double the number pattern
Pattern: –
1 2 i 4 2 1 8 4 ii i 16 8 4 2 1 32 sixteen viii 4 two 1 64 32 16 viii 4 2 i 128 64 32 16 viii 4 2 1
Note: In each column, every number is double it'south the preceding number.
Program: –
rows = nine for i in range(one, rows): for j in range(-1 + i, -1, -ane): impress(format(ii ** j, "4d"), stop=' ') print("")
Random number blueprint
i i ii i one 2 4 2 1 1 2 4 8 4 2 i 1 2 4 8 16 8 4 two 1 1 two 4 8 16 32 sixteen 8 iv 2 i i 2 iv viii 16 32 64 32 sixteen eight four 2 1 1 two 4 eight sixteen 32 64 128 64 32 sixteen 8 4 2 i
Program: –
rows = 9 for i in range(1, rows): for i in range(0, i, ane): print(format(2 ** i, "4d"), end=' ') for i in range(-1 + i, -one, -1): print(format(2 ** i, "4d"), end=' ') print("")
Pyramid of numbers less than x
Pattern: –
1 2 iii iv five 6 vii 8 9
Program: –
current_num = 1 stop = 2 rows = 3 for i in range(rows): for column in range(1, end): print(current_num, stop=' ') current_num += 1 impress("") stop += ii
Pyramid of numbers up to 10
Blueprint: –
ane 2 3 4 v 6 vii 8 9 x
Plan: –
current_num = 1 rows = 4 finish = ii for i in range(rows): for column in range(ane, finish): impress(current_num, end=' ') current_num += 1 impress("") stop += 1
Even number pattern
Pattern: –
10 ten 8 10 8 half dozen 10 8 half dozen iv 10 viii 6 4 two
Programs: –
rows = five last_num = ii * rows even_num = last_num for i in range(1, rows + ane): even_num = last_num for j in range(i): print(even_num, terminate=' ') even_num -= two print("\r")
Unique pyramid pattern of digits
Blueprint: –
ane 1 2 1 1 ii three 2 1 ane 2 3 4 3 2 1 1 2 3 4 5 4 3 two 1
Programme: –
rows = 6 for i in range(1, rows + i): for j in range(one, i - 1): print(j, finish=" ") for j in range(i - i, 0, -1): print(j, stop=" ") print()
Blueprint double number on each cavalcade
Pattern: –
0 0 1 0 2 4 0 iii 6 9 0 4 8 12 xvi 0 v x fifteen 20 25 0 6 12 18 24 xxx 36
Plan: –
rows = 7 for i in range(0, rows): for j in range(0, i + i): print(i * j, cease=' ') print()
Number reduction pattern
Pattern: –
1 ii 3 4 5 2 three 4 5 iii four 5 4 5 5
Programme: –
rows = 5 for i in range(0, rows + ane, 1): for j in range(i + 1, rows + 1, one): print(j, end=' ') print()
Pant style blueprint of numbers
Pattern: –
v 4 iii ii 1 1 two 3 4 5 five 4 three ii 2 3 4 5 5 4 3 three 4 five v four 4 v 5 5
Program: –
rows = 6 for i in range(0, rows): for j in range(rows - 1, i, -1): impress(j, '', finish='') for l in range(i): print(' ', end='') for 1000 in range(i + 1, rows): print(k, '', stop='') impress('\n')
Design with a combination of numbers and stars
Pattern: –
1 * ii * 3 * 4 one * 2 * three 1 * 2 one
Program: –
row = 4 for i in range(0, row): c = 1 print(c, end=' ') for j in range(row - i - ane, 0, -one): print('*', cease=' ') c = c + 1 print(c, terminate=' ') print('\n')
Also, run across how to calculate the sum and average in Python.
Practise Trouble
Design: –
0 2 4 4 8 8 8 xvi 16 16
Solution: –
num = 4 counter = 0 for 10 in range(0, num): for y in range(0, x + ane): print(counter, end=" ") counter = two ** (x + 1) print()
Next Steps
Solve:
- Python Basic Practise for Beginners
- Python exercise for beginners
- Python Quiz for beginners
If you don't find the blueprint you are looking for, let me know by leaving a annotate and questions below.
1 5 9 13 Pattern,
Source: https://pynative.com/print-pattern-python-examples/
Posted by: wootenaskinkin.blogspot.com
0 Response to "1 5 9 13 Pattern"
Post a Comment