12th Report File Bhargavi
12th Report File Bhargavi
SESSION 2020-2021
COMPUTER SCIENCE
REPORT FILE
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:
Thereafter, the function in this module is used through importing functions from
File.py to this program named 'FileRun'
OUTPUT:
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:
cursor=mycon.cursor()
data=cursor.fetchall()
count=cursor.rowcount
if count==0:
return
for x in data:
print(x)
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:
QUERY 20:
Alter table Department
Modify Dept int(2) primary key;
OUTPUT:
**Thank You**