Python Tutorial: Pattern

Thursday, 5 May 2022

Pattern

1.  Write Pattern Program *

*

* *

* * *

* * * *

* * * * *

* * * * * *

Source Code

n=int(input("Enter number of rows/lines"))

for line in range(n):

    for star in range(line):

        print("* ",end='')

    print()

OR

n=int(input("Enter number of rows/lines"))

for line in range(n):

        print("*"*(line+1))

 

2. Print half Pyramid using numbers

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Source Code

n=int(input("Enter number of rows/lines"))

for line in range(n+1):

    for n in range(1,line+1):

        print(n,end=' ')

    print()

 

3. Print half Pyramid using numbers

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

Source Code

n=int(input("Enter number of rows/lines"))

for line in range(0,n+1):

    for n in range(0,line):

        print(line,end=' ')

    print()

 

4.  Print half Pyramid using ASCII Code/Alphabets

A

B B

C C C

D D D D

E E E E E

F F F F F F

G G G G G G G

Source Code

n=int(input("Enter number of rows/lines"))

ascii_value=65

for line in range(0,n+1):

    for n in range(0,line+1):

        alphabet=chr(ascii_value)

        print(alphabet,end=' ')

    ascii_value+=1

    print()

 

5. Pattern Reverse:

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

Source Code

n=int(input("Enter number of rows/lines"))

for line in range(n,0,-1):

    for n in range(0,line):

        print(line,end=' ')

    print()

 

6. Make Pattern Code

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

Source Code

n=int(input("Enter number of rows/lines"))

for line in range(n,0,-1):

    for n in range(1,line+1):

        print(n,end=' ')

    print()

 

7. Pattern Pyramid using numbers

     *

    **

   ***

  ****

 *****

Source Code

rows = int(input("Enter number of rows: "))

for line in range(rows):

    for space in range(rows,line,-1):

        print(end=' ' )

    for star in range(0,line+1):

        print("*",end='')

    print()

 

8. Pattern Pyramid using numbers

      1

     12

    123

   1234

  12345

 123456

Source Code

rows = int(input("Enter number of rows: "))

for line in range(rows):

    for space in range(rows,line,-1):

        print(' ',end='' )

    for star in range(1,line+1):

        print(star,end='')

    print()

 

9. Write Pattern code of given output:             

          1

        12

      123

    1234

  12345

Source Code

rows = int(input("Enter number of rows: "))

for line in range(rows):

    for space in range(rows,line,-1):

        print(' ',end=' ' )

    for star in range(1,line+1):

        print(star,end='')

    print()

 

10.  Write Python Pattern  code  

          *

        **

      ***

    ****

  *****

Source Code

rows = int(input("Enter number of rows: "))

for line in range(rows):

    for space in range(rows,line,-1):

        print(' ',end=' ' )

    for star in range(0,line+1):

        print("*",end='')

    print()

11.  Write Python Pattern  code  

Py 

Pyt 

Pyth 

Pytho 

Python 


Source Code:

j=0

st="Python"

for i in range(len(st)):

   print(st[0:i+1],"")

12.  Write Python Pattern  code  

               

Python 

Python 

Pytho 

Pyth 

Pyt 

Py 

Source Code:

j=0

st="Python"

for i in range(len(st),-1,-1):

   print(st[0:i+1],"")

13.  Write Python Pattern  code  


Python

 Pytho

  Pyth

   Pyt

    Py

     P



Source Code:

st="Program"

k=0

for i in range(len(st),-1,-1):

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

      print(" ",end="")      

   print(st[0:i])

   k=k+1

14.  Write Python Pattern  code    

COMPUTER

O               E

M               T

P               U

U               P

T               M

E               O

RETUPMOC

Source Code:

st="COMPUTER"

k=0

L=len(st)

print(st)

for i in range(L-2):

   print(st[i+1]+"      "+st[L-2-i])#keep space for 6 letters to print the pattern

print(st[::-1])

15.  Write Python Pattern  code  


PYTHON

YTHONP

THONPY

HONPYT

ONPYTH

NPYTHO


Source Code:

st="PYTHON"

k=0

p=len(st)

for a in range(p):

   print(st[a:p],end="")

   print(st[0:a])

No comments:

Post a Comment