You are on page 1of 29

All Saints School

Agra

PRACTICAL FILE
COMPUTER SCIENCE
[NEW] [083]

SESSION: 2019-20

Submitted By: Submitted To:

Name: ABIGAIL
RODRIGUES Mr. SK Sharma
Class: XII-B PGT(Comp.sc)
Acknowledgement
I would like to express a deep sense of thanks &
gratitude to my subject teacher Mr. SK Sharma for guiding
me immensely through the course of the computer lab
practical. He always evinced keen interest in my work. His
constructive advice & constant motivation have been
responsible for the successful completion of this computer
lab file report.
My sincere thanks go to Mr. Yogesh Upadhyay, our
principal sir, for his co-ordination in extending every
possible support for the completion of this practical lab
file.
I also thanks to my parents for their motivation &
support. I must thanks to my classmates for their timely
help & support for compilation of this practical lab file.
Last but not the least, I would like to thank all those
who had helped directly or indirectly towards the
completion of this practical lab file.

ABIGAILRODRIGUES

Class:XIIB
Certificate
This is to certify that School ABIGAIL
RODRIGUES of class XII-B, All Saint School,
Agra has successfully completed his lab practical
in computer science (083) for the AISSCE as
prescribed by CBSE in the year 2019-20.
The report is the result of her efforts &
endeavors. She has prepared the computer lab file
under my guidance.

Date:

Subject In-charge Academic Director Principal


Index
Sl. PRACTI Date Sign
No. CAL

1. Recursively find the factorial of a


natural number

2. Read a file line by line and print it

3. Remove all the lines that contain the


character `a' in a file and write it to
another file.

4. Write a Python function sin(x, n) to


calculate the value of sin(x) using its
Taylor series expansion up to n terms.
Compare the values of sin(x) for different
values of n with the correct value

5. Write a random number generator that


generates random numbers between 1
and 6 (simulates a dice).

6. Write a recursive code to find the sum of


all elements of a list

7. Write a recursive code to compute the n


Fibonacci number.

8. Write a Python program to implement a


stack and queue using a list data-
structure.
9. Write a recursive Python program to test
if a string is a palindrome or not.

10. Write a Python program to plot the


function y = x2 using the pyplot or
matplotlib libraries.

11. Create a graphical application that


accepts user inputs, performs some
operation on them, and then writes the
output on the screen. For example, write
a small calculator. Use the tkinter library.

12. Compute EMIs for a loan using the


numpy or scipy libraries.

13. Program that receives two numbers in a


function and returns the result of all
arithematic operations.

14. Program to creat a dictionary containing


names of completion winner students as
keys and number of their wins as values

15. Write a recrusive function to print a


string backwards.

16. Find the min, max, sum, and average


of the marks in a student marks
table.

17. Find the total number of customers from


each country in the table (customer ID,
customer name, country) using group by.

18. Write a SQL query to order the (student


ID, marks) table in descending order of
the marks.
19. Integrate SQL with Python by importing
the MySQL module
20. Write a SQL program table name
“softdrink”.write a query for the
following.
PRACTICAL-1

1. Recursively find the factorial of a natural number.


def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
num = int(input('Enter a number:- '))
if num< 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",factorial(num))

OUTPUT :--
PRACTICAL-2

2. Read a file line by line and print it.

def COUNTLINES():
file=open('file.txt','r')
count=0
for w in file.readlines():
if w[0]=="A" or w[0]=="a":
count=count+1
print('Total lines ',count)
COUNTLINES()

OUTPUT :--
PRACTICAL 3

3. Remove all the lines that contain the character `a' in a file
and write it to another file.

file=open('file.txt','r')
for i in file:
print(i)

OUTPUT;-
PRACTICAL 4
4. Write a Python function sin(x, n) to calculate the value of
sin(x) using its Taylor series expansion up to n terms.
Compare the values of sin(x) for different values of n with the
correct value.
import numpy as np
import matplotlib.pyplot as plt
def mysin(x, order):
a=x
s=a
for i in range(1, order):
a *= -1 * x**2 / ((2 * i) * (2 * i + 1))
s += a
return s
mysin = np.vectorize(mysin, excluded=['order'])
x = np.linspace(-80, 80, 500)
y2 = mysin(x, 2)
y10 = mysin(x, 10)
y100 = mysin(x, 100)
y1000 = mysin(x, 1000)
y = np.sin(x)
plt.plot(x, y, label='sin(x)')
plt.plot(x, y2, label='order 2')
plt.plot(x, y10, label='order 10')
plt.plot(x, y100, label='order 100')
plt.plot(x, y1000, label='order 1000')
plt.ylim([-3, 3])
plt.legend()
plt.show(
OUTPUT:-
PRACTICAL 5

5. Write a random number generator that generates random


numbers between 1 and 6 (simulates a dice).

import random
for i in range(11):
print("Dice value:- ",random.randint(1,6))

OUTPUT:-
PRACTICAL 6

6. Write a recursive code to find the sum of all elements of a


list.

def findSum(arr, N):


if len(arr)== 1:
return arr[0]
else:
return arr[0]+findSum(arr[1:], N)
arr =[]
arr = [1, 2, 3, 4, 5]
N = len(arr)
ans =findSum(arr,N)
print("The list is :-",arr)
print ("Sum of all elements in the array:-",ans)

OUTPUT:-
PRACTICAL 7

7. Write a recursive code to compute the n Fibonacci number.


print("***Fibonacci series***")
n = int(input("Enter the value of n "))
a=0
b=1
for i in range(n):
print(a)
c=a+b
a=b
b=c

OUTPUT:-
PRACTICAL 8

8. Write a Python program to implement a stack and queue


using a list data-structure.
stack = ["Amar", "Akbar", "Anthony"]
stack.append("Ram")
stack.append("Iqbal")
print(stack)
print(stack.pop())
print(stack)
print(stack.pop())
print(stack)

OUTPUT:-

from collections import deque


queue = deque(["Ram", "Tarun", "Asif", "John"])
print(queue)
queue.append("Akbar")
print(queue)
queue.append("Birbal")
print(queue)
print(queue.popleft())
print(queue.popleft())
print(queue)
.
OUTPUT:-
PRACTICAL 9

9. Write a recursive Python program to test if a string is a


palindrome or not.

string = input("Enter a string ")


new=string[::-1]
for i in range(len(string)):
if string[i]!=new[i]:
print("String is not Palindrome")
break
else:
print("String is Palindrome")

OUTPUT: -
PRACTICAL 10

10. Write a Python program to plot the function y = x2 using


the pyplot or matplotlib libraries.

from matplotlib import pyplot as plt


x1, y1 = [], []
for x in range(-25,25):
y = x*x
x1.append(x)
y1.append(y)
plt.plot(x1,y1)
plt.show()

OUTPUT:-
PRACTICAL 11

11. Create a graphical application that accepts user inputs,


performs some operation on them, and then writes the output
on the screen. For example, write a small calculator. Use the
tkinter library.

from tkinter import *


window = Tk()
window.title("Calculator")
w=300
h=200
ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
window.geometry('%dx%d+%d+%d' % (w, h, x, y))
window.resizable(0, 0)

def Add():
L4=Label(window,text="Result : "+str(int(I1.get())+int(I2.get())))
L4.grid(column=2, row=8)

def Sub():
L4=Label(window,text="Result : "+str(int(I1.get())-int(I2.get())))
L4.grid(column=2, row=8)

def Mul():
L4=Label(window,text="Result : "+str(int(I1.get())*int(I2.get())))
L4.grid(column=2, row=8)

def Div():
L4=Label(window,text="Result : "+str(float(I1.get())/float(I2.get())))
L4.grid(column=2, row=8)
L1 = Label(window, text = "Number 1")
L1.grid(column=2, row=2)
L2 = Label(window, text = "Number 2")
L2.grid(column=2, row=4)
I1 = Entry(window, width = 10)
I1.grid(column=4, row=2)
I2 = Entry(window, width = 10)
I2.grid(column=4, row=4)
B1 = Button(window, text = "Add",command = Add)
B1.grid(column=1, row=6)
B2 = Button(window, text = "Subtract",command = Sub)
B2.grid(column=2, row=6)
B3 = Button(window, text = "Multiply",command = Mul)
B3.grid(column=3, row=6)
B4 = Button(window, text = "Divide",command = Div)
B4.grid(column=4, row=6)
window.mainloop()

OUTPUT:--
PRACTICAL 12

12. Compute EMIs for a loan using the numpy or scipy


libraries.
import numpy as np
loan_value = int(input("Enter Loan Amount:- "))
interest = float(input("Enter rate of interest:- "))
annual_rate = interest/100.0
monthly_rate = annual_rate/12
years = float(input("Enter Time Period:- "))
number_month = years * 12
monthly_payment = abs(np.pmt(monthly_rate, number_month, loan_value))
sf1 = "Paying off a loan of ${:,} over {} years at"
sf2 = "{}% interest, your monthly payment will be ${:,.2f}"
print(sf1.format(loan_value, years))
print(sf2.format(interest, monthly_payment))

OUTPUT:-
PRACTICAL 13

13.Program that receives two numbers in a function and


returns the result of all arithematic operations.
def arCalc(x,y):
return x+y,x-y,x*y,x/y,x%y
# main
num1=int(input("ENTER NUMBER 1:"))
num2=int(input("ENTER NUMBER2:"))
add,sub,mult,div,mod=arCalc(num1,num2)
print("SUM OF THE GIVEN NUMBER IS:",add)
print("SUBSTRACTION OF GIVEN NUMBERS:",sub)
print("PRODUCT OF GIVEN NUMBERS:",mult)
print("DIVISION OF GIVEN NUMBERS:",div)
print("MODULO OF GIVEN NUMBERS:",mod)

OUTPUT:
PRACTICAL 14

14.Program to creat a dictionary containing names of


completion winner students as keys and number of their wins
as values
n=int(input("how many students?"))
CompWinners={}
for a in range(n):
key=input("name of the student:")
value=int(input("number of competitions won:"))
CompWinners[key]=value
print("the dictionary now is:")
print(CompWinners)

OUTPUT:-
PRACTICAL 15

15.Write a recrusive function to print a string backwards.

#simple recursion
def bp(sg,n):
if n>0:
print(sg[n],end='')
bp(sg,n-1)
elif n==0:
print(sg[0])

# main
s=input("ENTER A STRING:")
bp(s,len(s)-1)

OUTPUT:-
Data Management: SQL and
web-server
Q1. Find the min, max, sum, and average of the marks
in a student marks table.
Create table student (sid int PRIMARY KEY ,name varchar(20),marks
int);
Insert into student values(101,'Bill',45);
Insert into student values(102,'Mary',31);
Insert into student values(103,'Sue',95);
Insert into student values(104,'Tom',35);
Insert into student values(105,'Alex',75);
Insert into student values(106,'Sam',25);
Insert into student values(107,'Joan',90);
Select min(marks) as Minimum_Marks,max(marks)as
Maximum_Marks,sum(marks) as Sum,avg(marks) as Average from
student;

OUTPUT:-
2. Find the total number of customers from each country in
the table (customer ID, customer name, country) using group
by.

Create table customer (customer_id int PRIMARY


KEY ,customer_name varchar(20), Country varchar(10));
Insert into customer values(101,'Billy','India');
Insert into customer values(121,'James','USA');
Insert into customer values(161,'Tina','France');
Insert into customer values(301,'Ana','India');
Insert into customer values(201,'Marky','India');
Insert into customer values(109,'Thomas','France');
Insert into customer values(115,'Joan','India');
Select Country,count(customer_id) from customer group by Country;

OUTPUT:-
3. Write a SQL query to order the (student ID, marks) table in
descending order of the marks.

Create table student (student_id int PRIMARY KEY ,name


varchar(20),marks int);
Insert into student values(101,'Bill',45);
Insert into student values(102,'Mary',31);
Insert into student values(103,'Sue',95);
Insert into student values(104,'Tom',35);
Insert into student values(105,'Alex',75);
Insert into student values(106,'Sam',25);
Insert into student values(107,'Joan',90);
Select student_id,marks from student order by marks desc;

OUTPUT:-

You might also like