[go: up one dir, main page]

0% found this document useful (0 votes)
13 views53 pages

12th Report File Bhargavi

CS Report

Uploaded by

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

12th Report File Bhargavi

CS Report

Uploaded by

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

DELHI PUBLIC SCHOOL JODHPUR

SESSION 2020-2021

COMPUTER SCIENCE
REPORT FILE

SUBMITTED TO: SUBMITTED BY:


Mrs. JYOTI MATHUR BHARGAVI
CHHANGANI
INDEX
1. Alphabets, vowels, constants and digits in a string
2. Calculation of billing amount
3. Lower and uppercase letters in string
4. Triple of an odd number and double of even number
5. Display table of a number
6. Max and min value of a list
7. Displaying numbers ending with 2 from list
8. Exchanging the cases of letters
9. Cube of an odd number and square of even
10. Interest amount calculator
11. Creating our own module
12. Factorial of smallest number in a list
13. Number of time a number appears in a integer list
14. Using the functions of inbuilt math module
15. Reading a text file
16. Number of words starting with letter ‘a’ in text file
17. Number of times given word appear in the file
18. Number of occurrences of letter ‘a’ and ‘b’ in file
19. Write a line in the file
20. Reading CSV file
21. Inserting a new record in CSV File
22. Number of records
23. Stack operation in python
24. Displaying the sql table in python
25. Searchin a record of sql table in python
26. 25 SQL Queries
Program 1:
Takes any string as an input and then displays the
number of alphabets, vowels, consonants and digits in
the string:
CODE:
word=input('Enter a string:')
acount=dcount=vcount=ccount=0
for x in word:
if x.isalpha():
acount=acount+1
if x in('a','e','i','o','u'):
vcount=vcount+1
else:
ccount=ccount+1
elif x.isdigit():
dcount=dcount+1
print('Number of alphabets: ',acount)
print()
print('Number of vowels: ',vcount)
print()
print('Number of consonants: ',ccount)
print()
print('Number of digits ',dcount)
print()

OUTPUT: (next page)


Program 2:
Calculation of billing amount
CODE:
# creating a bill
itemname=input('Enter the item name ')
price=float(input('Enter the price of each item '))
quan=int(input('Enter the quantity '))
Amount=price*quan
print('Amount to be paid ',Amount)
OUTPUT:
Program 3:
Takes a string as input and from all the alphabets that it
has, displays number of uppercase and lowercase letters.
CODE:
word=input('Enter a string:')
ucount=lcount=0
for x in word:
if x.isalpha():
if x.islower():
lcount=lcount+1
else:
ucount=ucount+1
print('Number of upper case letters: ',ucount)
print()
print('Number of lower case letters: ',lcount)
print()
OUTPUT:
Program 4:
Triple of an odd number and double for even number
CODE:
x=int(input('Enter the number '))
if x %2==0:
print('This is an even number')
print('Double the number is: ', 2*x)
else:
print('This is an odd number')
print('Triple the number is: ',3*x)
OUTPUT:
Program 5:
display table of a given number
CODE:
num=int(input('Enter the number:'))
for x in range(1,11):
if x==10:
print(num,' X ',x,'= ',num*x)
else:
print(num,' X ',x,' = ',num*x)

OUTPUT:

Program 6:
To display the maximum value and minimum value in a
list of number accepted as input.
CODE:
n=int(input('Enter the number of elements that are to be inserted
in list'))
L=[]
for x in range(0,n):
num=int(input('Enter a number'))
L.append(num)
print('Inserted list: ',L)
print()
L.sort()
print('Lowest value in the list: ',L[0])
print('Highest value in the list: ',L[n-1])

OUTPUT:

Program 7
Takes an integer list as input and then displays the
number ending with digit 2
CODE:
n=int(input('Enter the number of elements that are to be inserted
in list'))
L=[]
for x in range(0,n):
num=int(input('Enter a number'))
L.append(num)
print('Inserted list: ',L)
endtwo=[]
for number in L:
if number%10==2:
endtwo.append(number)
print('Numbers in the list that end with 2: ',endtwo)

OUTPUT:
Program 8:
Defining a program named case_exchange() which
converts the letter in lowercase in uppercase and vice
versa
CODE:
#word must be string value that must be accepted before function call
def case_exchange(word):
newword=''
for x in word:
if x.islower():
newword=newword+x.upper()
elif x.isupper():
newword=newword+x.lower()
else:
newword=newword+x
global n
n=newword
print(newword)
n=input('Enter a word')
case_exchange(n)

OUTPUT:
Program 9:
Defining a function Cube_Square(n) that displays
the cube and square of a number.
CODE:
#n must be an int value that must be accepted as
a parameter in function call
def cube_square(n):
cube=n**3
sq=n**2
print('Cube of the given number is:',cube)
print('Square of the given number is:',sq)
cube_square(5)
OUTPUT:
Program 10
Defining a function Interest() that calculates the
interest amount
CODE:
def Interest(P,R=0.1,T=10):
I=(P*R*T)/100
print('The interest amount is:',I)
Interest(20000)

OUTPUT:

R and T in this function are default parameters, they are


assigned the defined values when no value is given in the
function call.
Program 11:
To create your own module and using its functions
in other program
CODE:
A module(.py file) named file.py is created in which

Thereafter, the function in this module is used through importing functions from
File.py to this program named 'FileRun'
OUTPUT:

This module(file.py) can be used in other


programs just by importing the modules
or just by importing the specific
functions in the program.
Program 12:
Takes an integer list and then displays the factorial of the
smallest number in the list.
CODE:
n=int(input('Enter the number of elements that are to be inserted in list'))
L=[]
for x in range(0,n):
num=int(input('Enter a number'))
L.append(num)
print('Inserted list: ',L)
L.sort()
minnum=L[0]
x=2
factnum=1
while x<=minnum:
factnum=factnum*x
x=x+1

print('factorial of the smallest number in the list is')


print(factnum)

OUTPUT:
Program 13
Finds the number of times the given number appears in a
specified list.
CODE:
L=[1,1,6,3,6,8,3,0,4,8,2,2,7]
count=0
def searchval(n):
for x in L:
if x==n:
global count
count+=1
else:
continue
return count
n=int(input('Enter the number you want to search in the list '))
searchval(n)
if count==0:
print('Number not found in the list.')
else:
print('Number of times the given number appears in the list
is',count)
OUTPUT:
Program 14:
Using the functions sqrt and pow of inbuilt library math
after importing it in python to display the square roots of
numbers in the specified list
CODE:
import math
numbers=[144,121,81,196,49,289]
for x in numbers:
print('The square root of ',x,' is:')
print(math.sqrt(x))
print(math.pow(x,0.5))
OUTPUT:
Text files

Covid.txt

Books.txt
Further text file handling programs would be based
on these two text files
Program 15:
Reading the contents of the text file
‘Books.txt’.
CODE:
myfile=open('books.txt')
content=myfile.read()
print(content)
myfile.close()

OUTPUT:
Program 16:
Counting the number of letter ‘a’ in the text
file ‘Books.txt’.
CODE:
myfile=open('books.txt')
content=myfile.read()
count=0
for x in content:
if x in('A','a'):
global count
count+=1
if count!=0:
print("The number of times 'a' letter appears is:
",count)
if count==0:
print("'a' letter does not appears in this file.")
myfile.close()
OUTPUT:
Program 17:
To count the number of times the word ‘virus’
appears in the text file ‘Covid.txt’.
CODE:
myfile=open('covid.txt')
content=myfile.read()
count=0
words=content.split()
for x in words:
if x.lower()=='virus':
count+=1
if count!=0:
print("The number of times 'virus' appears is: ",count)
if count==0:
print("'Virus' does not appear in this file.")
myfile.close()
OUTPUT:
Program 18:
To count the number of words starting with ‘a’ and
‘b’ letter in the text file ‘Books.txt’.
CODE:
myfile=open('books.txt')
content=myfile.read()
counta=0
countb=0
words=content.split()
for x in words:
x=x.lower()
print(x)
if x[0]=='a':
counta+=1
elif x[0]=='b':
countb+=1
if counta!=0:
print("The number of words starting from a is ",counta)
else:
print('No words start from the letter A')
if countb!=0:
print("The number of words starting from B is ",countb)
else:
print('No words start from the letter B')
OUTPUT:
Program 19:
To write a line in the text file ‘covid.txt’
CODE:
myfile=open('covid.txt','a')
x=1
while x==1:
line=input('Enter the line you want to insert in the file')
myfile.write(line)
ch=input('Want to add more??(y/n)')
if ch.lower()=='y':
x=1
else:
x=0
print(‘line inserted successfully’)
myfile.close()
OUTPUT:
‘Items.csv’:

Program 20:
To read the contents of csv file ‘items.csv’ through
python software
CODE:
import csv
myfile=open('items.csv')
read=csv.reader(myfile)
for x in read:
print(x)

OUTPUT:
Program 21:
To add a new record in the CSV file ‘items.csv’
CODE:
import csv
myfile=open('items.csv','a',newline='')
writer=csv.writer(myfile)
number='6'
name=input('Enter item name')
price=int(input('Enter price of item'))
quan=int(input('Enter quantity of item'))
rec=[number,name,price,quan]
writer.writerow(rec)
myfile.close()
print('Record saved successfully')
OUTPUT:
Program 22:
To count the number of records in the CSV file ‘items.csv’
CODE:
import csv
myfile=open('items.csv')
read=csv.reader(myfile)
count=0
for x in read:
count+=1
if count==0:
print('The file is empty')
else:
print('the number of records in this file is: ',count)
OUTPUT:
Program 23:
Program for stack implementation
CODE:
S=[2,5,4,8,2,7,3,6]
def isEmpty(S):
if len(S)==0:
return True
else:
return False
def Push(S,item):
S.append(item)
top=len(S)-1
def pop(S):
if isEmpty(S):
return 'Underflow'
else:
val=S.pop()
if len(S)==0:
top=None
else:
top=len(S)-1
return val
def Peek(S):
if isEmpty(S):
return 'Underflow'
else:
top=len(S)-1
return S[top]
def Show(S):
if isEmpty(S):
print('Sorry no item in the stack')
else:
t=len(S)-1
print('Top', end='')
while(t>=0):
print(S[t],'<==',end='')
t-=1
print()

# main program
S=[2,5,4,8,2,7,3,6]
top=None
while True:
print('STACK Demonstration')
print('1. Push')
print('2. Pop')
print('3. Peek')
print('4. Show Stack')
print('0. Exit')
ch=int(input('Enter your choice'))
if ch==1:
val=int(input('Enter your choice:'))
Push(S,val)
elif ch==2:
val=pop(S)
if val=='Underflow':
print('Stack is Empty')
else:
print('\ndeleted item was:',val)
elif ch==3:
val=Peek(S)
if val=='Underflow':
print('Stack Empty')
else:
print('Top item',val)
elif ch==4:
Show(S)
elif ch==0:
print('Bye')
break

OUTPUT:
Programs of Interface of Python and
SQL
Program 24:
CODE:
import mysql.connector as sqlcon
def show():
mycon=sqlcon.connect(host="localhost", user="root",
passwd="scott", database="project")
cursor=mycon.cursor()
cursor.execute("Select * from Employee")
data=cursor.fetchall()
count=cursor.rowcount
print('Number of rows',count)
for x in data:
print(x)
mycon.close()
show()

OUTPUT:
Program 25:
CODE:
def showdname():

try:

mycon= sqlcon.connect(host="localhost", user="root", passwd="scott", database="project")

cursor=mycon.cursor()

dno=input("Enter Department Number : ")

cursor.execute("Select * from Employee where Dept='{}'".format(dno))

data=cursor.fetchall()

count=cursor.rowcount

if count==0:

print("No matching record")

return

for x in data:

print(x)

print("Row count in department table: ", count)

mycon.close()

except:

print("connectivity error")

showdname()

OUTPUT:
SQL Queries
Table Creation Query:
QUERY 01:
Create table Employee
(No int(3), Name varchar(20), salary double(7,2),
Zone varchar(10), Age int(2), Grade char(1), Dept
int(2) );
OUTPUT:

QUERY 02:
Create table Department (Dept int(2), Dname
varchar(15),
Minsal double(7,2), Maxsal double (7,2), HOD
int(1));
OUTPUT:
Inserting values in a table:
QUERY 03:
Insert into Employee values
(1, 'Mukul', 30000, 'West', 28, 'A',10),
(2, 'Kritika', 35000, 'Center', 30,'A',10),
(3, 'Naveen', 32000, 'West', 40, NULL, 20),
(4, 'Uday',38000,'North',38,'C',30),
(5,'Nupur', 32000,'East',26,NULL,20),
(6,'Moksh',37000,'South',28,'B',10),
(7,'Shelly',36000,'North',26,'A',30);

OUTPUT:
QUERY 04:
Insert into Department values
(10,'Sales', 25000,32000,1),
(20,'Finance',30000,50000,5),
(30,'Admin',25000,40000,7);

OUTPUT:
Viewing all the content from the
tables:
QUERY 05:
Select * from Department;
OUTPUT:

QUERY 06:
Select * from Employee;
OUTPUT:
Displaying particular data from table
(based on conditions):
QUERY 07:
Select * from department where Minsal=25000;
OUTPUT:

QUERY 08:
Select * from Employee where Grade is NULL:
OUTPUT:

Displaying unique values in Zones column in


the Employee table:
QUERY 09:
Select distinct Zone from Employee;
OUTPUT:

Displaying all values in Zone column of


Employee table:
QUERY 10:
Select all Zone from Employee;
OUTPUT:

Displaying names of Employee from the


Employee table whose name starts with
letter ‘N’
QUERY 11:
Select Name from Employee where Name like ‘N
%’;
OUTPUT:
Displaying sum of all the maximum salaries
given in the departments
QUERY 12:
Select sum(maxsal) from Department:
OUTPUT:

Dispalying total number of employees


working in each Department:
QUERY 13:
Select Dept, count(*) from Employee group by
Dept;
OUTPUT:
Assigning value of Grade as B whose Grade is
not assigned(NULL):
QUERY 14:
Update Employee set Grade=’B’
where Grade is NULL;
OUTPUT:

Adding a column HireDate of datatype date in


employee table
QUERY 15:
Alter table Employee
Add HireDate date;
OUTPUT:
Deleting column from the table
QUERY 16:
Alter table Employee
Drop column HireDate;
OUTPUT:

Displaying the structure of table:


QUERY 17:
Describe Employee;
OUTPUT:
QUERY 18:
Desc Department;
OUTPUT:

Adding any field in the table as primary key:


QUERY 19:
Alter table Employee
Modify No int(3) primary key;
OUTPUT:

QUERY 20:
Alter table Department
Modify Dept int(2) primary key;
OUTPUT:

Displaying Names and salaries of employees


working in West Zone
QUERY 21:
Select Name,salary from Employee
where Zone=’west’;
OUTPUT:
Displaying records of employees that have
salary above 35000;
QUERY 22:
Select * from Employee
where salary>35000;
OUTPUT:

Displaying records categorized on their


values of Zone column
QUERY 23:
Select Name, salary, Age from Employee
group by Zone;
OUTPUT:
Deleting records in Employee table that have
Age<28
QUERY 24:
Delete from Employee where Age<28;
OUTPUT:

Displaying the output arranged in ascending


order
QUERY 25:
Select * from Employee order by Name;
OUTPUT:
Displaying the output in descending order
QUERY 26:
Select * from Employee order by Name desc;
OUTPUT:

Displaying all the details after joining both the


tables
QUERY 27:
Select * from Employee,Department where
Employee.No=Department.HOD;
OUTPUT:
Deleting tables
QUERY 28:
Drop table Department;
OUTPUT:

**Thank You**

You might also like