Print patterns in Python | Part - 2

05 min read

Task: Write a code to print the following pattern in Python:

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

Solution:

num = 1
n=5

for i in range(0, n):
     for j in range(0, i+1):
            print(num, end=" ")           #print the current value of num with a space
            num += 1                      # increment number by 1
        print("\r")                       # end the rows
 

 

Task: Write a code to print the following pattern in Python:

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

Solution:

num = 65                                   # unicode value.
n=5
for i in range(0, n):
    for j in range(0, i+1):
            ch = chr(num)                  # chr command converts unicode value to a character.
            print(ch, end=" ")
    num = num + 1                          # increment num value by one in each iteration
        print("\r")
 

 

Task: Write a code to print the following pattern in Python:

A 
B C 
D E F 
G H I J 
K L M N O 

Solution: 

num = 65
for i in range(0, n):
        for j in range(0, i+1):
            ch = chr(num)
            print(ch, end=" ")
            num = num + 1
        print("\r")

 

 

Task: Write a code to print the following pattern in Python:

***********
 **********
  *********
   ********
    *******
     ******
      *****
       ****
        ***
         **
          *

Solution:

n=11                                 # Number of rows
for
e in range (n,0,-1): # from e=n to e=0 decrementing by one in each iteration print((n-e) * ' ' + e * '*') # printing the pattern


 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error