Sunteți pe pagina 1din 21

COMPUTER SCIENCE

PRACTICAL FILE
2019-2020

Name-Khushi Sahay
Class-XII-A
Roll No:-

1
ACKNOWLEDGEMENT
It gives me immense pleasure to present my
practical file.It would not have been possible
without the kind support of my teacher in charge,
Ms. Monika Jain under whose guidance and
constant supervision the file was brought to the
present state.I would also like to express my
gratitude towards my parents for their kind co-
operation and encouragement which helped me in
the completion of this practical file. Last but not
the least, I thank my friends who shared necessary
information and useful web links for preparing my
file. Thanks again to all.
Khushi Sahay

2
#1 Program to sort a list such that all the even
numbers are on extreme right and the odd on
extreme left.
list1=[]
n=int(input("enter number of elements:"))
for i in range(n):
ele=int(input('enter element:'))
list1.append(ele)
print("list",list1)
index=0
for t in list1:
no=list1[index]
if no%2==0:
list1.remove(no)
list1.append(no)
else:
index+=1
print("sorted list:",list1)

Output:-

3
#2 Create a dictionary containing names of
competition winners as keys and no. of their wins
as values and display the name of student with
max wins.
n=int(input("enter the number of students:"))
dict1={}
for i in range(n):
name=input("enter the name of the student:")
dict1[name]=int(input("Number of wins: "))
print("dictionary:",dict1)
max_win=0
for i in dict1:
if dict1[i]>max_win:
max_win=dict1[i]
winner=i
print(winner,"has maximum number of
wins:",max_win)

Output:-

4
#3 Program to sort a list in descending order using bubble
sort.
list1=[]
n=int(input("enter number of elements:"))
for i in range(n):
ele=int(input('enter element:'))
list1.append(ele)

for no in range(n):
for j in range(n):
if list1[no]>list1[j]:

list1[no],list1[j]=list1[j],list1[no]
print("sorted list:",list1)

Output:-

5
#4 Program to sort a list in ascending order using
selection sort.
list1=[]
n=int(input("enter number of elements:"))
for i in range(n):
ele=int(input('enter element:'))
list1.append(ele)
for no in range(n):
min_idx = no
for j in range(no+1,n):
if list1[min_idx] > list1[j]:
min_idx = j
list1[no],list1[min_idx] =
list1[min_idx],list1[no]
print("sorted list:",list1)

Output:-

6
#5 Binary Search
def binary_search(val,low,high,l1):
if high<low:
return None
else:
mid=low+((high-low)//2)
if l1[mid]>val:
return binary_search(val,low,mid 1,l1)
elif l1[mid]<val:
return binary_search(val,mid+1,high,l1)
else:
print(mid)

list1=[]
n=int(input("enter number of elements:"))
for i in range(n):
ele=int(input('enter element:'))
list1.append(ele)
val=int(input("enter the number to be
searched:"))
binary_search(val,0,len(list1)-1,list1)

Output:-

7
#6 Program to find the nth Fibonacci number using
recursion.
def Fibonacci(n):
if n<0:
print("Incorrect input")
elif n==1:
return 0
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
num=int(input("Enter the term:"))
print(Fibonacci(num))

Output:-

#7 Program to generate a random number between


1 and 6.
import random
number=random.randint(1,6)
print(number)

8
#8 Program that asks for two numbers in a
function and returns the result of all arithmetic
operations on these numbers
def calculator(no1,no2):
print("Sum= ",no1+no2)
print("Difference= ",no1-no2)
print("Product= ",no1*no2)
print("Quotient= ",no1/no2)
print("Remainder= ",no1%no2)

Output:-

9
#9 Program to create a table student with 10
dummy records.add,delete,update any record and
display all details in descending order of primary
key.

10
Updation

Deletion

Output

11
Records in descending order

#10 Query to display no of students in each house

12
#11 Program to use aggregate functions on marks
column

13
#12 Query to join two tables using left join and
right join

Right Join

Left Join

14
#13 Program to read data from a file and copy it to
another file
readfile=open("abc.txt","r")
str1=readfile.read()
writefile=open("xyz.txt","w")
writefile.write(str1)
writefile.close()
readfile.close()

Output:-

15
#14 Program that reads data from file 1 and copy
all the words starting with ‘a’ in file 2.
myfile=open("myfile.txt","r")
myfile_write=open("copyfile.txt","w")
for line in myfile:
words=line.split(" ")
for j in words:
if j[0]=='a':
myfile_write.write(j+"\n")
myfile_write.close()
myfile.close()

Output:-
Myfile

16
Copyfile

#15 Program to create a function that swaps the


values of two variables
def swap_values(no1,no2):
no1,no2=no2,no1
print("Swapped values:- ",no1,no2)

Output:-

17
#16 Program that will display a ‘#’ after every
word of file 1 and also display the size of the file.
myfile=open("abc.txt")
size=0
for i in myfile:
line=i.strip('\n')
for j in i:
size+=1
word=line.split(' ')
for m in word:
print(m,"#")

print("Size:-",size)
myfile.close()

Output:-

18
#17 Program that will split all the lines starting
with ‘p’ on parameter ‘,’.
myfile=open("abc.txt")
for line in myfile:
if line[0]=='p':
print(line.split(','))

Output:-

19
SQL+PYTHON
#18 In a table use insert and update
import mysql.connector
sqlconnect=mysql.connector.connect(host="lo
calhost",user="Khushi",passwd="modern",data
base="test",port=3307)
query=sqlconnect.cursor()
query.execute("CREATE TABLE IF NOT EXISTS
student102(Admn int,Name char(20),Age
int)")
sql="INSERT INTO student102(Admn,Name,Age)
VALUES(%s,%s,%s)"
val=(1,'riya',17)
query.execute(sql,val)
sqlconnect.commit()
sql="INSERT INTO student102(Admn,Name,Age)
VALUES(%s,%s,%s)"
val=(2,'Priya',16)
query.execute(sql,val)
sqlconnect.commit()
Output:-

20
Updation
query.execute("UPDATE student102 SET
Age=17 WHERE Admn=2")
sqlconnect.commit()

Output:-

#19 Delete a record


query.execute("DELETE FROM student102
WHERE Admn=1")
sqlconnect.commit()

Output:-

21

S-ar putea să vă placă și