/**/ Sanjeev Study Hub – Class 11 & 12 Notes, Question Papers, IP & CS Study Material: pattern programs in python
Showing posts with label pattern programs in python. Show all posts
Showing posts with label pattern programs in python. Show all posts

Tuesday, May 2, 2023

PYTHON PATTERN CODING

 PATTERN CODING (PYTHON)


# 1. Pyramid pattern of numbers

rows = 10

for i in range(1, rows + 1):

    for j in range(1, i + 1):

        print(j, end='  ')

    print(' ')

OUTPUT

1   

1  2   

1  2  3   

1  2  3  4   

1  2  3  4  5   

1  2  3  4  5  6   

1  2  3  4  5  6  7   

1  2  3  4  5  6  7  8   

1  2  3  4  5  6  7  8  9   

1  2  3  4  5  6  7  8  9  10   

# 2. Reverse pattern for loop from 7 to 1

rows = 7

n = 0

for i in range(rows, 0, -1):

    n += 1

    for j in range(1, i + 1):

        print(n, end=' ')

    print('\r')

OUTPUT

1 1 1 1 1 1 1 

2 2 2 2 2 2 

3 3 3 3 3 

4 4 4 4 

5 5 5 

6 6 

7 


# 3. Inverted Pyramid pattern with the same digit

rows = 7 #You can use any number like 8, 9, 4 etc.

num = rows

# reverse for loop

for i in range(rows, 0, -1):

    for j in range(0, i):

        print(num, end=' ')

    print("\r")

OUTPUT

7 7 7 7 7 7 7 

7 7 7 7 7 7 

7 7 7 7 7 

7 7 7 7 

7 7 7 

7 7 

7 

# 4. Another reverse number pattern (8 to 1)

rows = 8

for i in range(0, rows + 1):

    for j in range(rows - i, 0, -1):

        print(j, end=' ')

    print()

OUTPUT

8 7 6 5 4 3 2 1 

7 6 5 4 3 2 1 

6 5 4 3 2 1 

5 4 3 2 1 

4 3 2 1 

3 2 1 

2 1 

1 

# 5. Print other reverse number from 10 to 1

start = 1

stop = 2

current_num = stop

for row in range(2, 6):

    for col in range(start, stop):

        current_num -= 1

        print(current_num, end=' ')

    print("")

    start = stop

    stop += row

    current_num = stop

OUTPUT

1 

3 2 

6 5 4 

10 9 8 7 

# 6. Number triangle pattern

rows = 10

for i in range(1, rows):

    num = 1

    for j in range(rows, 0, -1):

        if j > i:

            print(" ", end=' ')

        else:

            print(num, end='  ')

            num += 1

    print(" ")

OUTPUT

                 1   

                1  2   

              1  2  3   

            1  2  3  4   

          1  2  3  4  5   

        1  2  3  4  5  6   

      1  2  3  4  5  6  7   

    1  2  3  4  5  6  7  8   

  1  2  3  4  5  6  7  8  9  

# 7. Pyramid pattern of stars in python


rows = 8

for i in range(0, rows):

    for j in range(0, i + 1):

       print("*", end=' ')

    print("\r")

OUTPUT

* 

* * 

* * * 

* * * * 

* * * * * 

* * * * * * 

* * * * * * * 

* * * * * * * * 








Class 12 Informatics Practices 2026 Question Paper with Answers (Full Solution)

  CBSE Class 12 IP 2026 बोर्ड पेपर + Solutions 1. State whether the following statement is True or False: In Pandas Series, the Positional...