0% found this document useful (0 votes)
38 views43 pages

Moksh Practical File1

Uploaded by

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

Moksh Practical File1

Uploaded by

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

PRACTICAL FILE

NAME:MOKSH
CLASS: XII-A
ROLL NO: 21
SCHOOL: KENDRIYA VIDYALAYA SAINIK VIHAR
1. Write a single loop to display all the contents of a text file output.txt after
removing leading and trailing WHITESPACES

O/P:

2. Read the code given below and answer the questions


f1=open('main.txt','w')
f1.write('bye')
f1.close()

if the file contains 'GOOD' before execution, what will be the content of the file after
execution of the code.

Ans) BYE

3. Observe the following code and answer the follow


f1=open("mydata","a")
______#blank1
f1.close()

(i)what type of file is mydata.

- Text file

(ii) Fill in the blank1 with statement to write "abc" in the file "mydata"

- f1.write(“abc”)

4. A given text file data.txt contains :


Line1\n
\n
line3
Line 4
\n
line6

What would be the output of the following code?


f1=open('data.txt')
L=f1.readlines()
print(L[0])
- Line1
print(L[2])
- line3
print(L[5])
- line6
print(L[1])
- (blank line)
print(L[4])
- (blank line)
print(L[3])
- Line 4

5. In which of the following file modes the existing data of the file will not be lost?
i) rb
ii) w
iii) a+b
iv) wb+
v) r+
vi) ab
vii) w+b
viii)wb
ix) w+
Ans) rb, a+b, r+, ab

6. What would be the data types of variables in the following statements?


i) Data=f.read( ) → string
ii) Data=f.read(10) → string
iii) Data=f.readline() → string
iv)Data=f.readlines() → list of string

7 Suppose a file name test1.txt store alphabets in it then what is the output of the
following code
f1=open("test1.txt")
size=len(f1.read())
print(f1.read(5))

Ans)The output will be an empty string because the file pointer is already at the end
after the first f1.read().

8.Write a user-defined function in Python that displays the number of lines starting
with 'H' in the file para.txt.
INPUT:
OUTPUT:

CONTENT OF FILE:

9.Write a function countmy() in Python to read the text file DATA.TXT and count the
number of times "my" occurs in the file.

INPUT:

OUTPUT:

CONTENT OF THE FILE:


10.Write a method in python to read lines from a text file DIARY.TXT and display
those lines which start with the
alphabets P.

INPUT:

OUTPUT:

CONTENT OF FILE:

11.write a method in python to read lines from a text file MYNOTES.TXT and display
those lines which start with alphabets 'K'
INPUT:

OUTPUT:

CONTENT OF THE FILE:

12.write a program to display all the records in a file along with line/record number.

INPUT:
OUTPUT:

CONTENT OF THE FILE:

13.consider a binary file employee.dat containing details such as empno: ename:


salary (separator ':') write a python function to display details of those employees
who are earning between 20000 and 30000(both values inclusive)

14.Write a program that copies a text file "source.txt" onto "target.txt" barring the
lines starting with @ sign.
INPUT:

OUTPUT:

CONTENT OF SOURCE.TXT:

CONTENT OF TARGET.TXT:
15. A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].
i. Write a user defined function CreateFile() to input data for a record and add to
Book.dat .
ii. Write a function CountRec(Author) in Python which accepts the Author name as
parameter and count and return number of books by the given Author are
stored in the binary file “Book.dat

i)

INPUT:

OUTPUT:

CONTENT OF FILE:
ii)

INPUT:

OUTPUT:

CONTENT OF THE FILE:

16. A binary file “STUDENT.DAT” has structure [admission_number, Name,


Percentage]. Write a function countrec() in Python that would read contents of the file
“STUDENT.DAT” and display the details of those students whose percentage is above
75. Also display the number of students scoring above 75%.
INPUT:

17.Write a function in python to search and display details, whose destination is


“Cochin” from the binary file “Bus.Dat”. Assuming the binary file is containing the
following elements in the list:

i. Bus Number
ii. Bus route
iii. Bus Destination

INPUT:
OUTPUT:

CONTENT OF THE FILE:

18. Write a function addrec() in Python to add more new records at the bottom of a
binary file “STUDENT.dat”, assuming the binary file is containing the following
structure :

[Roll Number, Student Name]

INPUT:

OUPUT:

CONTENT OF THE FILE:


19.. Write a function searchprod( pc) in python to display the record of a particular
product from a file product.dat whose code is passed as an argument. Structure of
product contains the following elements [product code , product price]

INPUT:

OUTPUT:

CONTENT OF THE FILE:

20.Write a function routechange(route number) which takes the Route number as


parameter and modify the route name(Accept it from the user) of the passed route
number in a binary file “route.dat”.
INPUT:

OUTPUT:

CONTENT OF THE FILE:

21.Write a function countrec(sport name) in Python which accepts the name of sport
as parameter and count and displays the coach name of a sport which is passed as
argument from the binary file “sport.dat”. Structure of record in a file is given below
[sport name, coach name]

OUTPUT:

CONTENT OF THE FILE:


INPUT:

22.. A binary file “salary.DAT” has structure [employee id, employee name, salary].
Write a function countrec() in Python that would read contents of the file “salary.DAT”
and display the details of those employees whose salary is above 20000.
23.Amit is a monitor of class XII-A and he stored the record of all the students of his
class in a file named “class.dat”. Structure of the record is [roll number, name,
percentage]. His computer teacher has assigned the following duty to Amit.
Write a function remcount( ) to count the number of students who need remedial
class (student who scored less than 40 percent)

INPUT:

OUTPUT:

CONTENT OF THE FILE:


24.Write steps to print data from csv file in list object and support your answer with
an example.

1. Import csv module – import csv


2. Open the csv file in reading mode – f = open(“demo.csv”,”r”)
3. Use list object to store the data read from csv using reader – data =
csv.reader(f)
4. close the csv file – f.close()
5. print the data object – print(data)

25.How to print the following data for cust.csv in tabular form using python code?

S.no Customer name city amount

1 Dhaval Anand 1500

2 Anuj Ahmedabad 2400

3 Mohan Vadodara 1000

4 Sohan Surat 700

INPUT:

OUTPUT:
CONTENT OF CSV FILE:

26.Write code to insert multiple rows in the above csv file.


INPUT:
OUTPUT:

27.Write code to delete a row in a csv file.

INPUT:

OUTPUT:
CONTENT OF THE FILE:

28.Write a python program to search an element in a list and display the frequency of
elements present in the list and their location using Linear search by using a user
defined function. [List and search element should be entered by user]

INPUT:
OUTPUT:

29.Write a python program to search an element in a list and display the frequency of
elements present in list and their location using binary search by using a user defined
function. [List and search element should be entered by user]

INPUT:
OUTPUT:

30.Write a python program to pass a list to a function and double the odd values and
half even values of a list and display list elements after changing.
INPUT:

OUTPUT:

31.Write a Python program input n numbers in tuple and pass it to function to count
how many even and odd numbers are entered.

INPUT:
OUTPUT:

32.Write a Python program to function with key and value, and update value at that
key in the dictionary entered by the user.

INPUT:
OUTPUT:

33.Write a Python program to pass a string to a function and count how many vowels
present in the string.

INPUT:

OUTPUT:
34.Write a Python program to generator(Random Number) that generates random
numbers between 1 and 6 (simulates a dice) using a user defined function.

INPUT:

OUTPUT:

35.Write a python program to implement python mathematical functions.

INPUT:
OUTPUT:

36.Write a python program to implement python string functions.

INPUT:
OUTPUT:

37.Write a python program to read and display file content line by line with each word
separated by #.

INPUT:

OUTPUT:

CONTENT OF THE FILE:

38.Write a python program to remove all the lines that contain the character ‘a’ in a
file and write it to another file.

INPUT:

OUTPUT:

39.Write a python program to read characters from keyboard one by one, all lower
case letters gets stored inside a file “LOWER”, all uppercase letters gets stored inside
a file “UPPER”, and all other characters get stored inside “OTHERS”

INPUT:
OUTPUT:

40.Write a python program to create a binary file with name and roll number. Search
for a given roll number and display name, if not found display appropriate message

INPUT:
OUTPUT:

41. Write a python program to create a binary file with roll number, name and
marks, input a roll number and update the marks.

INPUT:
OUTPUT:

42. Write a python program to create a CSV file with empid, name and mobile
no. and search empid, update the record and display the records.
INPUT:
OUTPUT:
43.Write a Python program to create Lpush( ) and Lpop( ) functions to do push and
pop operations on a stack using a list e.g. take a student information and push and
pop the details.

INPUT:

OUTPUT:

44. Create a student table and insert data. Implement the following SQL commands
on the student table:

ALTER table to add new attributes / modify data type / drop attribute

UPDATE table to modify data ORDER By to display data in ascending /


descending order DELETE to remove tuple(s) GROUP BY and find the min,
max, sum, count and average

INPUT:-

use
studentdb;
Database
changed
CREATING
TABLE

create table
student (roll int
not null,

studentname varchar(30)
not null, class char(5) not
null,

section char(1) not null,


classstream char(20) not
null); desc student;

OUTPUT:

INSERTING VALUE

insert into student values

(1, "Akhil", "XII", "A", "Science"),

(2, "Satya", "XII", "C", "Science"),

(3, "Antony", "XII", "D", "Commerce"),

(4, "Vishal", "XII", "E", "Humanities"),

(5, "Deepak", "XII", "A", "Science"),

(6, "Brij", "XII", "B", "Science");

select * from student;

OUTPUT:

ADDING COLUMN IN TABLE USING ALTER COMMAND

alter table student


add(Substream char(20) not
null), (Percentage float not
null); desc student;

UPDATING VALUE

update student set Substream = "Computer Science"


where roll = 1; update student set Substream = "Biology"
where roll = 2;

update student set Substream = "Maths" where


roll = 3; update student set Substream = "NA"
where roll = 4;

update student set Substream = "Computer Science"


where roll = 5; update student set Substream =
"Computer Science" where roll

= 6; select * from student;

update student set Percentage = 81 where


roll = 6; update student set Percentage =
69 where roll = 6; update student set
Percentage = 92 where roll = 6;

update student set Percentage = 55 where


roll = 6; update student set Percentage =
85 where roll = 6; update student set
Percentage = 72 where roll = 6; select *
from student;
alter table
student add(

Adhaar_card bigint not null);

desc student;

alter table student


drop column
Adhaar_card;

desc student

ORDER BY COMMAND

select studentname,section from student order by Percentage;

AVERAGE COMMAND

select avg(Percentage) from student;


COUNT COMMAND

select count(*) from student;

MIN, MAX AND SUM COMMAND

select min(Percentage) from student;select max(Percentage) from student;

select sum(Percentage)/count(*) from student where substream = "Computer Science";

DELETE A TUPLE FROM TABLE

delete from student where classstream = "Science"; select * from student;

45.Integrate SQL with Python by importing the MySQL module record of employees
and display the record.

INPUT:

OUTPUT:

46.Integrate SQL with Python by importing the MySQL module to search an


employee using empno and if present in table display the record, if not display
appropriate method.

INPUT:
OUTPUT:

47.Integrate SQL with Python by importing the MySQL module to search a student
using rollno, update the record.

INPUT:
OUTPUT:

48.Integrate SQL with Python by importing the MySQL module to search a student
using rollno, delete the record.

INPUT:

OUTPUT:
49.Take a sample of ten phishing e-mails (or any text file) and find most commonly
occurring word(s)

INPUT:

OUTPUT:

You might also like