DEV Community

Guru prasanna
Guru prasanna

Posted on

1

Python Day- 17 Pattern formation using nested For loop

Pattern formations:
Exercises:

1)

for row in range(5):
    for col in range(5-row):
        print(5-col,end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

5 4 3 2 1 
5 4 3 2 
5 4 3 
5 4 
5 
Enter fullscreen mode Exit fullscreen mode

2)

no = 1
for row in range(5): 
    for col in range(5-row):
        print(no, end=' ')
        no+=1
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4 5 
6 7 8 9 
10 11 12 
13 14 
15 
Enter fullscreen mode Exit fullscreen mode

3)

for row in range(5): 
    for col in range(5-(row+1)):
        print(col+1, end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4 
1 2 3 
1 2 
1 

Enter fullscreen mode Exit fullscreen mode

4)

for row in range(5): 
    for col in range(4-row):
        print(col+1, end=' ')
    print("* ", end= ' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4 *  
1 2 3 *  
1 2 *  
1 *  
*  
Enter fullscreen mode Exit fullscreen mode

5)

for row in range(5): 
    for col in range(4-row):
        print(col+1, end=' ')
    for col in range(5):
        print("* ",end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4 *  *  *  *  *  
1 2 3 *  *  *  *  *  
1 2 *  *  *  *  *  
1 *  *  *  *  *  
*  *  *  *  *  
Enter fullscreen mode Exit fullscreen mode

6)

for row in range(5): 
    for col in range(4-row):
        print(col+1, end=' ')
    for col in range(5):
        print(col,end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4 0 1 2 3 4 
1 2 3 0 1 2 3 4 
1 2 0 1 2 3 4 
1 0 1 2 3 4 
0 1 2 3 4 
Enter fullscreen mode Exit fullscreen mode

7)

for row in range(5): 
    for col in range(4-row):
        print(" ", end=' ')
    for col in range(row+1):
        print(col+1,end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

        1 
      1 2 
    1 2 3 
  1 2 3 4 
1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

8)

for row in range(5): 
    for col in range(4-row):
        print("", end=' ')
    for col in range(row+1):
        print(col+1,end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

9)

for row in range(5): 
    for col in range(4-row):
        print(" ", end=' ')
    for col in range(5):
        print(col+1,end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

        1 2 3 4 5 
      1 2 3 4 5 
    1 2 3 4 5 
  1 2 3 4 5 
1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

10)

for row in range(5): 
    for col in range(row+1):
        print(col+1,end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

11)

for row in range(5): 
    for star in range(4-row):
        print("*",end=' ')
    for col in range(row+1):
        print(col+1,end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

* * * * 1 
* * * 1 2 
* * 1 2 3 
* 1 2 3 4 
1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

12)

for row in range(5): 
    for star in range(4-row):
        print(" ",end=' ')
    for col in range(row+1):
        print(5-col,end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

        5 
      5 4 
    5 4 3 
  5 4 3 2 
5 4 3 2 1 
Enter fullscreen mode Exit fullscreen mode

13)

for row in range(5): 
    for space in range(4-row):
        print(' ',end=' ')
    for col in range(row+1):
        print(5+col-row,end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

        5 
      4 5 
    3 4 5 
  2 3 4 5 
1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

๐Ÿ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay