Monday, October 15, 2018

Python program to print number pattern Exercises and answers


Python Exercises to Print Patterns

  • Write a program that uses a for loop to print  below pattern.
    **********
    **********
    **********
    **********
    **********

    for i in range(5):
        print('*'*10)
  • Write a program that uses a for loop to print the numbers 1 2 3 4 5 6 7 8 9 10.

    for i in range(1,10+1,1):
        print(i,end=' ')
  • Write a program that uses a for loop to print the numbers 10,9,8,7,6,5,4,3,2,1.

    for i in range(10,1-1,-1):
        print(i,end=',')
  • Write a program that uses a for loop to print the numbers 8, 11, 14, 17, 20, ..., 83, 86, 89.

    for i in range(8,89+3,3):
        print(i,end=',')
  • Write a program that uses a for loop to print  below pattern.
    *
    **
    ***
    ****
    *****

    for i in range(4):
        print('*'*(i+1))
  • Write a program that uses a for loop to print  below pattern.
    *****
    ****
    ***
    **
    *

    for i in range(6,1-1,-1):
        print('*'*(i-1))
  • Write a program that asks the user for their name and how many times to print it. The program should print out the user’s name the specified number of times.

    Name=input('Enter Your Name?')
    times=eval(input('Enter How many times to repeat?'))
    for i in range(times):
        print(Name)

  • Write a program that uses a for loop to print the numbers 8, 11, 14, 17, 20, ..., 83, 86, 89.

    for i in range(8,89+3,3):
        print(i,end=',')
  • Python program to print number pattern as below
    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

    mylist=[] # list
    for i in range(1,11):
        for j in range(1,i+1):
            mylist.append(j) # add element to list
        print(mylist)
        mylist.clear()
  • Python program to print number pattern as below
                   * 
                * * * 
              * * * * * 
            * * * * * * * 
          * * * * * * * * * 
        * * * * * * * * * * * 
      * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * 
  * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * 

k = 0
rows = 10
for i in range(1, rows+1):
    for space in range(1, (rows-i)+1):
        print(end="  ")
    while k != (2*i-1):
        print("* ", end="")
        k = k + 1
    k = 0
    print()


Click below link to Learn Python Video Lession:



1 comment: