You are on page 1of 15

SR.

PROGRA DAT PAGE TEACHERS


M E SIGNATURE&

NO NO.
. REMARKS

WAP to find the largest number in a


01. list.

WAP to implement Linear Search in a


02. list.

Write Python program to print


generate Fibonacci Series using
03. tuple.

Write Python program to print the


04. largest even and largest odd number
in a list.
Write a Python program Calculate the
05. Average of Numbers in a Given List.

06. Write Python Program to search the


number of times a particular number
occurs in a list.
Write Python program to sort a list of
tuples in increasing order by the last
element in each tuple.
07.
WAP to check if a given key exists in
08. a dictionary or not.

WAP to add a key-value pair to a


09. dictionary.

WAP to find the sum all the items in a


10. dictionary.

WAP to to multiply all the items in a


11. dictionary.

WAP to remove the given key from a


12. dictionary.

PYTHON PROGRAMS

1. WAP to find the largest number in a list.

Code: a=[]

n=int(input("Enter number of

elements:"))
for i in range(1,n+1):

b=int(input("Enter element:"))

a.append(b) a.sort()

print("Largest element is:",a[n-

1])

SAMPLE OUTPUT:

2.WAP to implement Linear Search in a list.

Code:
list_of_elements = [4, 2, 8, 9, 3, 7] x =

int(input("Enter number to search: "))

found = False for i in

range(len(list_of_elements)):

if(list_of_elements[i] == x):

found = True print("%d found at %dth

position"%(x,i))

break

if(found == False):

print("%d is not in list"%x)

SAMPLE OUTPUT:
3. WritePython program to print generate Fibonacci Series
using tuple.

Code: nterms =

10 n1 = 0

n2 = 1

count = 0

tup=()

# check if the number of terms is valid if

nterms <= 0:

print("Please enter a positive integer")

elif nterms == 1: print("Fibonacci sequence

upto",nterms,":") print(n1) else:

print("Fibonacci sequence upto",nterms,":")

while count < nterms:

tup=tup+(n1,)

nth = n1 + n2

# update values

n1 = n2 n2 =
nth count += 1

print (tup)

SAMPLE OUTPUT:

4. Write
Python program to print the largest even and largest odd
number in a list.
Code:

n=int(input("Enter the number of elements to be in the list:"))


b=[] for i in range(0,n):
a=int(input("Element: "))
b.append(a) c=[] d=[] for i
in b: if(i%2==0):
c.append(i)
else:
d.append(i)
c.sort()
d.sort() count1=0
count2=0 for k in c:
count1=count1+1
for j in d:
count2=count2+1
print("Largest even number:",c[count1-1])
print("Largest odd number",d[count2-1])

SAMPLE OUTPUT:

5. Write a Python program Calculate the Average of Numbers in


a Given List.

Code: n=int(input("Enter the number of elements to be

inserted: ")) a=[] for i in range(0,n):

elem=int(input("Enter element: "))


a.append(elem) avg=sum(a)/n print("Average of

elements in the list",round(avg,2))

SAMPLE OUTPUT:
6. Write Python Program to search the number of times a
particular number occurs in a list.

Code: a=[] n=int(input("Enter number of


elements:")) for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
k=0 num=int(input("Enter the number to be
counted:"))
for j in a:
if(j==num):
k=k+1
print("Number of times",num,"appears is",k)

SAMPLE OUTPUT:
7. Write Python program to sort a list of tuples in increasing
order by the last element in each tuple.

Code:

def last(n):

return n[-1] def

sort(tuples):

return sorted(tuples, key=last)

a=input("Enter a list of tuples:")

print("Sorted:") print(sort(a))

SAMPLE OUTPUT:
8. WAP to check if a given key exists in a dictionary or not.

Code:

d={'A':1,'B':2,'C':3} key=raw_input("Enter key to

check:") if key in d.keys(): print("Key is

present and value of the key is:")

print(d[key]) else:

print("Key isn't present!")

SAMPLE OUTPUT:

9. WAP to add a key-value pair to a dictionary.

Code:
key=int(input("Enter the key (int) to be added:"))

value=int(input("Enter the value for the key to be added:"))

d={}

d.update({key:value}) print("Updated

dictionary is:") print(d)

SAMPLE OUTPUT:

10. WAP to find the sum all the items in a dictionary.

Code:

d={'A':100,'B':540,'C':239} print("Total sum


of values in the dictionary:")
print(sum(d.values()))

SAMPLE OUTPUT:
11. WAP to to multiply all the items in a dictionary.

Code:

d={'A':10,'B':10,'C':239}

tot=1

for i in d:

tot=tot*d[i] print(tot)

SAMPLE OUTPUT:

12. WAP to remove the given key from a dictionary.


Code:

d = {'a':1,'b':2,'c':3,'d':4} print("Initial

dictionary") print(d) key=raw_input("Enter the

key to delete(a-d):")

if key in d:

del d[key]

else:

print("Key not found!")

exit(0) print("Updated

dictionary") print(d)

SAMPLE OUTPUT:

You might also like