Computer
Computer
Computer
CHHATARPUR
Session : 2022-23
Computer Science
Project File
Date :
Principal’ s Signature
ACKNOWLEDGEMENT
import math
print("For quadratic equation ax^2+bx+c=0, enter coefficients")
a= int(input("Enter a value for coefficient a: "))
b= int(input("Enter value for coefficient b : "))
c= int(input("Enter a value of constant c :"))
if a ==0:
print("Value of",a,"should not be zero")
print("Aborting!")
else :
delta= b*b-4*a*c
if delta > 0:
root1 = (-b+math.sqrt(delta))/(2*a)
root2 = (-b-math.sqrt(delta))/(2*a)
print("Roots are real and unequal")
print("Root=1",root1,"Root2",root2)
elif delta ==0:
root3=(-b+math.sqrt(delta))/(2*a)
print("Roots are real and equal")
print("roots=",root3,root3)
else:
print("Roots are complex and imaginary")
Output
For quadratic equation ax**2+bx+c=0,enter coefficients
Enter value of coefficient a :1
Enter value of coefficient b :-6
Enter value of constant c :5
Roots are real and unequal
Root1=5.0 Root2=1.0
Program-2
Call by reference and Call by value
#call by value
def f1(a,b):
a=a+1
b=b+1
print("Inside function f1:",a,b)
#call by reference
def f2(L):
L[1]=33
print("Inside function f2:",L)
#__main__
x,y= 10,20
f1(x,y)
print("In main function x,y:",x,y)
lst[10,20,30]
print("In main before function call lst:",lst)
f2(lst)
print("In main after function call lst:",lst)
Output
Inside function f1: 11 21
In main function x,y : 10 20
In main before function call lst:[10,20,30]
Inside function f2:[10,33,30]
In main after function call lst:[10,33,30]
Program-3
Demonstration of types of arguments
def positional(a,b):
print(a-b,end='\t')
def default(a,b=10):
print(a-b,end='\t')
def keyword (c1,c2,c3):
print("\tThe youngest child is:",c3)
def variable(*arg):
print(arg,end='\t')
#__main__
print('Positional Argument')
positional(100,200)
positional(200,100)
print("\nDefault Argument")
default(100,200)
default(100)
print("\nKeyword Argument")
keyword (c3='Amar', c1= 'Akbar', c2='Anthony')
print("Variable Length Argument")
variable(2,3,4)
Variable(1,2)
Output
Positional Argument
-100 100
Default Argument
-100 90
Keyword Argument
The youngest child is: Amar
Variable Length Argument
(2,3,4) (1,2)
Program-4
Demonstration of scope of variable [LEGB] rule
import math
p=5
def sum(q,r=2):
global p
p=r+q**2
return p
a,b=10,5
print('First function call sum(a,b):',sum(a,b))
print('First function call sum(r=5,q=1):',sum(r=5,q=1))
print("The value of Pi",math.pi) #Extended scope
Output
First function call sum(a,b): 105
First function call sum(r=5,q=1): 6
The value of Pi 3.141592653589793
Program-5
Creating a package and accessing two modules from it
../
Geometry Package
area.py
vol.py
__init__.py
Shape.py
area.py
def Cir(r):
'''Function for area of circle'''
print(__name__)#print current module name
return 3.14*r*r
def Rect(a,b):
return a*b
vol.py
def cuboid(l,b,h):
print(__name__)#print current module name
return l*b*h
Shape.py
from geometry import area,vol
print(area.Cir(10)) # 314.0
print(help(area.Cir)) # function for area of circle
# print the doc string of function
print(area.Rect(10,20))
#200
print(vol.cuboid(10,20,5)) #10000
print(__name__) #print current module name
Output
3.14.0,200,1000
Program-6
Count number of lines in a text file
f=open("a.txt",'r')
line_count=0
for line in f:
line_count+=1
print("The number of lines in a.txt is:", line_count)
Output
The number of lines in a.txt is: 2
Program-7
Count number of words in a text file
f=open("a.txt",'r')
word_count= 0
content= f.read()
word_lst= content.split( )
word_count= len(word_lst)
print("The number of words in poem.txt is : ",word_count)
Output
The number of word in a.txt is: 8
Program-8
Working with tell and seek function
f=open("a.txt",'w')
line='Welcome to KV Chhatarpur\n Regularly attend CS classes'
f.write(line)
f.close()
f=open("a.txt",'r+')
print((f.read(7)))
# read seven character
print(f.tell())
print(f.read())
print(f.tell())
f.seek(9,0)
# moves to 9 position from beginning
print(f.read(5))
f.close()
Output
0
Welcome
7
To Care English School
Regularly attend CS classes
54
O Ca
Program-9
Finding a character in a file and write it to another file
file_inp=open('input.txt','r+')
for line in file_inp:
if 'a' in line:
file_out=open("output.txt",'a+')
file_out.write(line)
file_out.close( )
file_inp.close( )
Output
Program-10
Stack Implementation using list
def isEmpty(stk):
if stk==[]:
return True
else:
return False
def Push(stk,item):
stk.append(item)
top=len(stk)-1
def Pop(stk):
if isEmpty(stk):
return "Underflow"
else:
item=stk.pop()
if len(stk)==0:
Top=None
else:
top=len(stk)-1
return item
def Display(stk):
if isEmpty(stk):
print("Stack Empty")
else:
top=len(stk)-1
print(stk[top],"<--top")
for a in range(top-1,-1,-1):
print(stk[a])
#___main()____
Stack=[]
top=None
while True:
print("---Stack Operations---")
print("1. Push","2. Pop", "3. Display","4. Exit")
ch= int(input("Enter your choice(1-5):"))
if ch==1:
item=int(input("Enter item: "))
Push(Stack,item)
elif ch==2:
item=Pop(Stack)
if item=="Underflow":
print("Underflow :stack is empty")
else:
print("Popped item is",item)
elif ch==3:
Display(Stack)
elif ch==4:
break
else:
print("Please enter the correct number in range(1-5)")
Output
Program-11
SQL queries
1. show database;
create database if not exists xiia23;
use xiia23;
2. create table DEPT(
DCODE varchar(3) primary key,
DEPARTMENT varchar(5),
CITY varchar(12));
Output
0 rows affected
3. describe DEPT;
Output
Output
5 rows affected
7. Display the details of all the departments
select * from DEPT;
Output
Output
5 rows affected
9. Display all the details of the workers
select * from WORKER;
Output
10. Display all the details of the workers working in D01 Department
select * from WORKER where DCODE=’D01’;
Output
13. Display the details of the workers having birthday after 01.07.1990
select * from WORKER where DOB > ‘1990-07-01’;
Output
14. Display the details of the workers who have joined after 01.01.2013 and before
01.01.2014
select * from WORKER where DOJ between ‘2013-01-01’ and
‘2014-01-01’;
Output
15. Display the details of female workers in descending order of their joining date
select * from WORKER where GENDER=’FEMALE’ order by DOJ desc;
Output
16. Display the names and worker number of the workers who live in delhi
select NAME,WNO from WORKER W,DEPT D where W.DCODE=D>DCODE
and D.CITY=’Delhi’;
Output
Output
Program-12
SQL queries
1. show databases;
create database if not exists xiia23;
use xiia23;
2. create table if not exist movie
(
No int(10) primary key,
Title varchar(30),
type varchar(30),
Rating varchar(15),
stars varchar(10),
qty int,
price float(10)
);
Output:
0 rows affected
3. describe movie;
Output:
4. insert into movie values
(1,"gone with wind ","drama","G","gable",4,39.95),
(2,"friday the 13th ","horror","R","jason",2,69.95),
(3,"top gun","drama","PG","cruise",7,49.95),
(4,"splash","comedy","PG13","hanks",3,29.95),
(5,"independence day ","drama","R","tumer",3,19.95),
(6,"risky business ","comedy","R","cruise",2,44.95),
(7,"cocoon ","scifi","PG","amecho",2,31.95),
(8,"crocodile dundee ","comedy","PG13","harris",2,69.95),
(9,"101 dalmatians ","comedy","G","",3,59.95),
(10,"tootsie ","comedy","PG","hoffman",1,29.95);
Output:
10 rows affected
5. Display a list of all movies with price over 20 & sorted by price
select * from movie where price>20 order by price;
Output:
7. Select Item name of those items which are quantity between 100-200
select * from item where qty between 100 and 200;
Output
8. Select all record of Items which contains pen word in it
select * from item where item like '%pen%';
Output
Output
16. Display all record whose dcode is not assigned
select * from item where dcode is null;
Output
Program-15
SQL queries
1. show database;
create database if not exists xiia23;
use xiia23;
2. create table if not exists student1(
no int(10) primary key,
name varchar(15),
stipend float(30),
stream varchar(15),
avgmark float(10),
grade char(3),
class varchar(3));
Output
0 rows affected
3. describe student1;
Output
4. Display all student details
select * from student1;
Output
10. Display the sum of stipend for students which get B grade
select sum(stipend) from student1 where grade=”B”;
Output
Program-16
Python to SQL database connectivity
import mysql.connector
from mysql.connector import Error
try:
global connection
Connection=\
mysql.connector.connect(host='localhost',database='mydatabase',\
user='root',password='care')
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
cursor.execute("select * from Student")
data = cursor.fetchall()
for i in data:
for j in i:
print(' \t|',j,end=" | ")
print()
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
Output
Connected to MySQL Server version 8.0.31-0ubuntu0.22.04.1
You're connected to database: ('mydatabase',)
| 1 | | Atharv Ahuja | | 2003-05-15 | | 444444444444 |
| 2 | | Daisy Bhutia | | 2002-02-28 | | 111111111111 |
| 3 | | Tahleem Shah | | 2002-02-28 | | 101010101010 |
MySQL connection is closed