Pattern Program In Python
Tags: python, beginners, programming, tutorial
Published: 2022-08-09 09:36:20

`Python` has become the most rapidly growing programming language. Its use case is vast from handing server side to building machine learning models.

Loops➿ in Python have the most important role because it repeatedly does a certain task that we need.

As a beginner, you must practice loops a lot🧘.
Let's create some star, number, and alphabet pattern programs in Python to learn loops and conditional in a better way.

Originally posted at **TutorialsTonight**: [Pattern program in Python](https://www.tutorialstonight.com/python/pattern-program-in-python)

## 1. Square Pattern

```text
*****
*****
*****
*****
*****
```
Let's start with the simplest one. A hollow square pattern using stars.
To create this pattern execute a nested for loop in Python and print star at each iteration of the internal loop. At the end of the internal loop print a new line.
Here is the program:
```python
# size of the square is 5 here
# Create a list of rows
for i in range(0, 5):
    # Create a list of columns
    for j in range(0, 5):
        print("*", end="")
    print()
```
---

## 2. Hollow Square Pattern
```text
*****
*   *
*   *
*   *
*****
```
Let's complicate the thing a little further. Now You have to print a square pattern but from hollow inside.
Try yourself first, then check the solution below.

```python
# hollow square pattern
size = 5
for i in range(size):
    for j in range(size):
        # print * completely in first and last row
        # print * only in first and last position in other rows
        if i == 0 or i == size - 1 or j == 0 or j == size - 1:
            print('*', end='')
        else:
            print(' ', end='')
    print()
```
---

## 3. Pyramid Pattern

```text
    *
   ***
  *****
 *******
*********
```
Pyramid is quite famous in patterns. Let's create it using Python.

```python
# pyramid star pattern
n = 5
for i in range(n):
    for j in range(n - i - 1):
        print(' ', end='')
    for k in range(2 * i + 1):
        print('*', end='')
    print()
```
---

## 4. Hollow Pattern Program

```text
    *
   * *
  *   *
 *     *
*********
```
Complexing the thing a little further. Now you have to create a pattern program but from hollow inside.
Here is the Python program for this.

```python
# hollow pyramid star pattern
n = 5
for i in range(n):
    # printing spaces
    for j in range(n - i - 1):
        print(' ', end='')

    # printing stars
    for k in range(2 * i + 1):
        # print star at start and end of the row
        if k == 0 or k == 2 * i:
            print('*', end='')
        else:
            if i == n - 1:
                print('*', end='')
            else:
                print(' ', end='')
    print()
```
---

## 5. Hourglass Pattern

```text
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********
```
Here is a Python program for the hourglass pattern.

```python
# hourglass star pattern
n = 5

# downward pyramid
for i in range(n-1):
    for j in range(i):
        print(' ', end='')
    for k in range(2*(n-i)-1):
        print('*', end='')
    print()
# upward pyramid
for i in range(n):
    for j in range(n-i-1):
        print(' ', end='')
    for k in range(2*i+1):
        print('*', end='')
    print()
```
---

## 6. Heart Pattern

```text
 ***   ***
***** *****
***********
 *********
  *******
   *****
    ***
     *
```
Here is the heart pattern program in Python.
```python
# heart pattern 
n = 6

# upper part of the heart
for i in range(n//2, n, 2):
    # print first spaces
    for j in range(1, n-i ,2):
        print(" ", end="")
    # print first stars
    for j in range(1, i+1, 1):
        print("*", end="")
    # print second spaces
    for j in range(1, n-i+1, 1):
        print(" ", end="")
    # print second stars
    for j in range(1, i+1, 1):
        print("*", end="")
    print()

# lower part
for i in range(n,0,-1):
    for j in range(i, n, 1):
        print(" ", end="")
    for j in range(1, i*2, 1):
        print("*", end="")
    print()
```
---

## 7. Plus Pattern
```text
  *  
  *  
*****
  *  
  *
```
Here is the program for + pattern in Python.
```python
# plus pattern in python

size = 5

for i in range(size):
    for j in range(size):
        if i == size // 2:
            print('*', end='')
        else:
            if j == size // 2:
                print('*', end='')
            else:
                print(' ', end='')
    print()
```
---

## 8. Cross pattern
```text
*   *
 * * 
  *  
 * * 
*   *```
Here is the cross pattern program in Python.
```python
# cross pattern in python
size = 5

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

## 9. Number Pyramid Pattern
```text
    1
   123
  12345
 1234567
123456789
```
Here is the Python program for the number pyramid pattern.
```python
# number pyramid pattern
size = 5
for i in range(size):
    # print spaces
    for j in range(size - i - 1):
        print(" ", end="")
    # print stars
    for k in range(2 * i + 1):
        print(k+1, end="")
    print()
```
---

## 10. Alphabet Pyramid Pattern
```text
    A
   ABC
  ABCDE
 ABCDEFG
ABCDEFGHI
```
Here is the Python program for the alphabet pyramid pattern.
```python
# alphabet pyramid pattern
size = 5
alpha = 65

for i in range(size):
    # print spaces
    for j in range(size - i):
        print(" ", end="")
    # print alphabets
    for k in range(2 * i + 1):
        print(chr(alpha + k), end="")
    print()
```
---