Printing patterns is a common exercise in Python that helps beginners understand loops and conditional statements. Below are examples of how to print different types of patterns in Python using nested loops.
1. Printing a Simple Triangle Pattern
A basic triangle pattern can be printed using a nested loop, where the outer loop controls the number of rows and the inner loop controls the number of columns in each row.
Example:
# Simple triangle pattern
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print("*", end=" ")
print("")
Output:
*
* *
* * *
* * * *
* * * * *
2. Printing an Inverted Triangle Pattern
You can print an inverted triangle pattern by adjusting the number of columns printed in each row.
Example:
# Inverted triangle pattern
rows = 5
for i in range(rows, 0, 1):
for j in range(1, i + 1):
print("*", end=" ")
print("")
Output:
* * * * *
* * * *
* * *
* *
*
3. Printing a Pyramid Pattern
A pyramid pattern can be printed by centering the stars in each row. This requires adjusting the spaces before the stars in each row.
Example:
# Pyramid pattern
rows = 5
for i in range(1, rows + 1):
print(" " * (rows  i), end="")
print("* " * i)
Output:
*
* *
* * *
* * * *
* * * * *
4. Printing a Diamond Pattern
A diamond pattern can be created by combining a pyramid pattern and an inverted pyramid pattern.
Example:
# Diamond pattern
rows = 5
# Top part of the diamond
for i in range(1, rows + 1):
print(" " * (rows  i), end="")
print("* " * i)
# Bottom part of the diamond
for i in range(rows  1, 0, 1):
print(" " * (rows  i), end="")
print("* " * i)
Output:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
5. Printing a Number Pattern
Instead of stars, you can print numbers in the pattern. Here’s an example of a simple number triangle.
Example:
# Number triangle pattern
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=" ")
print("")
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Hourglass Pattern Example
This code will print an hourglass pattern where the top part starts with the maximum number of dashes, decreasing by two dashes in each subsequent row, followed by a similar increasing pattern for the bottom part.
Example:
# Hourglass pattern using ''
rows = 5
# Top part of the hourglass
for i in range(rows, 0, 1):
print(" " * (rows  i) + "" * (2 * i  1))
# Bottom part of the hourglass
for i in range(2, rows + 1):
print(" " * (rows  i) + "" * (2 * i  1))
Output:









Explanation
In this example:
 The top part of the hourglass is created by iterating from
rows
down to 1, printing decreasing numbers of dashes, centered by spaces.  The bottom part of the hourglass is created by iterating from 2 to
rows
, printing increasing numbers of dashes, again centered by spaces.
How to Print a Same Number Pyramid Pattern in Python
A same number pyramid pattern involves printing a pyramid where each row contains the same number repeated. Below is an example of how to create such a pattern using nested loops in Python.
Same Number Pyramid Pattern Example
This code will print a pyramid where each row consists of the same number repeated, starting from 1 at the top and increasing with each row.
Example:
# Same number pyramid pattern
rows = 5
for i in range(1, rows + 1):
print(" " * (rows  i) + (str(i) + " ") * i)
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Explanation
In this example:
 The outer loop iterates through the numbers from 1 to
rows
.  For each row, the code prints a number of spaces to center the numbers, followed by the number itself repeated
i
times.
How to Print an Odd Numbers Pattern in Python
Printing an odd numbers pattern involves creating a pattern where each row consists of consecutive odd numbers. Below is an example of how to create such a pattern using nested loops in Python.
Odd Numbers Pattern Example
This code will print a pattern where each row starts with the smallest odd number and continues with the next odd numbers.
Example:
# Odd numbers pattern
rows = 5
current_odd = 1
for i in range(1, rows + 1):
for j in range(i):
print(current_odd, end=" ")
current_odd += 2
print("")
Output:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
Explanation
In this example:
 The outer loop controls the number of rows in the pattern.
 The inner loop prints the required number of odd numbers for each row.
 The variable
current_odd
starts at 1 and increments by 2 on each iteration to generate the next odd number.
“`html
How to Print a Square Pattern Using Characters in Python
A square pattern using characters involves printing a grid of characters where each row and column contains the same character. Below is an example of how to create such a pattern using nested loops in Python.
Square Pattern Example
This code will print a square pattern where each row consists of the same character repeated across the entire row.
Example:
# Square pattern using characters
size = 5
char = '*'
for i in range(size):
for j in range(size):
print(char, end=" ")
print("")
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Explanation
In this example:
 The outer loop runs
size
times, controlling the number of rows.  The inner loop also runs
size
times, controlling the number of columns.  The character
char
is printedsize
times in each row, forming a square pattern.  The
end=" "
in theprint()
function ensures that the characters are printed on the same line with a space in between.
Customizing the Pattern
You can easily customize this pattern by changing the size
and char
variables to create squares of different sizes and using different characters.
Example with Custom Character:
# Custom square pattern using characters
size = 4
char = '#'
for i in range(size):
for j in range(size):
print(char, end=" ")
print("")
Output:
# # # #
# # # #
# # # #
# # # #
How to Print Multiplication Numbers in Columns in Python
Printing multiplication numbers in columns involves displaying the multiplication tables for a range of numbers, each in its own column. Below is an example of how to create such a pattern using nested loops in Python.
Multiplication Numbers in Columns Example
This code will print the multiplication tables from 1 to 5, each table displayed in its own column.
Example:
# Multiplication numbers in columns
num = 5
max_val = 10
for i in range(1, max_val + 1):
for j in range(1, num + 1):
print(f"{j} x {i} = {j * i}", end="\t")
print("")
Output:
1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5
1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45
1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50
Explanation
In this example:
 The outer loop iterates from 1 to
max_val
(which is 10 in this case), controlling the row number.  The inner loop iterates from 1 to
num
(which is 5), controlling the column number.  For each iteration, the code prints the multiplication result in a formatted string, with each column separated by a tab character (
\t
).
Customizing the Pattern
You can easily customize this pattern by changing the num
and max_val
variables to generate multiplication tables for different ranges.
Example with More Columns:
# Multiplication numbers in columns for 1 to 10
num = 10
max_val = 10
for i in range(1, max_val + 1):
for j in range(1, num + 1):
print(f"{j} x {i} = {j * i}", end="\t")
print("")
Output:
1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5 6 x 1 = 6 7 x 1 = 7 8 x 1 = 8 9 x 1 = 9 10 x 1 = 10
1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10 6 x 2 = 12 7 x 2 = 14 8 x 2 = 16 9 x 2 = 18 10 x 2 = 20
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15 6 x 3 = 18 7 x 3 = 21 8 x 3 = 24 9 x 3 = 27 10 x 3 = 30
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20 6 x 4 = 24 7 x 4 = 28 8 x 4 = 32 9 x 4 = 36 10 x 4 = 40
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25 6 x 5 = 30 7 x 5 = 35 8 x 5 = 40 9 x 5 = 45 10 x 5 = 50
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36 7 x 6 = 42 8 x 6 = 48 9 x 6 = 54 10 x 6 = 60
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49 8 x 7 = 56 9 x 7 = 63 10 x 7 = 70
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 9 x 8 = 72 10 x 8 = 80
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81 10 x 9 = 90
1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50 6 x 10 = 60 7 x 10 = 70 8 x 10 = 80 9 x 10 = 90 10 x 10 = 100
How to Print Alphabet and Letter Patterns in Python
Printing alphabet and letter patterns is a common exercise in Python that helps in understanding loops and character manipulation. Below are examples of how to create various alphabet and letter patterns using nested loops in Python.
1. Simple Alphabet Triangle Pattern
This example prints a triangle pattern where each row contains consecutive letters of the alphabet.
Example:
# Simple alphabet triangle pattern
rows = 5
ascii_value = 65 # ASCII value of 'A'
for i in range(1, rows + 1):
for j in range(i):
print(chr(ascii_value + j), end=" ")
print("")
Output:
A
A B
A B C
A B C D
A B C D E
2. Alphabet Pyramid Pattern
This example prints a pyramid pattern where each row contains the same letter repeated across the row.
Example:
# Alphabet pyramid pattern
rows = 5
ascii_value = 65 # ASCII value of 'A'
for i in range(rows):
print(" " * (rows  i  1) + (chr(ascii_value + i) + " ") * (i + 1))
Output:
A
B B
C C C
D D D D
E E E E E
3. Inverted Alphabet Triangle Pattern
This example prints an inverted triangle pattern where each row starts with ‘A’ and continues with the next letters of the alphabet.
Example:
# Inverted alphabet triangle pattern
rows = 5
ascii_value = 65 # ASCII value of 'A'
for i in range(rows, 0, 1):
for j in range(i):
print(chr(ascii_value + j), end=" ")
print("")
Output:
A B C D E
A B C D
A B C
A B
A
4. Alphabet Diamond Pattern
This example prints a diamondshaped pattern using alphabet letters. The top half is an increasing sequence of letters, and the bottom half is a decreasing sequence.
Example:
# Alphabet diamond pattern
rows = 5
ascii_value = 65 # ASCII value of 'A'
# Top part of the diamond
for i in range(rows):
print(" " * (rows  i  1) + (chr(ascii_value + i) + " ") * (i + 1))
# Bottom part of the diamond
for i in range(rows  1):
print(" " * (i + 1) + (chr(ascii_value + rows  i  2) + " ") * (rows  i  1))
Output:
A
B B
C C C
D D D D
E E E E E
D D D D
C C C
B B
A
5. Alphabet Square Pattern
This example prints a square pattern where each row contains the same letter, and each subsequent row advances to the next letter of the alphabet.
Example:
# Alphabet square pattern
rows = 5
ascii_value = 65 # ASCII value of 'A'
for i in range(rows):
print((chr(ascii_value + i) + " ") * rows)
Output:
A A A A A
B B B B B
C C C C C
D D D D D
E E E E E
How to Display Letters of the Word “Justaskans” in a Pattern in Python
Displaying the letters of a word in a pattern is a creative way to practice using loops in Python. Below is an example of how to display the letters of the word “Justaskans” in a pattern.
Example: Displaying Letters of “Justaskans” in a Pattern
This code will print the letters of the word “Justaskans” in a triangular pattern, where each row contains an increasing number of letters from the word.
Example:
# Displaying letters of "Justaskans" in a pattern
word = "Justaskans"
length = len(word)
for i in range(1, length + 1):
print(word[:i])
Output:
J
Ju
Jus
Just
Justa
Justas
Justask
Justaska
Justaskan
Justaskans
Explanation
In this example:
 The word “Justaskans” is stored in the variable
word
.  The loop iterates from 1 to the length of the word.
 In each iteration, the slice
word[:i]
is used to print the firsti
characters of the word, resulting in a pattern where each row contains an increasing number of letters.
Customizing the Pattern
You can easily customize this pattern by modifying the slicing or changing the loop to create different patterns. Below is an example of an inverted pattern.
Example: Inverted Pattern
# Inverted pattern of "Justaskans"
word = "Justaskans"
length = len(word)
for i in range(length, 0, 1):
print(word[:i])
Output:
Justaskans
Justaskan
Justaska
Justask
Justas
Justa
Just
Jus
Ju
J
How to Print a Pattern with the Same Alphabetic Character in Python
Printing a pattern using the same alphabetic character is a straightforward exercise in Python. Below is an example of how to create a pattern where each row consists of the character ‘N’.
Example: Printing a Square Pattern with ‘N’
This code will print a square pattern where each row and column contains the character ‘N’.
Example:
# Square pattern with 'N'
size = 5
for i in range(size):
for j in range(size):
print('N', end=" ")
print("")
Output:
N N N N N
N N N N N
N N N N N
N N N N N
N N N N N
Explanation
In this example:
 The outer loop controls the number of rows in the pattern.
 The inner loop controls the number of columns in each row.
 For each iteration, the character ‘N’ is printed, followed by a space, to create a square pattern.
Example: Printing a Triangle Pattern with ‘N’
This code will print a rightangled triangle pattern where each row contains an increasing number of ‘N’ characters.
Example:
# Triangle pattern with 'N'
rows = 5
for i in range(1, rows + 1):
print('N ' * i)
Output:
N
N N
N N N
N N N N
N N N N N
Example: Printing an Inverted Triangle Pattern with ‘N’
This code will print an inverted rightangled triangle pattern where each row contains a decreasing number of ‘N’ characters.
Example:
# Inverted triangle pattern with 'N'
rows = 5
for i in range(rows, 0, 1):
print('N ' * i)
Output:
N N N N N
N N N N
N N N
N N
N