C12 LessonP PT1 2023
C12 LessonP PT1 2023
C12 LessonP PT1 2023
Lesson Plan
Topics to be covered:
— Revision of Python topics covered in Class XI
— Functions
— Introduction to files, types of files (Text file, Binary file, CSV file)
— Text file/ Binary Files/ CSV Files
A Learning Objectives
Student should be able to
Apply the concept of function.
Explain and use the concept of file handling.
Use basic data structure: Stacks
Explain basics of computer networks.
Use Database concepts, SQL along with connectivity between Python and SQL.
B Learning outcomes: After completion of the chapter the students will be able to:-
Write python programs implementing Problem solving and programming capability.
To acquire programming skills in core Python.
To acquire Object Oriented Skills in Python
To develop the ability to write database applications in Python
C Pre-requisite knowledge:
Computer Science- Class XI
D Introduction of the Chapter (in brief): Python is a widely used general-purpose, high level programming language. It was created
by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with an emphasis on
code readability, and its syntax allows programmers to express their concepts in fewer lines of code.
Python is a programming language that lets you work quickly and integrate systems more efficiently. There are two major Python
versions: Python 2 and Python 3. Both are quite different.
Topic: File — Introduction to files, types of files (Text file, Binary CA:
Handling in file, CSV file), relative and absolute paths 1. Class Assignment
Python (refer Annexure III–
W.S : 1.3) (Q1 to Q12)
— Text file/ Binary Files/ CSV Files : opening, open
modes (r, r+, w, w+, a, a+), closing, opening a
file using with clause, writing/appending data HA:
Duration: file using write() and write lines(), reading from 1. Home Assignment
03/04/2023 a text file using read(), readline() and (refer Annexure III –
To readlines(), seek and tell methods, manipulation H.A: 1.3) (Q1 to Q15)
07/04/2023 of data in a text file
ICT Usage
To Make Mind Map of theoretical Topics, so that they
can be retained by the students.
(ii)Learning schedule/ practices: - Worksheets/Assignments / Practices (oral & written) and other material to be used
for learning purposes. (Attach the relevant material of assignments, related worksheets, flashcards, silent reading of a particular portion
of the chapter assigned to a particular group of students followed with question / answer session, dramatization of the content of the
chapter, visit to some place related to the content of the chapter, arrange some guest lecturers in relation to the content of the chapter, if
required, framing of questions by the students related to the content of the chapter etc.)
A= (10:1000,20:2000,30:3000,40:4000, 50:5000}
print A.items()
print A.keys()
print A.values()
ii) a = 30, b = 40
print(a;b)
iv) x, y = 30, 7
z = (int) x/y
(ii) a=5
b=a
c=b–3
print(a,b,c)
(iv) a = 55
b=a+4
a = 30, b + 2
print(b, a)
(v) a, b = 10, 30
a, b, a = b, b + 3, a – 5
print( a, b)
(vi) a, b = 10 , 5
print(type(print(a+b)))
2. def add(i):
if(i*3%2==0):
i*=i
else:
i*=4
return i
a=add(10)
print(a)
3. import math
def area(r):
return math.pi*r*r
a=int(area(10))
print(a)
5. def div5(n):
if n%5==0:
return n*5
else:
return n+5
def output(m=5):
for i in range(0,m):
print(div5(i),’@’,end=” “)
print(‘\n’)
output(7)
output()
output(3)
Q7. Error-based questions working with functions, Find and correct the errors of code
1. def in(x,y):
x=x + y
print(x.y)
x*=y
print(x**y)
2. void get(x=10,y):
x=x+y
print(x,n,y)
5. def sum(*n):
total=0
for i in n:
total+=i
print(‘Sum=’, total)
sum()
sum(5)
sum(10,20,30)
Application Based Questions / HOTS
Q8. Write a function that takes amount in dollars and dollar to rupee conversion price and returns amount converted to rupees. Create a function in void
and non-void forms.
Q9. Write a function that receives two numbers and generates a random number from that range. Using this function, the main program should be able to
print three numbers randomly.
Q10. Write a program with a function that takes an integer and prints the number that follows after it. Call the function with these arguments
4, 6, 8, 2+1, 4-3*2, -3-2
Q1. What are the comments? What are the role comments in the program? How to write single-line comments and multi-line comments?
Q2. Explain the physical line structure of a program. Illustrate with an example.
Q3. Illustrate the flow of execution in the function call statement.
Q4. Write and explain the types of functions supported by python.
Q5. Write the ways of import module in the python program.
Q6. Differentiate between parameters and arguments.
Q7. What are the arguments supported by python? Explain each of them with a suitable example.
Q8. What is the local variable and global variable? Explain with an example.
Q9. What are the rules for combining three types of arguments in a Python function?
Q9. 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 40000 (both values inclusive).
Q10. Write a function countmy() in Python to read the text file "DATA.TXT" and count the number of times "my" occurs in the file.
For example, if the file "DATA.TXT" contains "This is my website. I have displayed my preferences in the CHOICE section." - the countmy() function should
display the output as: "my occurs 2 times".
Q11. Write a method/function DISPLAYWORDS() in python to read lines from a text file STORY.TXT, and display those words, which are less than 4
characters.
Q12. Write a method in Python to read lines from a text file DIARY.TXT and display those lines which start with the alphabet 'P'.
Q13. Consider the file “SarojiniPoem.txt”
Autumn Song
Like a joy on the heart of a sorrow, The sunset hangs on a cloud;
A golden storm of glittering sheaves, frail and fluttering leaves,
The wild wind blows in a cloud.
Hark to a voice that is calling
To my heart in the voice of the wind: My heart is weary and sad and alone,
For its dreams like the fluttering leaves have gone, And why should I stay behind?
-----------Sarojini Naidu
Q14. Write a program using Dictionary and Text files to store roman numbers and find their equivalents.
Q15. Observe the following code and answer the questions that follow:
File = open ("Mydata","a")
_______#Blank1
File.close()
Q2. Following code is written to update a record in a file opened with following code:
import pickle
fin = open("Stu.dat', 'rb')
try:
while True:
_____ = fin.tell() # Line 1: store file-pointer position before reading the record
stu = pickle.load(fin))
if stu['Marks'] in [92, 93, 94]:
stu[ 'Marks'] += 3 #changes made in the record
fin._____ (_____) # Line 2 :place the file-pointer at the exact location of the record
pickle.dump(stu, fin) # now write the updated record on the exact location
except:
:
Fill in the blanks in Lines 1 and 2 to complete the code.
Q3. Identify the error in the following code:
1. import pickle
2. data = ['one', 2, [3, 4, 5]]
3. with open('data2.dat', 'rb') as f:
4. pickle.dump(data, f)
PRACTICAL IMPLEMENTATION QUESTIONS
Q4. Write a program to read a text file and display the count of lowercase and uppercase letters in the file.
Q5. Your recipe uses some ingredients. Write a program to store the list of ingredients in a binary file.
Q6. Write a method in python to read lines from a text file INDIA.TXT, to find and display the occurrence of the word "India".
For example:- If the content of the file is
"India is the fastest growing economy.
India is looking for more investments around the globe.
The whole world is looking at India as a great market.
Most of the Indians can foresee the heights that India is
capable of reaching."
The output should be 4
Q7. Write a method in python to read lines from a text file MYNOTES.TXT, and display those lines, which are starting with an alphabet "K".
Q8. Considering the following definition of dictionary MULTIPLEX, write a method in python to search and display all the content in a pickled file
CINEMA.DAT, where MTYPE key of the dictionary is matching with the value "Comedy".
MULTIPLEX = {'MNO': _____, 'MNAME": _____, 'MTYPE': _____}
Q7. Write a program that reads a text file and then create a new file where each character‘s case is inverted.
Q8. Write code to print just the last line of a text file 'data.txt'.
Q6. Write a function remove_lowercase() that accepts two filenames, and copies all lines that do not start with a lowercase letter from the first file into the
second.
Q7. What is the output of following code ?
file ("e:\\poem.txt", "r").readline().split()
Recall that poem.txt has some leading and trailing whitespaces.
Q8. What is the output of following code ?
file("e: \\poem.txt", "r").readline()
Q9. What is the output of following code ?
fh = file ("poem.txt", "r")
size = len(fh.read())
print (fh.read (5))
Q10. Write a program to display all the records in a file along with line / record number.
Q11. What is the output produced by following code ?
obj = open("New.txt","w")
obj.write("A poem by Paramhansa Yogananda ")
obj.write("Better than Heaven or Arcadia")
obj.write("I love thee, O my India!")
obj.write("And thy love I shall give")
obj.write("To every brother nation that lives.")
obj.close()
obj1 = open("New.txt","r")
s1 = obj1.read(48)
print(s1)
obj1.close()
Q2. Considering the two files pl.txt and p2.txt created in previous question, what output will be produced by following code fragments ?
(a)
obj1 = open("p1.txt", "r")
s1 = obj1.readline()
s2= obj1.read(15)
print (s2)
obj1.close()
(b)
obj1 = open("p2.txt", "r")
s1 = obj1.readline()
s2 = obj1.read(15)
print(s2)
obj1.close()
Q3. Consider the file p2.txt created above. Now predict the output of following code that works with p2.txt. Explain the reason behind this output.
Fp1= open("p2.txt", "r")
print (fp1.readline(20))
s1= fp1.readline (30)
print(s1)
print (fp1.readline (25))
Q4. Read the code given below and answer the question:-
fh = open("main.txt", "W")
fh.write("Bye")
fh.close()
If the file contains “GOOD” before execution, what will be the contents of the file after execution of this code?
Q5. A given text file "data.txt" contains:
Line 1\n
\n
Line 3
Line 4
\n
Line 6
What would be the output of following code?
fh = open ("data.txt", "r")
1st = fh.readlines()
print (1st[0], end = '')
print (1st[2], end = "")
print (1st[5], end = '')
print (1st[1], end = '')
print (1st[4], end = '')
print (1st[3])
Q6. Write a program that copies a text file "source.txt" onto "target.txt" barring the lines starting with a "@" sign.
Q7. Consider the following statement. In which file mode is the file opened ? Justify your answer.
with open( 'poen.txt') as f:
Q8. What is pickling process? What is its need ?
Q9. What is unpickling?
Q10. What is the difference between a regular text file and a delimited text file ?
Q11. What is the significance of newline = ’’ argument in file open() function?
Q12. What does csv.writer object do?
Q13. What is the function of csv.reader object?
Q14. What will be stored in the file school.csv after the following code is executed?
data = [[ 'DRS Delhi', 'Esha', 'Badminton'], ['BTS Patna', 'Abhi', 'Tennis']]
import csv
with open('schoo;.csv', 'w', newline ="") as csvfile:
writer = csv.writer(csvfile, delimiter = ',')
writer.writerow(['School', 'Nickname', 'Sport'])
writer.writerows(data)
Q15. Write a program to remove all the lines that contain the character 'a' in this file and write other lines into another file.
Q16. Differentiate between the following:-
(i) f = open ('diary.txt', 'r')
(ii) f = open ('diary.txt", "w')
Following are topics which at least should be cover and student can increase many things depend on the complexity of the project.
Synopsis Topic :
— Introduction of Project
— Objective of the Project
— Scope of the Project
— Project Plan
— The Existing System
— Background of the Organization
— Current Problems
— Areas for Improvement
— Proposed System
— Hardware and Software Requirement
— Future Scope of the Project
— Conclusion
— Reference or Bibliography
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------