Patterns using nested For Loop in Python

Creating a simple string using for loop
for i in range(10):
  print('*',end='')  
Output
**********

Pattern #1 : Rectangle

for i in range(5): #change the range stop value for rows 
  for j in range(10): # change the range stop value for columns
    print('*',end='')  
  print('')
Output
**********
**********
**********
**********
**********

Pattern #2 : Right-Angled Triangle

for i in range(1,10):
  for j in range(i):
    print('*',end='') 
    #print(j,end='') # to show numbers 	
  print('')
*
**
***
****
*****
******
*******
********
*********

Pattern #3 : Inverted Right-Angled Triangle

for i in range(10):
  for j in range(10-i):
    print('*',end='')  
    #print(j,end='') # to show numbers 
  print('')
**********
*********
********
*******
******
*****
****
***
**
*

Pattern #4 : Equilateral Triangle

for i in range(1,11):
  for k in range(i,10):
    print(' ',end='')
  for j in range(2*i-1):
    print('*',end='')
  print('')   
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************

Pattern #5 : Inverted Triangle

for i in range(11,0,-1):
  for k in range(i,11):
    print(' ',end='')
  for j in range((2*i)-1):
    print('*',end='')
  print('')
*********************
 *******************
  *****************
   ***************
    *************
     ***********
      *********
       *******
        *****
         ***
          *

Pattern #6 : Double-Sided Diamond Border

for i in range(0,12):
  for k in range(i,11):
    print('*',end='')  # left side 
  for j in range((2*i)): # 
    print(' ',end='')  # blank between 
  for k in range(i,11):
    print('*',end='')  # right side 
  print('')
**********************
**********  **********
*********    *********
********      ********
*******        *******
******          ******
*****            *****
****              ****
***                ***
**                  **
*                    *

Pattern #7 :Diamond Pattern

n = 5
for i in range(n):
    print(" " * (n-i-1) + "*" * (2*i+1))
for i in range(n-2, -1, -1):
    print(" " * (n-i-1) + "*" * (2*i+1))
    *
   ***
  *****
 *******        
*********       
 *******        
  *****
   ***
    *

Pattern #8 : Hollow Square Pattern

n = 5
for i in range(n):
    for j in range(n):
        if i == 0 or i == n-1 or j == 0 or j == n-1:
            print('*', end='')
        else:
            print(' ', end='')
    print('')
*****
*   *
*   *
*   *
*****

Pattern #9 : Right Arrow Pattern

n = 5
for i in range(n):
    print(' ' * i + '*')
for i in range(n-2, -1, -1):
    print(' ' * i + '*')
*
 *
  *
   *
    *
   *
  *
 *
*

Pattern #10 : Diamond Outline Pattern

n = 5
for i in range(n):
    print(' ' * (n-i-1) + '*' + ' ' * (2*i-1) + ('*' if i > 0 else ''))
for i in range(n-2, -1, -1):
    print(' ' * (n-i-1) + '*' + ' ' * (2*i-1) + ('*' if i > 0 else ''))
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

Pattern #11 : Hourglass Pattern

n = 5
for i in range(n, 0, -1):
    print(' ' * (n-i) + '*' * (2*i-1))
for i in range(2, n+1):
    print(' ' * (n-i) + '*' * (2*i-1))
*********
 *******
  ***** 
   ***  
    *   
   ***
  *****
 *******
*********

Pattern #12 : Floyd's Triangle

n = 5
num = 1
for i in range(1, n+1):
    for j in range(1, i+1):
        print(num, end=' ')
        num += 1
    print('')
1 
2 3 
4 5 6
7 8 9 10
11 12 13 14 15

Pattern #13 : Butterfly Pattern

n = 5
for i in range(1, n+1):
    print('*' * i + ' ' * (2*(n-i)) + '*' * i)
for i in range(n, 0, -1):
    print('*' * i + ' ' * (2*(n-i)) + '*' * i)
*        *
**      **
***    ***
****  ****
**********
**********
****  ****
***    ***
**      **
*        *

Pattern #14 Pascal's Triangle

n=5
for i in range(n):
    num = 1
    # Print leading spaces for alignment
    print(' ' * (n - i), end='')
    for j in range(i + 1):
        print(num, end=' ')
        num = num * (i - j) // (j + 1)
    print()
     1 
    1 1 
   1 2 1
  1 3 3 1
 1 4 6 4 1
These patterns can be adjusted by modifying the range or loop values.
Multiplication table using nested for loops
View and Download for_loop ipynb file ( .html format )
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    14-01-2023

    12345
    1234
    123
    12
    1

    02-10-2023

    12345
    1234
    123
    12
    1

    03-12-2023

    12345
    1234
    123
    12
    1

    06-12-2023

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

    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer