# Programs for printing pyramid patterns in Python

Patterns can be printed in python using simple for loops. First outer loop is used to handle number of rows and Inner nested loop is used to handle the number of columns. Manipulating the print statements, different number patterns, alphabet patterns or star patterns can be printed.

• Simple pyramid pattern

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern ` `def` `pypart(n): ` `     `  `    ``# outer loop to handle number of rows ` `    ``# n in this case ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# printing stars ` `            ``print``(``"* "``,end``=``"") ` `      `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver Code ` `n ``=` `5` `pypart(n) `

Output:

```*
* *
* * *
* * * *
* * * * *
```

Another Approach:
Using List in Python 3, this could be done in a simpler way

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern ` `def` `pypart(n): ` `    ``myList ``=` `[] ` `    ``for` `i ``in` `range``(``1``,n``+``1``): ` `        ``myList.append(``"*"``*``i) ` `    ``print``(``"\n"``.join(myList)) ` ` `  `# Driver Code ` `n ``=` `5` `pypart(n) `

• After 180 degree rotation

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern ` `def` `pypart2(n): ` `     `  `    ``# number of spaces ` `    ``k ``=` `2``*``n ``-` `2` ` `  `    ``# outer loop to handle number of rows ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# inner loop to handle number spaces ` `        ``# values changing acc. to requirement ` `        ``for` `j ``in` `range``(``0``, k): ` `            ``print``(end``=``" "``) ` `     `  `        ``# decrementing k after each loop ` `        ``k ``=` `k ``-` `2` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# printing stars ` `            ``print``(``"* "``, end``=``"") ` `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver Code ` `n ``=` `5` `pypart2(n) `

Output:

```        *
* *
* * *
* * * *
* * * * *
```

• Printing Triangle

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern triangle ` `def` `triangle(n): ` `     `  `    ``# number of spaces ` `    ``k ``=` `2``*``n ``-` `2` ` `  `    ``# outer loop to handle number of rows ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# inner loop to handle number spaces ` `        ``# values changing acc. to requirement ` `        ``for` `j ``in` `range``(``0``, k): ` `            ``print``(end``=``" "``) ` `     `  `        ``# decrementing k after each loop ` `        ``k ``=` `k ``-` `1` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# printing stars ` `            ``print``(``"* "``, end``=``"") ` `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver Code ` `n ``=` `5` `triangle(n) `

Output:

```    *
* *
* * *
* * * *
* * * * *
```

• Number Pattern

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern of numbers ` `def` `numpat(n): ` `     `  `    ``# initialising starting number  ` `    ``num ``=` `1` ` `  `    ``# outer loop to handle number of rows ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# re assigning num ` `        ``num ``=` `1` `     `  `        ``# inner loop to handle number of columns ` `            ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `                ``# printing number ` `            ``print``(num, end``=``" "``) ` `         `  `            ``# incrementing number at each column ` `            ``num ``=` `num ``+` `1` `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver code ` `n ``=` `5` `numpat(n) `

Output:

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

• Numbers without re assigning

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern of numbers ` `def` `contnum(n): ` `     `  `    ``# initializing starting number  ` `    ``num ``=` `1` ` `  `    ``# outer loop to handle number of rows ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# not re assigning num ` `        ``# num = 1 ` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# printing number ` `            ``print``(num, end``=``" "``) ` `         `  `            ``# incrementing number at each column ` `            ``num ``=` `num ``+` `1` `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `n ``=` `5` ` `  `# sending 5 as argument ` `# calling Function ` `contnum(n) `

Output:

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

• Character Pattern

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern of alphabets ` `def` `alphapat(n): ` `     `  `    ``# initializing value corresponding to 'A'  ` `    ``# ASCII value ` `    ``num ``=` `65` ` `  `    ``# outer loop to handle number of rows ` `    ``# 5 in this case ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# explicitely converting to char ` `            ``ch ``=` `chr``(num) ` `         `  `            ``# printing char value  ` `            ``print``(ch, end``=``" "``) ` `     `  `        ``# incrementing number ` `        ``num ``=` `num ``+` `1` `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver Code ` `n ``=` `5` `alphapat(n) `

Output:

```A
B B
C C C
D D D D
E E E E E
```

• Continuous Character pattern

 `# Python code 3.x to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern of alphabets ` `def`  `contalpha(n): ` `     `  `    ``# initializing value corresponding to 'A'  ` `    ``# ASCII value ` `    ``num ``=` `65` ` `  `    ``# outer loop to handle number of rows ` `-`   `for` `i ``in` `range``(``0``, n): ` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# explicitely converting to char ` `            ``ch ``=` `chr``(num) ` `         `  `            ``# printing char value  ` `            ``print``(ch, end``=``" "``) ` `         `  `            ``# incrementing at each column ` `            ``num ``=` `num ``+``1` `     `  `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver code ` `n ``=` `5` `contalpha(n) `

Output:

```A
B C
D E F
G H I J
K L M N O
```
• This article is contributed by Manjeet Singh(S.Nupur) . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Patterns can be printed in python using simple for loops. First outer loop is used to handle number of rows and Inner nested loop is used to handle the number of columns. Manipulating the print statements, different number patterns, alphabet patterns or star patterns can be printed.

• Simple pyramid pattern

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern ` `def` `pypart(n): ` `     `  `    ``# outer loop to handle number of rows ` `    ``# n in this case ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# printing stars ` `            ``print``(``"* "``,end``=``"") ` `      `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver Code ` `n ``=` `5` `pypart(n) `

Output:

```*
* *
* * *
* * * *
* * * * *
```

• After 180 degree rotation

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern ` `def` `pypart2(n): ` `     `  `    ``# number of spaces ` `    ``k ``=` `2``*``n ``-` `2` ` `  `    ``# outer loop to handle number of rows ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# inner loop to handle number spaces ` `        ``# values changing acc. to requirement ` `        ``for` `j ``in` `range``(``0``, k): ` `            ``print``(end``=``" "``) ` `     `  `        ``# decrementing k after each loop ` `        ``k ``=` `k ``-` `2` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# printing stars ` `            ``print``(``"* "``, end``=``"") ` `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver Code ` `n ``=` `5` `pypart2(n) `

Output:

```        *
* *
* * *
* * * *
* * * * *
```

• Printing Triangle

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern triangle ` `def` `triangle(n): ` `     `  `    ``# number of spaces ` `    ``k ``=` `2``*``n ``-` `2` ` `  `    ``# outer loop to handle number of rows ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# inner loop to handle number spaces ` `        ``# values changing acc. to requirement ` `        ``for` `j ``in` `range``(``0``, k): ` `            ``print``(end``=``" "``) ` `     `  `        ``# decrementing k after each loop ` `        ``k ``=` `k ``-` `1` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# printing stars ` `            ``print``(``"* "``, end``=``"") ` `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver Code ` `n ``=` `5` `triangle(n) `

Output:

```    *
* *
* * *
* * * *
* * * * *
```

• Number Pattern

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern of numbers ` `def` `numpat(n): ` `     `  `    ``# initialising starting number  ` `    ``num ``=` `1` ` `  `    ``# outer loop to handle number of rows ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# re assigning num ` `        ``num ``=` `1` `     `  `        ``# inner loop to handle number of columns ` `            ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `                ``# printing number ` `            ``print``(num, end``=``" "``) ` `         `  `            ``# incrementing number at each column ` `            ``num ``=` `num ``+` `1` `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver code ` `n ``=` `5` `numpat(n) `

Output:

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

• Numbers without re assigning

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern of numbers ` `def` `contnum(n): ` `     `  `    ``# initializing starting number  ` `    ``num ``=` `1` ` `  `    ``# outer loop to handle number of rows ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# not re assigning num ` `        ``# num = 1 ` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# printing number ` `            ``print``(num, end``=``" "``) ` `         `  `            ``# incrementing number at each column ` `            ``num ``=` `num ``+` `1` `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `n ``=` `5` ` `  `# sending 5 as argument ` `# calling Function ` `contnum(n) `

Output:

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

• Character Pattern

 `# Python 3.x code to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern of alphabets ` `def` `alphapat(n): ` `     `  `    ``# initializing value corresponding to 'A'  ` `    ``# ASCII value ` `    ``num ``=` `65` ` `  `    ``# outer loop to handle number of rows ` `    ``# 5 in this case ` `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# explicitely converting to char ` `            ``ch ``=` `chr``(num) ` `         `  `            ``# printing char value  ` `            ``print``(ch, end``=``" "``) ` `     `  `        ``# incrementing number ` `        ``num ``=` `num ``+` `1` `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver Code ` `n ``=` `5` `alphapat(n) `

Output:

```A
B B
C C C
D D D D
E E E E E
```

• Continuous Character pattern

 `# Python code 3.x to demonstrate star pattern ` ` `  `# Function to demonstrate printing pattern of alphabets ` `def`  `contalpha(n): ` `     `  `    ``# initializing value corresponding to 'A'  ` `    ``# ASCII value ` `    ``num ``=` `65` ` `  `    ``# outer loop to handle number of rows ` `-`   `for` `i ``in` `range``(``0``, n): ` `     `  `        ``# inner loop to handle number of columns ` `        ``# values changing acc. to outer loop ` `        ``for` `j ``in` `range``(``0``, i``+``1``): ` `         `  `            ``# explicitely converting to char ` `            ``ch ``=` `chr``(num) ` `         `  `            ``# printing char value  ` `            ``print``(ch, end``=``" "``) ` `         `  `            ``# incrementing at each column ` `            ``num ``=` `num ``+``1` `     `  `     `  `        ``# ending line after each row ` `        ``print``(``"\r"``) ` ` `  `# Driver code ` `n ``=` `5` `contalpha(n) `

Output:

```A
B C
D E F
G H I J
K L M N O
```
• This article is contributed by Manjeet Singh(S.Nupur) . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5755758,"math_prob":0.8836642,"size":11030,"snap":"2020-45-2020-50","text_gpt3_token_len":3372,"char_repetition_ratio":0.17377108,"word_repetition_ratio":0.9145516,"special_character_ratio":0.3145059,"punctuation_ratio":0.08858268,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99858093,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-22T10:50:54Z\",\"WARC-Record-ID\":\"<urn:uuid:597a316f-222d-436e-a107-dd47da7e387e>\",\"Content-Length\":\"208275\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2a76a033-0fb2-44fb-9bdc-5fb1c509c80f>\",\"WARC-Concurrent-To\":\"<urn:uuid:4cedb827-19eb-4976-a2c3-ce79a4a0c20d>\",\"WARC-IP-Address\":\"23.217.129.104\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/programs-printing-pyramid-patterns-python/?ref=lbp\",\"WARC-Payload-Digest\":\"sha1:SMHCEXPT5QBZW2RLKRDMLT5ZV6CM6BZB\",\"WARC-Block-Digest\":\"sha1:LBYAS2YK4QDT5PXP2PXK4X4KRLLR65KG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107879362.3_warc_CC-MAIN-20201022082653-20201022112653-00444.warc.gz\"}"}