Computers Science
Computers Science
1. Write a function that accept a list containing integers randomly. Accept any
number and display the index position at which the number is found in the list.
PROGRAM
def searchElement(marks):
maxrange =int(input("Enter Total Count of
numbers in the list: ")) flag=False
for i in range(0,
maxrange): marks.append (
int (input (" ? ") ) )
print(marks)
number =int(input("Enter number to be
searched ")) for i in range(0,
maxrange):
if marks[i]==number:
print(number,"found at index position ", I , " & " ,
"as list element no" , i+1 ) flag=True
if flag==False:
print( number, "not found in list")
# main program marks=[]
searchElement(marks)
""" To See the content of the text file STORY.txt line by line where the words
are separated by '#' & also count the words in the file."""
f=open("STORY.txt", "r") line=" "
WC=0 L=0
while line:
line=f.readline()
S=line.split()
for word in S:
WC=WC+1
print(word, end="#")
print()
L=L+1
print(" Total no of words=", WC) print("
Total no of lines=", L)
f.close()
OUTPUT:
The#Lion#and#the#Mouse#
A#lion#was#once#sleeping#in#the#jungle#when#a#mouse#started#running#up#and#down#his#body#just#
for#fun.#This#disturbed#the#lion’s#sleep,#and#he#woke#up#quite#angry.#
He#was#about#to#eat#the#mouse#when#the#mouse#desperately#requested#the#lion#to#set#him#free.#
“I#promise#you,#I#will#be#of#great#help#to#you#someday#if#you#save#me.”#The#lion#laughed#at#the
# mouse’s#confidence#and#let#him#go.#
One#day,#a#few#hunters#came#into#the#forest#and#took#the#lion#with#them.#They#tied#him#up#agai
nst#a#tree.#
The#lion#was#struggling#to#get#out#and#started#to#whimper.#Soon,#the#mouse#walked#past#and#noti
ced#the#lion#in#trouble.#Quickly,#he#ran#and#gnawed#on#the#ropes#to#set#the#lion#free.#Both#of#th
em#sped#off#into#the#jungle.#
Total no of words= 147 Total
no of lines= 6
9. Write a Python Program defining a function EMPDATA() to get employee data (EmpID,
Dept, EName, Age, Salary) from user and write onto a binary file “EDATA.dat”. The
program should be able to get data from the user and write onto the file as long as the
user wants.
Also, define another function SHOWEMPDATA() to open the existing binary file “EDATA.dat” and display the
stored employees records data ( EmpID, Dept, EName, Age, Salary) from it on screen.
PROGRAM CODING :
# To add data into a binary file “EDATA.dat”. Also read & show on the screen
import pickle def
EMPDATA():
fobj=open("EDATA.dat","wb") ans=''
print("Do you want to enter new data in the file“EDATA.dat”?")
ans=input("Press (y/N) : ")
while ans.upper()=='Y':
EmpID=input("Enter Employee's ID Number: ")
Dept=input("Enter Employee's Department :")
Ename=input("Enter Employee's Name :")
Age = int(input("Enter Employee's age : "))
Salary = float(input("Enter Employee's Salary : "))
rec=[EmpID, Dept ,Ename,Age, Salary]
pickle.dump(rec, fobj)
print("Record data entered.")
print("Do you want to continue data entry in the file?")
ans=input("Press (y/N) : ")
fobj.close()
def SHOWEMPDATA(): # create a function sp="
"
f=open("EDATA.dat","rb") try:
print(" Employees' data are given below: \n" )
while True:
Sp=pickle.load(f) # decoding the data
print(Sp)
except:
f.close()
StudentID Integer
Class String
StudentName String
Score Integer
PROGRAM CODING :
from csv import writerdef
Writedata():
#Create Header row First
f = open("student-result.csv","w",newline='\n')
dt = writer(f)
dt.writerow( [ 'Student_ID' , 'Class' , 'StudentName' , 'Score' ] )
f.close()
#Insert individual Student’s Data
f = open("student-result.csv","a",newline='\n')
while True:
st_id= int(input("Enter Student's ID:"))
Class = input("Enter Student's class:")
st_name = input("Enter Student's name:")
st_score = input("Enter score:")
dt = writer(f)
dt.writerow( [ st_id, Class, st_name, st_score ] )
ch=input("Want to insert More records?(y or Y):")
ch=ch.lower()
if ch !='y':
break
print("Record has been added.")
f.close()
if isPalindrome(string):
print("Yes, the string is a palindrome")
else:
print("No, the string is not a palindrome")
Output:
Enter string to check:madam
Yes, the string is a palindrome
>>>
= RESTART: G:/CN/Term -2 Practical Record programs/Stack program -3 palindrome checking.py
Enter string to check:School
No, the string is not a palindrome
>>>
Part B: MYSQL (5 sets of SQL queries using one/two tables)
Set [1] Queries (Database Fetching records)
Consider the following MOVIE table and write the SQL queries based on it.
Movie_ID MovieName Type ReleaseDate ProductionCost BusinessCost
Type
Action
Thriller
Drama
Biography
Romance
c) SELECT Movie_ID, MovieName, ProductionCost + BusinessCost “Total earning”
FROM MOVIE ; OUTPUT:
Movie_ID MovieName Total earning
M001 The Kashmir Files 2545000
M002 Attack 2370000
M003 Looop Lapeta 550000
M004 Badhai Do 788000
M005 Shabaash Mithu 1800000
M006 Gehraiyaan 1620000
M007 Pushpa 3000000
d) SELECT Movie_ID , MovieName , ProductionCost
FROM MOVIE
WHERE ProductionCost >150000 AND ProductionCost < 1000000 ; OUTPUT:
e) SELECT MovieName
FROM MOVIE OUTPUT:
WHERE Type =’Action’ OR Type =’Drama’ ; MovieName
The Kashmir Files
Attack
Badhai Do
Pushpa
f) SELECT MovieName OUTPUT:
FROM MOVIE
MovieName
WHERE MONTH(ReleaseDate)=2 ; Looop Lapeta
Badhai Do
Shabaash Mithu
Gehraiyaan
ii. Write a query to display the number 563.854741 rounding off to the next hundred.
Command: SELECT ROUND(563.854741,-2); OUTPUT: → ROUND(563.854741,-2)
600
SQRT(625)
v. Write a query to display square root of 625. 25
Command:
• CREATE DATABASE Sports ;
• USE Sports ;
ii. Create a table “TEAM” with following considerations:
a) It should have a column TeamID for storing an integer value between 1 to 9, which refers to unique
identification of a team.
b) Each TeamID should have its associated name (TeamName), which should be a string of length not
less than 10 characters.
c) Using table level constraint, make TeamID as the primary key.
Command:
• CREATE TABLE TEAM (TeamID int(1), TeamName varchar(10), primary key(teamid));
iii. Show the structure of the table TEAM using a SQL statement.
Command:
• DESCRIBE TEAM ;
OUTPUT
Field Type Null Key Default Extra
TeamID int(1) NO PRI NULL
TeamName varchar(10) YES NULL
iv. As per the preferences of the students four teams were formed as given below.
Insert these four rows in TEAM table:
Row 1: (1, Royal)
Row 2: (2, Toofan)
Row 3: (3, Dream)
Row 4: (4, Dhoom)
Command:
• INSERT INTO TEAM VALUES(1, ‘Royal’);
Output: Query OK, 1 row affected
• INSERT INTO TEAM VALUES(2, ‘Toofan’);
Output: Query OK, 1 row affected
• INSERT INTO TEAM VALUES(3, ‘Dream’);
Output: Query OK, 1 row affected
• INSERT INTO TEAM VALUES(4, ‘Dhoom’);
Output: Query OK, 1 row affected
v. Show the contents of the table TEAM using a DML statement.
• SELECT * FROM TEAM;
Output:
TeamID TeamName
1 Royal
2 Toofan
3 Dream
4 Dhoom
Answers:
• CREATE TABLE STUDENTS
-> (StudentID varchar(2) NOT NULL primary key,
-> StudentNM varchar(25),
-> DOB date,
-> Stream varchar(25),
-> Class varchar(25),
-> Grade varchar(25) CHACK(Grade IN (‘A’,‘B’,‘C’)),
-> Stipend int(4));
Output:
StudentID StudentNM DOB Stream Class Grade Stipend
iv) Display Name, class, stream of the students getting stipend not more than 600.
vi) List out the students name and stipend amount those names starts with letter ‘M’.
Answer: SELECT StudentNM, Class, Stipend FROM STUDENTS WHERE StudentNM LIKE ‘M%’;
OUTPUT:
StudentNM Class Stipend
vii) Display maximum and minimum stipend of the students from the Table STUDENTS.
Answer: SELECT MAX(Stipend) “Maximum Stipend”, MIN(Stipend) “Minimum Stipend” FROM STUDENTS;
viii) Display Total amount and average of stipend of all the students from the Table STUDENTS.
Answer: SELECT SUM(Stipend) “Total Stipend”, AVG(Stipend) “Average Stipend” FROM STUDENTS;
ix) Modify the stipend amount and increase it by 100, for all students who get stipend less than 600.
Answer: UPDATE STUDENTS SET Stipend = Stipend + 100 WHERE Stipend < 600;
x) Add one column percentMark of data type decimal to the table STUDENNTS.
Answer: SELECT * FROM STUDENTS WHERE Grade =’A’ OR Grade =’B’ ORDER BY StudentNM;
xii) Display the number of students are getting stipend more than 500 stream-wise in table STUDENTS.
Answer: SELECT Stream , COUNT(*) FROM STUDENTS GROUP BY Stream HAVING Stipend>500;