Home > Backend Development > Python Tutorial > Python Day- Pattern formation using loop,Tasks

Python Day- Pattern formation using loop,Tasks

Mary-Kate Olsen
Release: 2024-12-11 18:11:14
Original
964 people have browsed it

Python Day- Pattern formation using loop,Tasks

Pattern formations:
Exercises:

1)

for row in range(5):
    for col in range(5-row):
        print(5-col,end=' ')
    print()
Copy after login

Output:

5 4 3 2 1 
5 4 3 2 
5 4 3 
5 4 
5 
Copy after login

2)

no = 1
for row in range(5): 
    for col in range(5-row):
        print(no, end=' ')
        no+=1
    print()
Copy after login

Output:

1 2 3 4 5 
6 7 8 9 
10 11 12 
13 14 
15 
Copy after login

3)

for row in range(5): 
    for col in range(5-(row+1)):
        print(col+1, end=' ')
    print()
Copy after login

Output:

1 2 3 4 
1 2 3 
1 2 
1 

Copy after login

4)

for row in range(5): 
    for col in range(4-row):
        print(col+1, end=' ')
    print("* ", end= ' ')
    print()
Copy after login

Output:

1 2 3 4 *  
1 2 3 *  
1 2 *  
1 *  
*  
Copy after login

5)

for row in range(5): 
    for col in range(4-row):
        print(col+1, end=' ')
    for col in range(5):
        print("* ",end=' ')
    print()
Copy after login

Output:

1 2 3 4 *  *  *  *  *  
1 2 3 *  *  *  *  *  
1 2 *  *  *  *  *  
1 *  *  *  *  *  
*  *  *  *  *  
Copy after login

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()
Copy after login

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 
Copy after login

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()
Copy after login

Output:

        1 
      1 2 
    1 2 3 
  1 2 3 4 
1 2 3 4 5 
Copy after login

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()
Copy after login

Output:

    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 
Copy after login

9)

for row in range(5): 
    for col in range(4-row):
        print(" ", end=' ')
    for col in range(5):
        print(col+1,end=' ')
    print()
Copy after login

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 
Copy after login

10)

for row in range(5): 
    for col in range(row+1):
        print(col+1,end=' ')
    print()
Copy after login

Output:

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
Copy after login

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()
Copy after login

Output:

* * * * 1 
* * * 1 2 
* * 1 2 3 
* 1 2 3 4 
1 2 3 4 5 
Copy after login

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()
Copy after login

Output:

        5 
      5 4 
    5 4 3 
  5 4 3 2 
5 4 3 2 1 
Copy after login

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()
Copy after login

Output:

        5 
      4 5 
    3 4 5 
  2 3 4 5 
1 2 3 4 5 
Copy after login

The above is the detailed content of Python Day- Pattern formation using loop,Tasks. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template