Python Program-Set 1(P1-P10)

1.Write a program to generate the following pattern in Python.

*

* *

* * *

* * * *

The number of rows would be entered by the user.

Solution: 

>>>
n = input('Enter the number of rows')
m = int(n)
*k=1
for i in range(m):
   for j in range(1, i+2):
     print('*', end=" ")
   print()
2.Write a program to generate the following pattern in Python.

2

2 3

2 3 4

2 3 4 5

The number of rows would be entered by the user.

Solution:

n = input('Enter the number of rows')
m = int(n)
k=1
for i in range(m):
   for j in range(1, i+2):
     print(j+1, end=" ")
   print()
3.Write a program to traverse a string.

Solution: 

str1= input('Enter a string\t:')

for i in str1:

print('Character \t:',i)

4.Write a program to find the length of the string entered by the user.

name=input('Enter your name\t');

length=0 for i in name:

length=length +1

print('The length of ',name,' is ',length)

5.Consider you have to enter a string and displace two characters to the right.How will you do it?

str1=input('Enter the string\t:')

i=0

str2=""

while i<len(str1):

str2[i]=str1[(i+2)%len(str1)]

print(str2)

6.Write a program to generate the following pattern in Python.

1

2 3

4 5 6

7 8 9 10

Solution: 

n = input('Enter the number of rows')
m = int(n)
k=1
for i in range(m):
   for j in range(1, i+2):
     print(k, end=" ")
     k=k+1
   print()
7.Write a program to implement a linear search.

Solution:

def search(L, item):
    flag=0
    for i in L:
        if i==item:
            flag=1
            print('Position ',i)
    if flag==0:
        print('Not found')
L =[1, 2, 5, 9, 10]
search(L, 5)
search(L, 2)
Output

Position 5
Not found
>>>

In binary search, The algorithm checks whether the item to be searched is present at the first position, at the last position or at the middle position. If the requisite element is not present at any of these positions and it is less than the middle element, then the left part of the list becomes the input of the procedure; else the right part of the element becomes the input to the procedure.

The complexity of the binary search is O(logn).

8.Write a program to generate the following pattern in Python.

*

***

*****

*******

*********

The number of rows would be entered by the user.

Solution:

n = input('Enter the number of rows')
m = int(n)
for i in range(m):
   for j in range(0, (m-i-1)):
     print(' ', end="")
   for k in range(0, 2*i+1):
     print('*',end="")
print()
9.Generate a n × m, matrix, wherein each element (aij), is given by ai, j = 5 × (i + j)2

Solution: The concept has been explained in the above discussion. There will be two loops; the outer loop for the number of rows and the inner loop for the number of columns.

n = int(input('Enter the number of rows')) m = int(input('Enter the number of columns')) for i in range (n): for j in range(m): element = 5*(i+j)*(i+j) print(element, sep=' ', end= ' ') print() >>>

Output

Enter the number of rows3

Enter the number of columns3

0 5 20

5 20 45

20 45 80

Handling list of lists

10.In the following program the first list’s second element is itself a list. Its first element can be accessed by writing hb[0][1] and the first letter of the first element of the nested list would be hb[0][1][0].

Program

>>>
hb=["Programming in C#",["amrita University campus", 2015]]
rm=["SE is everything",["Obscure shops", 2015]]
authors=[hb, rm]
print(authors)
print("List:\n"+str(authors[0])+"\n"+str(authors[1])+"\n")
print("Name of books\n"+str(authors[0][0])+"\n"+
                                  str(authors[1][0])+"\n")
print("Details of the books\n"+str(authors[0][1])+"\n"+
                                  str(authors[1][1])+"\n")
print("\nLevel 3 Publisher 1\t:"+str(authors[0][1][0]))

Output


[['Programming in C#', ['amrita University campus', 2015]],
        ['SE is everything', ['Obscure shops', 2015]]]
List:
['Programming in C#', ['amrita University campus', 2015]]
['SE is everything', ['Obscure shops', 2015]]
Name of books
Programming in C#
SE is everything
Details of the books
['amrita University campus', 2015]
['Obscure shops', 2015]
Level 3 Publisher 1      :amrita University campus

The user to enter the value of n and find the nth Fibonacci term.

Solution:

def fib(n):
if n==1:
return 1
elif n==2:
return 1
else
return (fib(n-1) + fib(n-2))
n=input(‘Enter the number\t:’)
f=fib(n)
print(‘The nth fib term is ‘,str(f))
Output

Enter the number:5
The nth fib term is 5