0% found this document useful (0 votes)
165 views18 pages

Computer Science Practical File (Term-2) PDF

The document contains Python code for various calculator and list operations including addition, subtraction, multiplication, division, finding the largest/smallest number in a list, swapping elements in odd and even locations of a list, selection sort, bubble sort, insertion sort, binary search, and searching for an element in a list. It also includes code for creating a dictionary to store student details with roll number, name, and marks and displaying names of students who scored above 75. Finally, it has code for a dictionary to add, search and print heritage sites with their name, location, year built, and person who built.

Uploaded by

Mayank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
165 views18 pages

Computer Science Practical File (Term-2) PDF

The document contains Python code for various calculator and list operations including addition, subtraction, multiplication, division, finding the largest/smallest number in a list, swapping elements in odd and even locations of a list, selection sort, bubble sort, insertion sort, binary search, and searching for an element in a list. It also includes code for creating a dictionary to store student details with roll number, name, and marks and displaying names of students who scored above 75. Finally, it has code for a dictionary to add, search and print heritage sites with their name, location, year built, and person who built.

Uploaded by

Mayank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

#Input :-

Practi
Calculator - using Fn.

cal -
def addition ():
return(a+b)
def subtraction () :
return(a-b)
def multiplication():

17
return(a*b)
def division ():
return(a/b)
print("1= For addition")
print("2= For subtraction")
print("3= For multiplication")
print("4= For division")
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter your choice:"))
if c==1:
print(addition())
elif c==2:
print(subtraction())
elif c==3:
print(multiplication())
elif c==4:
print(division())
else :
print("wrong choice")
print("choose again")
#Ouput :-
Practi
Find the largest/smallest no. in a list

#Input :-
cal -
a=eval(input("Enter a list to find the largest and the smallest number:-"))

18
b=a[0]
c=a[0]
for i in range(len(a)):
if a[i]<b:
b=a[i]
print("Smallest Number is:-",(b))
for j in range(len(a)):
if a[j]>c:
c=a[j]
print("Largest Number is:-",(c))

#Ouput :-
Practi
Swap no. in odd even locations

cal -19
#Input :-
a=eval(input("Enter a list with even number of elements in the list:-"))
b=a[0]
for i in range(0,len(a),2):
a[i],a[i+1]=a[i+1],a[i]
print(a)

#Output :-
#Input :-
PracticSelection Sort

al -20
a=eval(input("Enter a list:-"))
for i in range(len(a)):
b=i
for j in range(i+1, len(a)):
if a[b] > a[j]:
b=j
a[i], a[b] = a[b], a[i]
print(a)
#Output :-

#Input :- PracticBubble Sort

al -21
a=eval(input("Enter a list:-"))
b=len(a)
print()
print("Before sorting",a)
for i in range(b-1):
for j in range(b-1-i):
if a[j]>a[j+1]:
a[j],a[j+1]=a[j+1],a[j]
print("After sorting",a)
#Output :-
Practi
cal -22
Insertion Sort

#Input :-
a=eval(input("Enter a list:-"))
for i in range(1,len(a)):
key=a[i]
j=i-1
while j>=0 and key<a[j]:
a[j+1]=a[j]
j=j-1
else:
a[j+1]=key
print(a)

#Output :-
Practi Binary Search

#Input :-
cal -23
z=eval(input("Enter the list:-"))
z.sort()
print("")
b=int(input("Enter the element to find:-"))
c=len(z)
d=max(z)
e=d//2
if b<e:
for i in range (0,z.index(e)):
if z[i]==b:
print(i)
elif b==e:
print(z.index(e))
elif b>e:
for i in range(z.index(e)+1,c):
if z[i]==b:
print(i)

#Output :-
Practical
-24
Search for an element in a list

#Input :-
z=[]
x=int(input("Enter the number of elements :-"))
for i in range(x):
a=input("Enter the element:-")
z.append(a)
print("")
b=input("Enter the element to find:-")
c=len(z)
if b in z :
print("We found the element ")
print("Index values are:")
for i in range(0,c):
if b==z[i]:
print(i)
else:
print("Element is not there in the list or tuple")
print("")
y=int(input("Enter 0 if want to print the list"))
if y==0:
print(z)
elif y>0:
print("")z=[]
x=int(input("Enter the number of elements :-"))
for i in range(x):
a=input("Enter the element:-")
z.append(a)
print("")
b=input("Enter the element to find:-")
c=len(z)
if b in z :
print("We found the element ")
print("Index values are:")
for i in range(0,c):
if b==z[i]:
print(i)
else:
print("Element is not there in the list or tuple")
print("")
y=int(input("Enter 0 if want to print the list"))
if y==0:
print(z)
elif y>0:
print("")

#Output :-
Practic
al -25
Create a dictionary with the roll number, name and
marksof n students in a class and display the names of
students who have scored marks above 75

#Input :-
z={}
x=int(input("Enter the number of students:-"))
for i in range(x):
b=input("Enter the name of the student:-")
a=input("Enter the roll number of the student:-")
c=int(input("Enter the marks of the student:-"))
print()
z[a]=[b,c]
print("-----------NAMES OF THE STUDENTS SCORED ABOVE 75 --------")
for i in z:
if (z[i][1]) > 75:
print(z[i][0])

#Output :-
Practic
al -26
create a dictionary about heritage sites – to add and search for sites (key) - name of the site
(value) - location, city, State, When built, Who built , Website link (if any)

#Input :-
page={}
while True:
print("")
print("-------Heritage Sites------")
print("")
print("1. Add the site")
print("2. Search for a site")
print("3. Print all the Sites")
print("")
x=int(input("Enter your choice:-"))
if x==1:
a=input("Enter the name of the site:-")
b=input("Enter the address of the site:-")
c=input("Enter the year of the built:-")
d=input("Enter the name of the person who built:-")
page[a]=[b,c,d]

if x==2:
e=input("Enter the name of the site:-")
print("NAME OF THE SITE---",e)
print("Adress OF THE SITE---",page[e][0])
print("Year built OF THE SITE---",page[e][1])
print("Name of the person built SITE---",page[e][2])

if x==3:
for i in page:
print("NAME OF THE SITE---",i)
print("Adress OF THE SITE---",page[i][0])
print("Year built OF THE SITE---",page[i][1])
print("Name of the person built SITE---",page[i][2])
print("")

#Output :-

You might also like