Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python Programs to Create Pyramid and Patterns

1 min read

In this article, we will go over different ways to generate pyramids and patters in Python.

Half pyramid of asterisks

def half_pyramid(rows):
    for i in range(rows):
        print('*' * (i+1))

half_pyramid(6)

Output

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

An alternate way to generate half pyramid using nested loops in Python.

Program

def half_pyramid(rows):
    for i in range(rows):
        for j in range(i+1):
            print("*", end="")
        print("")
half_pyramid(6)

Output

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

Half pyramid of X's

def half_pyramid(rows):
    for i in range(rows):
        print('X' * (i+1))
half_pyramid(6)

Output

X
XX
XXX
XXXX
XXXXX
XXXXXX

Half pyramid of numbers

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

half_pyramid(5)

Output

1
12
123
1234
12345

Generating a full pyramid of asterisks

def full_pyramid(rows):
    for i in range(rows):
        print(' '*(rows-i-1) + '*'*(2*i+1))

full_pyramid(6)

Output

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

Full pyramid of X's

def full_pyramid(rows):
    for i in range(rows):
        print(' '*(rows-i-1) + 'X'*(2*i+1))

full_pyramid(6)

Output

     X
    XXX
   XXXXX
  XXXXXXX
 XXXXXXXXX
XXXXXXXXXXX

Reversed pyramid

def inverted_pyramid(rows):
    for i in reversed(range(rows)):
        print(' '*(rows-i-1) + '*'*(2*i+1))

inverted_pyramid(6)

Output

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


PROGRAMS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read