Computer Science, asked by madhulnath30, 8 days ago

How to print a pattern like this in python​

Attachments:

Answers

Answered by anindyaadhikari13
4

\textsf{\large{\underline{Solution}:}}

The given problem is solved using language - Python.

1. Using nested loops.

n=5

for i in range(n):

   for j in range(i):

       print('',end='  ')

   for j in range(n-i):

       print('*',end=' ')

   print()

2. Using 1 loop.

n=5

for i in range(n):

   print(i*'  ',end='')

   print((n-i)*'* ')

3. One liner.

print('\n'.join('  '*i+'* '*(5-i) for i in range(5)))

Note: Here I have taken n = 5 as there are 5 rows in this pattern. You can also take the value of n as input from the user.

\textsf{\large{\underline{O{u}tput}:}}

* * * * *  

 * * * *  

   * * *  

     * *  

       *

See attachment for verification.

Attachments:
Similar questions