0% found this document useful (0 votes)
847 views29 pages

12th Computer Science EM EC Guide Sample Notes English Medium PDF Download

The document is a study guide for Computer Science, specifically designed for students preparing for board exams. It includes various chapters covering essential topics, tips for effective study, and a structured approach to learning. The guide aims to assist students of different learning levels with additional questions and resources for better understanding and exam preparation.

Uploaded by

JANANISRI T
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)
847 views29 pages

12th Computer Science EM EC Guide Sample Notes English Medium PDF Download

The document is a study guide for Computer Science, specifically designed for students preparing for board exams. It includes various chapters covering essential topics, tips for effective study, and a structured approach to learning. The guide aims to assist students of different learning levels with additional questions and resources for better understanding and exam preparation.

Uploaded by

JANANISRI T
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/ 29

wwww wwww

ww
www.Padasalai.Net
ww www.CBSEtips.in

eet t eet t
lalai .iN
.N l a
l i
a .iN
.N l a
l i
a .iN
.N
assaa aa a
dd s
a aa
s
aa a
dd s
a aa
s
ww P
.. P ww P
..P
wwww wwww
et
e t eet t
lalai.iN
.N l a
l i
a .iN
.N l a
l i
a .iN
.N
assaa aasaa
s as
asaa

et
PPaadd P Padd
a
ww.. w
w ..
wwww wwww

.N
ai
eet t eet t
l a
l i
a .iN
.N al l a
l i
a .iN
.N l a
l i
a .iN
.N
assaa a s
a aa
s a s
a aa
s
as
P Paadd P Paadd
w
w .. ww..
wwww wwww
ad

t t
.P

ee t ee t
l a
l i
a .iN
.N l a
l i
a.iN
.N l a
l i
a.iN
.N
assaa aa aa
w

a
dd s
a s a
dd s
a s
P
.. Paa P
.. Paa
w

w
w ww
wwww wwww
w

eet t e t
e t
l a
l i
a .iN
.N lal i
a.iN
.N l a
l i
a .iN
.N
assaa 94433 81701,P a a aassaa
dd69810, 90474 74696 aa a
dd assaa
w w
81110 94696,
P
. .89400 02320, 89400 02321
94422
ww P P
. . 250/-
wwww wwww
eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww
www.CBSEtips.in

eet t eet t
lalai .iN
.N l a
l i
a .iN
.Nby
Published
l a
l i
a .iN
.N
assaa aa a
dd s
a aa
s
aa a
dd s
a aa
s
ww P
.. P ww P
..P
wwww wwww
et
e t eet t
lalai.iN
.N l a
l i
a .iN
.N l a
l i
a .iN
.N
assaa aasaa
s as
asaa

et
PPaadd P Padd
a
ww.. w
w ..
wwww wwww

.N
ai
eet t eet t
l a
l i
a .iN
.N al l a
l i
a .iN
.N l a
l i
a .iN
.N
assaa a s
a aa
s a s
a aa
s
as
P Paadd P Paadd
w
w .. ww..
wwww wwww
ad

Author:
t t
.P

ee t ee t
l a
l i
a .iN
.N l a
l a.iN
.N
P. Simon Navis M.Sc(CS)., B.Ed., M.Phil., MBA.,
i l a
l i
a.iN
.N
assaa aa aa
w

a
dd s
a s a
dd s
a s
P
.. Paa P
.. Paa
w

w
w ww
wwww wwww
w

eet t eet t
l a
l i
a .iN
.N lal i
a.iN
.N l a
l i
a .iN
.N
assaa aa a
dd s
a aa
s
add
a aassaa
ww P
..P ww P
.. P
wwww wwww
eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

eet t eet t
lalai .iN
.N l a
l ai .iN
.N l a
l i
a .iN
.N
assaa aa aas aa
s
dd PREFACE aa a
dd s
a aa
s
w
w P
.. P ww P
..P
wwwwComputer Science “ is in your hands. wwww
  “Loyola
   Students with average IQ always struggle to cope up studies. They
always seek for the best, sources to learn and score high marks.
eet t ee t t
lalai.iN
.N   l a
l a N N in the exams has changed lalai.iN
The pattern of the question beingi.iasked
. . N
s
a a
sa a as
dramatically and the difficulty aa
level
s has also increased considerably. To saa
aas

et
succeed in board exams
PP a ad d
and to actualize your dream, you are aadd
required
P P
ww . .
to prepare strategically and study in a focused manner. ww . .
w w w w

.N
  This wwbook gives important tips which covers thew w
entire chapter.
  Loyola serves the above cited purpose in perfect manner.

ai
➢t tSpecially designed for coaching students of t t
different levels.
i .iN ee
.N (Slow learners, average and Topper
al i .iN N ee
.students) i .iN
.N
l a
l a l a
l a l a
l a
assaa a s
a aagiven for toppers
s aassaa
as
P Paadd
➢ Lot of additional questions are
P Paadd
w w . . Science” is prepared with due carewon
➢ The “EC Computer w .the
. lines
w w
wwGovt. Examination Valuation, the easy method w w
ww of studying,
ad

of the
the lesson and the perfect way of answering the questions.
➢ The answers are well prepared, briefly and easily for the students to
t t
.P

ee t ee t
l a
l i
a .iN
.N l a
l a.iN
.N
study without any difficulty and stress.
i l a
l i
a.iN
.N
assaa aa aa
w

➢ Simplified text matter


aa ss
dofdtextbook. a
dd s
a s
P
.. Pa
➢ Focused on coveragea P
.. Paa
w

w w w
w
www
➢ MCQ’s wframed based on new pattern. wwww
are
w

➢ Included PTA questions and Govt. question papers with their key.

e t
➢ 
e tComprehensive questions are designed tfor
ee t average and above

l a
l i
a .iN a
l i
a.iN
.N average students based on keylpoints.
.N l a
l i
a .iN
.N
assaa a dd
a a
Wish
assaaAll the Best
you
aaddaassaa
ww P
..P ww P
.. P
wwww wwww Loyola Publication

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

eet t eet t
a ai .iN
.N a i
a .iN
.N a i
a .iN
.N
l
assaa
l
a s
a aa CONTENTS aassaa
s l l l l
P Paadd PPaadd
ww.. ww..
wwww wwww
et
e t e et t
lalai.iN
.N UNIT No. Chapter l a
l i
a .iN
.NTitle Page.No
l a
l i
a .iN
.N
assaa aassaa as
asaa

et
1 Function 5
PPa dd Data Abstraction
a P P add
a
UNIT- I w
w .. 2
w
w ..13

wwww 34 Scoping ww 21

.N
Algorithmic Strategies
w
w 32

ai
5 Python -Variables and Operators 44
eet t Control Structurese et t
l a
l i
a .iN
.N UNIT- II al 6
l a
l i
a .
i N
. N 53
l a
l i
a .iN
.N
assaa aa aa
7 Python functions 65
aas s a s
a s
as
8 d d Strings and String manipulations 77 d d
P Pa a P P
. . 9 Lists, Tuples, Sets and Dictionary ww. .87aa
w
w
wwww 10 Python Classes and objects wwww 105
UNIT- III
ad

11 Database Concepts 114

t t
.P

ee t UNIT- IV 12
e e t
Structured Query Language (SQL) 130

l a
l i
a .iN
.N 13
l l a .iN
Python andiCSV
a .Nfiles 151
l a
l i
a.iN
.N
assaa aa C++ programs in Python. 165 aassaa
w

14
aass
Importing
dd Data manipulation through SQL dd
P
.. Pa a P
.. Paa
w

15 177
UNIT- V
w
w w
w
wwww 16 chart, pie chart and bar chart wwww 193
Data visualization using pyplot: line
w

Parent Teacher Association - Model Question Papers 205

eet t e
GOVT. EXAM - MAY 2022et t 217

l a
l i
a .iN
.N lal i
a.iN
.N l a
l i
a .iN
.N
assaa aa 2023 aa
GOVT. SUPPLEMENTARY EXAM - AUGUST 2022 219

dda
GOVT. EXAMas
-
s
MARCH 222 ddaass
P
..Pa a P
.. Paa
ww w w
wwww wwww
eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

e et t t t
CHAPTER - 1
e e
lalai .N I
.iN
Unit
l a
l i
FUNCTION
a .iN
.N l a
l i
a .iN
.N
assaa aa a
dd s
a aa
s
aa a
dd s
a aa
s
ww P P
. . FUNCTIONS - An Overview ww. . P
P
ww ww Object:
w
w ww
Expression of Algorithm:
• A lgorithms are expressed using statements An object is an instance created from the
of a programming language class.
et
e t eet t Interface:
lalai.iN
.N
Subroutine:
l a
l i
a .iN
.N
• Subroutines are small sections of code that
l a i
a .iN
.N
• An interface is a set of action that an object
l
assaa a saa
s
are used to perform a particular task that
a as
as
can do.aa

et
a
can be used repeatedly.
PP add add
a
• Interface just defines what an object can do,
P P
Function :
ww.. w
w .. but won’t actually do it.

wwww wwww

.N
• A function is a unit of code that is often Implementation:
defined within a greater code structure. • Implementation carries out the instructions
• A function contains a set of code that works defined in the interface.

ai
on many kinds of inputs and produces a Pure functions:
eet t
concrete output. eet t • Pure functions are functions which will

l a
l i
a .iN
.N
Definition: al l a
l i
a .iN
.N l a i .iN
.N
give exact result when the same arguments
l a
assaa s aa
s
• Definitions are distinct syntactic blocks.
a a a s
a aa
s
are passed.
as
Parameters:
P Paadd P Paadd
Impure function:

w
w ..
• Parameters are the variables in a function
ww..
• The functions which cause side effects to
definition.
wwww wwww the arguments passed are called Impure
ad

Argument: function.
• Arguments are the values which are passed Recursive function:
to a function definition through the function • A function definition which calls itself is
t t
.P

ee t
definition.
e e tcalled recursive function.

l a
l i
a .iN
.N l a
l i
a.iN
.N l a
l i
a.iN
.N
assaa aa aa
w

PART - I
aas s
TEXT
dd PART - I
BOOK EVALUATION
ddaass
P P aa
. . Choose the best answer (1 Mark) ..P Pa a
w

w w w
w
The small wwwwof code that are used to perform a particularwwtask
wwis called
w

1. sections Mar 2023


a) Subroutines b) Files c) Pseudo code d) Modules
Ans : a) Subroutines
e et t e et t
l a
l i
a .iN
2.
.N a) Subroutines b) Function
lal ai.iN
.N c) Files
l a
l i
a .iN
Which of the following is a unit of code that is often defined within a greater code structure?
.N
d) Modules Aug 2022

assaa 3. Which of the following is aadistinct


a aas aa
s
dd syntactic block? a dd
a a as saa
Ans : b) Function

a) Subroutines w
w P P
.b).Function c) Definition d) w
w P
.. P
Modules
May 2022

wwww wwww Ans : c) Definition


5

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

e et t e et t
lalai .iN
4.
.N a) Subroutines b) Function
l a
l ai .iN
The variables in a function definition are called as
.N
c) Definition d) Parameters
l a
l i
a .iN
. N
s
a a
sa a as sa a aas saa
Ans : d) Parameters

aa
5. The values which are passed
PP d
tod
a function definition are called
P P a add
a) Arguments
w w..
b) Subroutines c) Function d) . .: a) Arguments
Definition
ww
www
6. Which of the
w wwww in the function
following are mandatory to write the type annotations
Ans

definition
a) { } b) ( ) c) [ ] d) < >
e et t eet t Ans : b) ( )

lalai.7.iN
.NWhich l a
of the following defines what an object
l ai .
iN. Ndo?
can
l a
l i
a .
i N
. N
s
a a
sa a) Operating System b) Compiler
aa s a
s a c) Interface d) Interpreter
a as saa

et
P Pa a ddout the instructions defined in the interface?PPaadd
Ans : c) Interface
8. Which of the following
ww.b).Compiler
carries
ww ..
wwww www w

.N
a) Operating System c) Implementation d) Interpreter
Ans : c) Implementation
9. The functions which will give exact result when same arguments are passed are called

ai
March 2020
e t t t t
a) Impure functions b) Partial Functions c) Dynamic Functions d) Pure functions
e ee
l a
l i
a .10.iN
.NThe functions which cause side effects tolathe
al l i
a .iN
.N passed are called
Ans : d) Pure functions
l a
l i
a .iN
.N
assaa a) Impure functions b) Partial aa c) Dynamic Functions d) Pure functionsaassaa
arguments
a as s
as
PP a add
Functions
P Paa
Ans : a) Impure dfunctions
d
w w .. w w ..
wwww Answer the following questions (2 Marks)
PART - II
wwww
ad

1. What is a subroutine? PTA - 1


• Subroutines are the basic building blocks of computer programs.
t t
.P

ee t ee t
• Subroutines are small sections of code that are used to perform a particular task that can

l a
l i
a .iN
.N be used repeatedly.
l a
l i
a.iN
.N l a
l i
a.iN
.N
assaa 2. •Define aa language. aa
• In Programming languages, these subroutines are called Functions.
w

Function with respect to a a


dthat ss dd
d is often defined within a greater code structure.
Programming aas s
P P aa
A function is a unit of code
.on. many kinds of inputs like variants, expressions P Pa a
. .and produces a
w

• A function works
ww ww ww w w
ww
concrete output.
w w
w

3. Write the inference you get from X:=(78).


• Definitions are distinct syntactic blocks.
• Definitions can have expressions nested inside them, and vice-versa.
e et t e e t t
• In X:=(78),(78) is a function definition which binds the value 78 to the name ‘X’.

l a
l i
a .4.iN
.NDifferentiate interface and implementation.
lal i.iN
a .N PTA - 3 a
l l i
a .iN
.N
assaa Interface
aa a s
a aa
s Implementation
a add a as
dd can do, Implementation carries out thePPinstructionssaa
Interface defines what
w w P
.it.P
an object
w
w ..
wwww
but won’t actually do defined in the
wwww
interface

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iN definition
l a
l i
a .iN
.N l a
l i
a .iN
5. Which of the following is a normal function definition and which is recursive function
.N .N
assaa i) let sum xy:
return x +y
aa a
dd s
a aa
s
aa a
dd s
a aa
s
ii) let disp: w P
.. P
Recursive Function
w ww P
..P
wwww
print ‘welcome’
Normal Function
wwww
iii) let rec sum num:
if (num!=0) then return num + sum (num-1)
et
e t
else
eet t
lalai.iN
.N
return num
l a
l i
a .iN
.N l a
l i
a .iN
.N
assaa aa - III aa
Recursive Function
aa ss as
as

et
P P a add PART
P
P add
a
ww . . the following questions (3 Marks) ww. .
Answer
ww wwww

.N
1. ww
Mention the characteristics of Interface. Mar 2023
• The class template specifies the interfaces to enable an object to be created and operated

ai
properly.

e t t e t t
• An object’s attributes and behaviour is controlled by sending functions to the object.
e e
l a
l i
a .2.iN
.NWhy strlen is called pure function? lalai.iN
al .N l a
l i
a .iN
.N
assaa • Pure functions are functions which
a ass awill
a give exact result when the same argumentss
a a saa
are
as
passed.
P Pa adbecause
d the function takes one variable as aPparameter,
Pa add and
w w . .length.
• strlen() is a pure function
ww ..
wwwwreads external memory but does not changeww
accesses it to find its ww
ad

• This function it, and the value returned


derives from the external memory accessed.

t t
.P

3.
ee t ee t
What is the side effect of impure function. Give example.

l a
l i
a .iN
.N world. l a
l i
a.iN
.N l a
l i
a.iN
.N
• Impure function has side effects when it has observable interaction with the outside

assaa aa aa
w

a s
a s a s
a s
• The variables used inside the function may cause side effects through the functions which
dd dd
P
.. Paa P
.. Paa
are not passed with any arguments. In such cases the function is called impure function.
w

w
w ww
• When a function depends on variables or functions outside of its definition block, you
wwww wwww
can never be sure that the function will behave the same every time it’s called.
w

• For example, the mathematical function random() will give different outputs for the same
function call.
let Random number
eet t let a: = random () eet t
l a
l i
a .iN
.N if a > 10 then
lal i
a.iN
.N l a
l i
a .iN
.N
assaa return : a
else
aa a
dd s
a aa
s
add
a aassaa
return: 10
ww P
..P ww P
.. P
wwww wwww
Here the function random is impure as it is not sure what will be the result when
we call the function.
7

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t ee t t
lalai .iN
.N
4. Differentiate pure and impure function.
l a
l i
a .
i N
. N March 2020 PTA - 6
l a
l i
a .iN
.N
assaa aa The return value of the impure functions aa
Pure Function Impure Function
aa
dpure ss dd aass
d functions does not solely depend onPitsPaarguments
P P aa
.its. arguments passed.
The return value of
 the
.. a
solely dependsw w
1

wwww w
on
passed..
wwww w
ure functions with the same set of 
P Impure functions with the same set of
2
arguments always return same values. arguments may return different values.
3 tThey
t t t
i.iN ee do not have any side effects.
.N4 They do not modify the arguments which i .TiN e
They have
e side effects.
.heyNmay modify the arguments which are lalai.iN
.N
lala l a
l a
assaa are passed to them
a a s saa passed to them as
asaa

et
5 Example: strlen(),sqrt()a
PP add Example: random(),date()
P Padd
a
ww . . w w. .
ww - IV wwww the variable ‘b’

.N
value which is passed to
wwPART
Answer the following questions (5 Marks)
is the argument.
• The precondition (requires) and post

ai
1. What are called Parameters and write a condition (returns) of the function is

eet t
note on PTA - 2 May 2022 e et t
given.

l a
l i
a .iN
.N al
i) Parameter without Type
l a
l i
a .iN
.N• Note we have not mentioned any types
(data types). l a
l i
a .iN
.N
assaa Parameters : ssaa s aa
s
ii) Parameter with Type
a a a
• Some language computer solves this
a
as
PPaa dd in a PPaadd
type (data type) inference problem
• Parameters are the
w w.. variables
ww ..
algorithmically, but some require the
ww
function definition.
w w w w w w
type to be mentioned.
ad

Arguments: ii). Parameter with Type:


• Arguments are the values which are Now let us write the same function
passed to a function definition. definition with types for some reason:
t t
.P

ee t e e t
(requires: b > 0 )

l a
l i
a .iN
.N Parameters passing are of two types
namely
l a
l i
a.iN
.N l a
l
(returns: a to the power of b ) i
a.iN
.N
assaa aa aa
w

1. Parameter Without Type


ddaass aas s
let rec pow (a: int) (b: int) : int : =
dd
2. Parameter With Type
P Pa
.Type:
. a P
if b=0 then 1
.. Pa a
w

ww
i) Parameter without ww
else a * pow b (a-1)
wwanwwexample of a function • When wewwrite wwwthe type annotations
w

Let us see
definition. for ‘a’ and ‘b’ the parentheses are
mandatory.
(requires: b > =0 )
eet t t t
• There are times we may want to
ee
l a
l i
a .iN
.N (returns: a to the power of b)
let rec pow a b:=
l a
l i
a.iN
.N
explicitly write down types.
l a
l i
a .iN
• This useful on times when you get.N
assaa

if b=0 then 1
else a*pow a(b-1)a a a
dd assaa
add
a
doesn’t make sense. aassaa
a type error from the compiler that

• In the abovew w PP
. . definition
function
P
.. P
• Explicitly annotating the types can help
ww
variablew
w ww
‘b’ is the parameter and the wwww
with debugging such an error message.

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iN
2.
.N Identify in the following program

l a
l i
a .iN
.N
PTA - 5
l a i
a .iN
• When a function depends on variables
.N
or functions outside of its definition
l
assaa 
let rec gcd a b :=
a a
dd s
a aa
s
if b <> 0 then gcd b (a mod b)
a aa a
dd s
a aa
s
block, we can never be sure that the
function will behave the same every
P
.. P
else return a
w
w ww P
..P time it’s called.

wwww gcd
i) Name of the function
wwww • For example, the mathematical
functions random ( ) will give different
outputs for the same function call.
ii) Identify the statement which tells it is let Random number

N e e t ta recursive function
eet t let a := random()

lalai i.N let rec gcd a b:=


. l a
l i
a .iN
.N
l a
l a .iN
.N
if a > 10 then
i
return: a
assaa a“rec” a as
keyword tells the compiler ita
s a
is
as
asaa else

et
recursive function ad d add
PP a
. . variable P
..
P a return: 10
ww
iii) Name of the argument
w w
• Here the function Random is impure
w
bw wwww

.N
as it is not sure what will be the result
a w w
and
when we call the function.
iv) Statement which invoke the function

ai
4. Explain with an example interface and
recursively
implementation.
eet t
gcd b (a modb) eet t
l a
l i
a .iN
.N al
v) Statement which terminates the
l a
l i
a .iN
.N Interface:
l a
l i
a .iN
.N
assaa aa aa
• An interface is a set of action that an
recursion
a s
a s a s
a s
object can do.
as
return a
P Paadd P Paadd
• In Object Oriented Programming
ww .Pure
. ww..
language, an Interface is a description
3.
functions. ww ww
Explain with example and impure
w
w ww
ad

of all functions that a class must have in


Pure functions: order to be a new interface.
• Pure functions are functions which • The purpose of interfaces is to allow
t t
.P

the computer to enforce the properties


ee t
will give exact result when the same
ee t
l a
l i
a .iN
.Narguments are passed.
l a
l i
a.iN
• For example, the mathematical function.N l a
l a.iN
.N
of the class of TYPE T (whatever the
i
interface is) must have functions called

assaa aa aa
w

sin (0) always results 0.


a
dd s
a s a
dd s
a s
X, Y, Z, etc.
Let us see an example.
P
.. Paa P
.. Paa• A class declaration combines the
w

w
w
let square x ww external interface (its local state) with

wwww
return: x * x
wwww an implementation of that interface (the
w

• The above function square is a pure code that carries out the behaviour).
function because it will not give • An object is an instance created from
different results for the same input. the class.
eet t eet t • The interface defines an object’s

l a
l i
a .iN
.N Impure functions:
lal i
a.iN
.N
• The variables used inside the function l a
l i
a .iN
.N
visibility to the outside world.

assaa a s aa
s
may cause side effects though the
aadd a
functions which are not passed with any
add
a assaa
• In object oriented programs classes
a are the interface and how the object
P
..P
arguments. In such cases the function is
ww ww P
.. P is processed and executed is the

wwww
called impure function.
wwww implementation.

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iN
.N Characteristics of interface
l a
• The class template specifies the
l i
a .iN
.N l a i
a .iN
• The person who drives the car doesn’t
.N
care about the internal working.
l
assaa a dd s
a aa
s
interfaces to enable an object to be
a
created and operated properly.
a aa a
dd s
a aa
s
• To increase the speed of the car he just
presses the accelerator to get the desired
P
.. P
• An object’s attributes and behaviour is
ww P
..P
behaviour.
ww
object. wwww
controlled by sending functions to the
wwww
• 
Here the accelerator is the interface
between the driver (the calling /
Interface: Vs Implementation: invoking object) and the engine
Interface Implementation ( the called object).
e et t eet t
• In this case, the function call would be

lalai.iN
.Nwhat an object carries out thelalai.iN
I
 nterface defines I m p l e m e n t a t i o n
.N i .iN
.N
Speed (70): This is the interface.
l a
l a
assaa aa aa
• Internally, the engine of the car is doing
can do, but won’t instructionsa a s s as
as

et
defined
actually do it in the a
PP add
interface
all the things.
P Padd
a
w w .. w
w ..
• It’s where fuel, air, pressure, and

wwww wwww
electricity come together to create the

.N
Example :
power to move the vehicle.
ENGINE All of these actions are separated from the

ai
driver, who just wants to go faster. Thus we
eet t eet t
l a
l i
a .iN
.N getspeed al l a
l i
a .iN
.N l a
l i
a .iN
separate interface from implementation.
.N
assaa a s
a aa
s a s
a aa
s
as
No
P Paadd P Paadd
required
w
w .. Pull Fuel
ww..
wwww wwww
speed
ad

Yes

Return

t t
.P

ee t ee t
l a
l i
a .iN
.N l a
l ai .
i N
. N
PART II - ADDITIONAL QUESTIONS
l a
l i
a.iN.N
assaa I. Choose thea a aa
w

The function definition is a d daa ss best answer (1 Mark)


d a
d s
a s
1.
P P
.b).let a
introduced by the keyword
P P a a
. . Ans : b) let
w

a) def c) rec d) requires


w w w w
2. www
The recursive w is defined using the keyword wwww
function
w

a) let b) requires c) name d) let rec


Ans : d) let rec
3.
eA
et t
 function definition which calls itself is called
eet t PTA - 1

l a
l i
a .iN
.N a) user defined function
c) built-in function lal ai .iN
.N
b) recursive function
d) derived function l a
l i
a .iN
.N
assaa  aadd a ass aa a ssaa
Ans : b) recursive function
add
a a
4.
a) datatype ww P
All functions are________ definitions.
P
.b).dynamic c) return d) w
w ..
staticPP

wwww wwww Ans : d) static
10

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t e et t
lalai .iN
5.
.N strlen() is an example of ________function. 5.
a) pure b) impure
l a
l ai .iN
Write notes on Interface.
.N
• An interface is a set of action that an
l a
l i
a .iN
.N
assaa a) user defined d) recursive

a a a as
dd doessaa
Ans : a) pure
object can do.
a dd a as s
• Interface just defines what an object can
a
aa
6. Evaluation of__________
not cause any w w w P P
. . to its output?
functions
w w P
.. P
do, but won’t actually do it

a) Impure w w w b) built-in
side effects
ww ww
6. Define Implementation.
c) Recursive d) pure Implementation carries out the instructions
 Ans : d) pure defined in the interface

e
7.t
e trandom() is an example of ________ 7.
eet t
Write notes on Pure functions.

lalai.iN
.N function.
a) user defined b) impure l a
l i
a .iN
.N • Pure functions are functions which
l a
l i
a .iN
.N
assaa aa aa
will give exact result when the same
c) pure
a a
d) recursivess a a ss

et
arguments are passed.
P Pa dd
a
Ans : b) impure
a a dd
• Example: : strlen(),sqrt()
P P
8. An _______ is anw
w . . created from 8. Write notes on Impurewwfunction.
instance . .  PTA - 5
www b) object www w

.N
a) Interfacew
the class.
• The functions which cause side effects
c) member d) function to the arguments passed are called

ai
Ans : b) object Impure function.
9.eet t
I n object oriented programs _______ are ee t t• Example: random(), date()

l a
l i
a .iN
.N the interface. al l a
l i
a .iN N is Recursive function?
.What9.
l a
l i
a .iN
.N
assaa a a aa
a) classes b) object
a s
a s a as
A function definition which calls itself
s
is
as
c) function
P Paadd
d) implementation called
Ans : a)classes
Recursive function.
P Paadd
w
w .. 10. w w
Differentiate parameters .and
. arguments.
ww
10. ____________ bind values to names.
ww w w
ww Arguments
ad

a) Modules b) Subroutines Parameters


c) Definition d) Function
Parameters are Arguments are the
Ans : c) Definition
the variables in a values which are
II.t t
.P

e e t
Answer the following questions
e e t
function definition passed to a function

l a
l i
a .1.iN
.NHow Algorithms are expressed?
(2 and 3 Marks)
l a
l i
a.iN
.N definition.
l a
l i
a.iN
.N
assaa Algorithms are expressed using aa 11. What are the roles of classesaasandsaa
w

d da as s doriented
d
..PP a a implementation in Object
PP a a
. . classes are the
w

statements of a programming language


2. What is meantw
w w
wDefinition?
programs?
In object orientedww ww
programs
w w by
ww
w

Definitions are distinct syntactic blocks. interface and how the object is processed
and executed is the implementation.
3. Define Parameters.

N Neet t
Parameters are the variables in a function
N N e t t
12. Define Object:
An eobject is an instance created from the
NN
l a
l i
a .
i . definition.
lal ai.i.class.
l a
l i
a .
i .
s
a a
sa 4. Define Argument.
Arguments are the values d aa s aarea
s aas saa
P P a a d
which
P Pa ad d
w w. . through the
passed to a function definition
ww ..
wwww
function definition.
wwww
11

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

N e et t 3. In e
N e t t
lalai .
i N
.1. Explain the syntax of function i
III. Answer the following questions (5 Marks)
l a
l a .
i .of chameleons. Whenever two different lalai.iN
N the island there are different types
.N
assaa definitions
• The syntax to defineP a a a s
a saa color chameleons meet they both change
dd is close their colors to the third color.d
aa aa
d inss
Suppose
aa
w w . P
.usage.
functions two types of chameleons
w w P
.. Pare equal

ww wwis introduced by the Construct anwwalgorithm


to the mathematical
• The definition
number. ww that arranges
keyword let, followed by the name of meetings between these two types so that
the function and its arguments; then they change their color to the third type.
et
e t the formula that computes the image of
eet t In the end, all should display the same

lalai.iN
.N l a
l a .iN
.N
the argument is written after an = sign.
i color.
l a
l i
a .iN
.N
assaa
• If you want to define a recursive
aasaa
s
function: use “let rec” instead of “let” . as
asaa
let ree monoehromatize a b c:=

et
PPaadd
Syntax for function definitions:
P

Padd
a if a > 0 then

ww..
 let rec fn a1 a2 ... an := k w
w .. a, b, c:= a - 1, b - 1, c + 2

wwww wwww
else

.N
 Here the ‘fn’ is a variable indicating a:= 0 b:= 0 c:= a + b + c
an identifier being used as a return c

ai
function name.
HANDS ON PRACTICE
eet t  The names ‘a1’ to ‘an’ are variables
e t
e t
l a
l i
a .iN
.N indicating the identifiers used as 1.
parameters. al l a
l i
a .
i N N
Write
. the algorithmic function definition
to find the minimum among 3 numbers. la l i
a .iN
.N
assaa  
a assaa 
The keyword ‘rec’ is required if
a
PTA
as 4a
-s a
as
aa dd
‘fn’ is to be a recursive function;
P P let min 3abc:= P P aadd
ww..
otherwise it may be omitted.

if a < b thenww..
w w wwan syntax for function if a <ww

wwa else c
ad

2. Write a short note c then


types. else
• The syntax for function types if b < c then b else c
t t
.P

ee t x→→y
ee t 2. Write the algorithmic recursive function

l a
l i
a .iN
.N xl →→x2→→y
l a
l i
a.iN
.N l a
l i
a.iN
.N
definition to find the sum of ‘n ‘ natural

assaa aa aa
x1 →→ .... →→xn→→y
w

a
dd s
a s
• The ‘x’ and ‘y’ are variables indicating
numbers.
a
dd s
a s
types. P
.. Paa
P
.. Paa let rec sum num:
w

If(num!=0) then
w
w ww
wwww wwww
• The type x →→ y is the type of a function return num+sum(num-1)
w

that gets an input of type ‘x’ and returns else


an output of type ‘y’ where as xl→→ x2 return num
-→→y is a type of a function that takes
eet t eet t
two inputs, the first input is of type ‹xl› ☺☺☺☺☺

l a
l i
a .iN
.N lal i
a.iN
.N
and the second input of type ‘xl’ , and
l a
l i
a .iN
.N
assaa dd s
a aa
s
returns an output of type ‹y’ .
a
• Likewise x1 →→...→→xn→→y has
aa add
a aassaa
P
..P
type ‘x’ as input of n arguments and ‘y’
ww ww P
.. P
wwww
type as output.
wwww
12

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

e et t eet t
CHAPTER - 13

lalai .N IV
.iN
Unit
PYTHONl a
l i
a .iN
AND.N CSV FILES l a
l i
a .iN
.N
assaa aaddaassaa
aa a
dd s
a aa
s
ww P
.. P ww P
..P
wwww wwww
PYTHON AND CSV FILES- An Overview
CSV file:
• A CSV file is a human readable text file where each line has a number of fields, separated
by commas or some other delimiter
et
e t
Two ways to read a CSV file: eet t
lalai.iN
.N l a
l i
a .iN
.N l a i
a .iN
.N
• The two ways to read a CSV file are using csv.reader() function and using DictReader
l
assaa class.
aasaa
s as
asaa

et
Python File Modes :
PPaadd P Padd
a
Mode
ww.. w
w ..
Description
wwww wwww

.N
‘r’ Open a file for reading. (default)
‘w’ Open a file for writing. Creates a new file if it does not
exist or truncates the file if it exists.

ai
‘x’ Open a file for exclusive creation. If the file already
eet t exists, the operation fails. e et t
l a
l i
a .iN
.N ‘a’ al l a
l i
a .iN
.N
Open for appending at the end of the file without
l a
l i
a .iN
.N
assaa ssaa
truncating it. Creates a new file if it does not exist.
a a a s
a aa
s
as
‘t’
P P a a dd
Open in text mode. (default)
P Paadd
‘b’
ww ..
Open in binary mode.
ww..
‘+’
ww ww Open a file for updating (reading and writing)
w www
ad

• The default mode of csv file in reading and writing is text mode
• Binary mode can be used when dealing with non-text files like image or exe files.

t t
.P

Garbage collector:
N ee t e e t
l a
l i
a .Ninitial space:
.Skip
i l a
l a.iN
.N
• Python has a garbage collector to clean up unreferenced objects
i l a
l i
a.iN
.N
assaa CSV•library: aa whitespaces after the delimiter aa
w

a ass
“skip initial space” is used for removing
dd dd a ass
P P aa P
. . objects and other code to read, write, and processP aa
. . data from and
w

w w
• The CSV library contains
to CSV files.w w ww w w
w w w w
w

csv.reader , csv.writer,csv.DictReader ,csv.DictWriter :


• csv.reader and csv.writer work with list/tuple.
• csv.DictReader and csv.DictWriter work with dictionary .
eet t eet t
• csv.DictReader and csv.DictWriter take additional argument fieldnames that are used as

l a
l i
a .iN
.N dictionary keys.
lal i
a.iN
.N l a
l i
a .iN
.N
assaa aa aa
• The csv.writer() method returns a writer object which converts the user’s data into
delimited strings. a
dd s
a s ddaa ss
P Paa P Pa a
• DictReader() class of csv module creates an object which maps data to a dictionary.
.. ..
operator.itemgetter():w
w w
w
wwww can be used to sort by more than one column
• operator.itemgetter() wwwwoperator.itemgetter()
151

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iN
itemgetter():
.N• itemgetter() with multiple indices is used
l a
l ai
to N
.Nby more than one column.
.isort l a
l i
a .iN
.N
assaa writerow() Vs Writerows() :
a add
• The writerow() method writes a as
one
s aa
row at a time. Writerows() method is
a
used
add
to aa s
write
saa
all
the data at once. .P
. P ..PP
w w ww
wwwcustom delimiter is read with the help of csv.register_dialect().
csv.register_dialect():
• CSV filew having wwww
close() method:
• close() method will free up the resources that were tied with the file
et
e t
Line Terminator:
eet t
lalai.iN
.N l a
l i
a .iN
.N l a
l i
a .iN
.N
• A Line Terminator is a string used to terminate lines produced by writer.

assaa aa aa
Dialect :
aass as
as

et
CSV.
PPaadd P Padd
• A dialect is a class of csv module which helps to define parameters for reading and writing
a
Ordered Dict: ww.. w
w ..
wwww wwww

.N
• An Ordered Dict is a dictionary subclass which saves the order in which its contents are
added.

ai
PART - I - TEXTBOOK EVALUATION

eet t PART - I e e t t
l a
l i
a .iN
.N al l a
l ai .iN
.N(1 Marks) l a
l i
a .iN
.N
assaa aa aa
Choose the best answer
a s
a s a s
a s
as
1. A CSV file is also known asa ad
a d
…. a dd 2020
a
March
a) Flat File
ww P P
.b). 3D File c) String File
ww P. P
d) Random. File
w w ww w w ww Ans : a) Flat File
ad

2. The expansion of CRLF is May 2022


a) Control Return and Line Feed b) Carriage Return and Form Feed
c) Control Router and Line Feed d) Carriage Return and Line Feed
t t
.P

N e e t e e t
l a
l i
a .NWhich of the following module is provided
.3. 
i l a
l ai.byiN
.N Ans : d) Carriage Return and Line Feed
Python to do several operations on the la l i
a.iN
.N
assaa CSV aa aa
w

files?
dda ass a as s
dd: c) csv
a) py
P Pa a
b) xls c) csv d) os
. .mode is used when dealing with non-text files P Pa a
Ans
.like. image or exe
w

w w
4. Which of the following
ww ww ww
files? w
w w w Aug 2022 Mar 2023
w

a) Text mode b) Binary mode c) xls mode d) csv mode


Ans : b) Binary mode
5.
N eet t
The command used to skip a row in a CSV file is
N e e t t
l a
l i
a .
i . N a) next() b) skip()
lal a.i. N
c) omit()
i d) bounce()
Ans : a) next() la
l i
a .iN
.N
assaa 6. csv a a dd a
Which of the following is a stringas saato terminate lines produced by writer()method
used
a add a as saa
of
module?
a) Line Terminatorww P P
.b). Enter key c) Form feed d) DatawTerminator w .. P
P
wwww www w: a) Line Terminator
Ans

152

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iN
7.
import csv
l a
l i
a .iN
What is the output of the following program?
.N .N l a
l i
a .iN
.N
assaa next (d)
aadd s
a aa
s
d=csv.reader(open(‘c:\PYPRG\ch13\city.csv’))
a aa a
dd s
a aa
s
for row in d:
ww P
.. P ww P
..P
wwww wwww
print(row)
if the file called city.csv” contain the following details
chennai,mylapore
mumbai,andheri
a) chennai,mylapore b) mumbai,andheri
eet t e et t
lalai.iN
.Nc) chennai
l a ai
mumba mumbai,andheri  Ans : b) mumbai,andheri
l .iN
d) chennai,mylapore
.N l a
l i
a .iN
.N
assaa 8. Which a as saawhich maps data to a dictionary? PTA
of the following creates an object
aas-s1a a

et
a) listreader() aa dd
b) reader()
P P c) tuplereader() d) DictReader ()a
P P add
ww.. wAns.: .d) DicReader ()
w
ww ww

.N
9. Making some ww
a) Editing
changes in the data of the
b) Appending
existing file or ww is called
adding
c) Modification
more data
d) Alteration
Ans : c) Modification

ai
t t t t
10. What will be written inside the file test. csv using the following program
ee ee
l a
l i
a .iN
.N import csv
al l a
l i
a .iN
.N
D = [[ Exam’],[‘Quarterly’],[ Halfyearly’]] l a
l i
a .iN
.N
assaa a s aa
s
csv.register_dialect( M’,lineterminator = ‘\n’)
a a s
a aa
s
as
aadd
with open( c:\pyprg\ch13\line2.csv’, w’) as f:
P P P Paadd
w
w ..
wr = csv.writer(f,dialect= M’)
ww..
wwww
wr.writerows(D)
wwww
ad

f.close()
a) Exam Quarterly Halfyearly b) Exam Quarterly Halfyearly
c) E d) Exam, Ans : d) Exam,
t t
.P

ee t
Q Quarterly,
ee t Quarterly,

l a
l i
a .iN
.N
H Halfyearly
l a
l i
a.iN
.N Halfyearly
l a
l i
a.iN
.N
assaa aa aa
PART - II
w

a as s
ddfollowing questions (2 Marks) dd aas s
P
.. Pa
Answer
athe
P P
.. - 3a a
w

1. ww
What is CSV File? w w ww ww PTA May 2022
w w is a human readable text file where each line has w
aw
w

• A CSV file number of fields, separated


by commas or some other delimiter.
• A CSV file is also called as a Flat File.
2.
eet t e et
Mention the two ways to read a CSV file using Python.t PTA - 2

l a
l i
a .iN
.N There are two ways to read a CSV file.
lal ai.iN
.N l a
l i
a .iN
.N
assaa aa aa
1. Use the csv module’s reader function
2. Use the DictReader class. a
dthe ass
d File. dd a as s
3. PP aa
. . in text mode. In this mode, while reading w
Mention the default modes of P P a a
. .the file the data
Mar 2023
• The default is w w w
ww
would be
wwformat of strings.
in the
reading
wwww
from

153

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iN
.N and writing is text mode.
l a
l i
a .iN
The default mode of csv file in reading
.N Method 1:
Syntax: l a
l i
a .iN
.N
assaa Mode a s
a aa
s
Description
aadd aa a
dd s
a aa
s
f = open(“test.txt”)
# perform file operations
‘r’
ww P
(default)
P
Open a file for reading.
.. w P
..
f.close()
w P
‘t’ wwww
Open in text mode. (default) wwww
# since no mode is specified the default
mode rt is used
4. What is use of next() function? Method 2:
The next() function returns the next item Syntax:
et
e t eet
from the iterator. It can also be used to t
with open(“test.txt”,’r’) as f:

lalai.iN
.N skip a row of the csv file.
l a
l i
a .iN
.N l a
l i
a .iN
.N
• The method1 is not entirely safe.

assaa aa aa
• If an exception occurs when you are
aass
5. How will you sort more than one
as
as

et
aadd
column from a csv file? Give an example
PP P Padd
performing some operation with the
a
statement. ww.. w
w ..
file, the code exits without closing the
file.
wwww wwww

.N
• To sort by more than one column • The best way to do this is using the
you can use itemgetter with multiple “with” statement.

ai
indices. • This ensures that the file is closed when

eet
operator.itemgetter (1,2)
t e et
the block inside with is exited. It is not
t
l a
l i
a .iN
.NSyntax:
al l a
l ai .iN
.N necessary to explicitly call the close()
method. l a
l i
a .iN
.N
assaa aa aa
sortedlist = sorted( data, key=operator.
a a ss
itemgetter( Colnumber ),reverse=True) • It is done internally. a ass
as
P P a add P P aadd an
Example:
ww ..
data = csv.reader(open(‘c:\\
2. Write a Python ..
program
w w to modify

ww ww existing file.
w
w ww Aug 2022
ad

PYPRG\\sample8.csv’))
Coding:
next(data) #(to omit the header)
import csv
#using operator module for sorting
row = [‘3: ‘Meena’Bangalore’]
t t
.P

ee t
multiple columns
ee t
l a
l i
a .iN
.N
sortedlist = sorted (data, key=operator.
l a
l i
a.iN
.N l a
l
reader = csv.reader(readFile)i
a.iN
with opent’student.csv; ‘r’) as readFile:
.N
assaa aa aa
itemgetter(1,2))
w

PART - III dd aa ss a
dd s
a s
lines = list(reader) # list()- to store each

P P a a
. . questions P
.. Paa
row of data as a list
w

w w
Answer the following ww
lines[3] = row
ww(3wwMarks) wwww
with open (student.csv, ‘w’) as writeFile:
w

# returns the writer object which converts


1. Write a note on open() function of python.
the user data with delimiter
What is the difference between the two
eet t
methods? eet
PTA - 1 Aug 2022t writer = csv.writer(writeFile)

l a
l i
a .iN
.N lal i
a.iN
.N
• Python has a built-in function open() to
l a
l i
a .iN
.N
#writerows()method writes multiple rows

assaa aa aa
to a csv file
open a file.
a
dd s
a s
• This function returns a file object also aass
writer. writerows(lines)
dd
P
..Paa
called a handle, as it is used to read or P
.. Paa
readFile.close()
ww ww
writeFile. close()
wwww
modify the file accordingly.
wwww
154

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iN Original File:
.N Roll No l a
l i
a .iN
3.
.N l a i
a .iN
Write a Python program to read a CSV
.N
file with default delimiter comma (,).
l
assaa aa aa
Name City
1 Harshini, aa
Chennai
dd s s Coding :
a
dd s
a s
Adhith, Pa a
#importing csv
2
P a
. . Bengaluru
Mumbai import csv P
..P a
w w ww
wwww wwww
3 Dhuruv, #opening the csv file which is in different
4 egiste, Tirchy location with read mode
with opent(‘c.\\pyprg\\samplel-csv’, ‘r’) as F:
5 Venkat Madurai
#other way to open the file is f= (‘c:\ \
Modified File after the coding: pyprg\ \ sample1.csv’, ‘r’)

N et
e t eet t reader = csv.reader(F)
a i
l la.i. N Roll No Name
l a
l i
a .iN
.N
City
l a
l i .iN
.N
# printing each line of the Data row by
a
s
a a
sa 1
aasaa
Harshini,
s Chennai row
as
asaa

et
2
PP add
Adhith,
a Mumbai
P Padd
print(row)
a
F.close()
3
ww..
Meena Bangalore
w
w ..
Output:
wwww wwww

.N
4 egiste, Tirchy
[‘SNO’, ‘NAME’, ‘CITY’]
5 Venkat Madurai
[‘12101’, ‘RAM’, ‘CHENNAI’]
[‘12102’, ‘LAVANYA’, ‘TIRCHY’]

ai
[‘12103’, ‘LAKSHMAN’, ‘MADURAI’]

N e et t N e et t
l a
l i
a .
i .
4.N al l a
l i
a .
i . N
What is the difference between the write mode and append mode. PTA - 2 & 5
l a
l i
a .iN
.N
assaa aa append mode is used to add the data aatasthesaa
write mode append mode
a a ss
as
The write mode creates a new
PPaadd
file.
P Pa add.
w w.. end of the file if the file ..
already
ww exists
If the file is ww existing write mode Otherwise createswwaw wone.
w w already new
ad

overwrites it.
5. What is the difference between reader() method and DictReader() class?Mar 2020 & 2023
t t
.P

e e t reader() method e e t DictReader() class

l a
l i
a .iN
.Ncsv. reader and csv.writer work with list/
l a
l i
a.iN
.N
csv.DictReader and csv.DictWriter work
l a
l i
a.iN
.N
assaa aa with dictionary. aa
w

tuple
aass
ddod not take csv.DictReader and csv.DictWriter aas s
dd take
P
csv. reader and csv.writer
.. . Paa P P aa
. . that are
w

w
additional argument
w ww
additional argument fieldnames
wwww wwww
used as dictionary keys
w

PART - IV

e et t t t
Answer the following questions (5 Marks)
e e
l a
l i
a .1.iN
.NDifferentiate Excel file and CSV file. lalai.iN .N PTA - 2 & 6 May 2022
l a
l i
a .iN
.N
assaa Excel
a a a as saa CSV
dd information CSV format is a plain textPformat aa a as s
ddwith a
aa
w w
about all the worksheets
PP
Excel is a binary file that holds
. .in a file, including both series of values separated
w w P
. .by commas.
content andww ww
formatting wwww
155

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

N Ne et t N Neet t N N
lalai .
i . Excel
l a
l i
a .
i . CSV
l a
l i
a .
i .
s
a a
sa as aa their CSV can be opened with any text editor
s
XLS files can only be read by applications
a a as saa
format, and can only PbeP aadd in the same in Windows like notepad,
that have been especially written to read
PPaa
MSddExcel,
ww .. written
Open Office, etc.
ww ..
wwww wwwwfor saving tabular
way.
CSV is a format
Excel is a spreadsheet that saves files into its
information into a delimited text file
own proprietary format viz. xls or xlsx
with extension .csv

N eet
Exceltconsumes-more memory while importing Importing
N ee t t CSV files can be much faster,

lalai.i N
. data l a
l ai .
i N
.and it also consumes less memory l a
l i
a .iN
.N
assaa 2. Tabulate the different modeaawith aa s aa
s a as aa
s

et
P P dd its meaning.
P Pa add
Mode
w w .for. reading (default) Description
w w ..
ww ww

.N
‘r’ open a file
ww w w
Open a file for writing. Creates a new file if it does not exist or truncates the
‘w’
file if it exists.

ai
eet t
‘x’
t t
Open a file for exclusive creation. If the file already exists, the operation fails.
e e
l a
l i
a .iN
.N ‘a’ al
Open for appending at the end ofithe
l a
l a.iN
.N
file without truncating it. Creates a new
l a
l i
a .iN
.N
assaa aa aa
file if it does not exist.
a s
a s a as s
as
‘t’
P Pa
Open in text mode.
a dddefault
P Pa add
‘b’
w w . . mode.
Open in binary
w w ..
ww wwa file for updating (reading and Writing) wwww
ad

‘+’ Open

3. Write the different methods to read a File in Python. Aug 2022


There are two ways to read a CSV file.
t t
.P

ee t
1. Use the csv module’s reader function e e t
l a
l i
a .iN
.N 2. Use the DictReader class.
l a
l i
a .iN
.N l a
l i
a.iN
.N
assaa csv aa aa
w

module’s reader function: ss s s


• We can read the contentsa ofd
a da
CSVa file with the help of csv.reader() method. ad
a da a
P
• The reader function.is
. P . P. P
w

w w w
designed to take each line of the file and make a list
w of all columns.
wwww
• Using this method one can read data from csv files of differentw w like quotes (“ “),
wwformats
w

pipe (|) and comma (,).


Syntax for csv.reader():
csv.reader( fileobject,delimiter,fmtparams)
eet t
where eet t
l a
l i
a .iN
.N lal i
a.iN
.N l
 file object: passes the path and the mode of the file a
l i
a .iN
.N
assaa  
aadd s
a aa
s
| etc can be omitted.
add
a aassaa
delimiter: an optional parameter containing the standard dilects like ,
a
w P
..P ww P
.. P
 Fmtparams: optional parameter which help to override the default values of the
w
wwww wwww
dialects like skipinitialspace,quoting etc. can be omitted.

156

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iNProgram:
.N
#importing csv
l a
l i
a .iN
.N l a
l i
a .iN
4. Write a Python program to write a CSV
.N
File with custom quotes.

assaa import csv


aa a
dd s
a aa
s
#opening the csv file which is in different a
Coding:
aadd s
a
import csv
aa
s
P
.. P
location with read mode
ww ww P
..P
csvData = [[‘SNO’,’Items’], [‘1’,’Pen’],
wwww
#other way to open the file is f= (‘c:\ \ wwww
with opent(‘c.\\pyprg\\samplel-csv’, ‘r’) as F: [‘2’,’Book’], [‘3’,’Pencil’]]
csv.register_dialect (‘myDialect’, delimiter
pyprg\ \ sample1.csv’, ‘r’) = ‘|’,quotechar = ‘”’, quoting = csv.
reader = csv.reader(F) QUOTE_ALL)
et
e t eet t
# printing each line of the Data row by row with open(‘c:\\pyprg\\ch13\\quote.

lalai.iN
.Nprint(row)
l a
l i
a .iN
.N l a
l i
a .iN
.N
csv’, ‘w’) as csvFile:

assaa saa s aa
F.close()
aa s aas
writer = csv.writer(csvFile,

et
Output:
PPaadd P P dd
dialect=’myDialect’)
aa
ww..
[‘SNO’, ‘NAME’, ‘CITY’]
w
w ..writer.writerows(csvData)

wwww wwww
[‘12101’, ‘RAM’, ‘CHENNAI’]

.N
print(“writing completed”)
[‘12102’, ‘LAVANYA’, ‘TIRCHY’] csvFile.close()
[‘12103’, ‘LAKSHMAN’, ‘MADURAI’]  When you open the “quote.csv” file in

ai
Reading CSV File into A Dictionary: notepad, we get following output:
e t t
• To read a CSV file into a dictionary
e eet t
l a
l i
a .iN
.N al l a
l i
a .iN
.N
can be done by using DictReader class Sl.No
1 l a
l i
a .iN
.N
”Items”
”Pen”
assaa aa aa
of csv module which works similar to
a s
a s a s
a s
as
2 ”Book”
P Paadd
the reader() class but creates an object
which maps data to a dictionary.
P Paadd 3 ”Pencil”
w
w ..
• The keys are given by the fieldnames as ww..
wwww wwww
5. Write the rules to be followed to format
ad

parameter.
• DictReader works by reading the the data in a CSV file. PTA - 5
first line of the CSV and using each 1. Each record (row of data) is to be located
on a separate line, delimited by a line
t t
.P

ee t e e t
comma separated value in this line as a
break by pressing enter key.
l a
l i
a .iN
.N dictionary key.
l a
l i
a.iN
• The columns in each subsequent row.N Example: l a
l i
a.iN
.N
assaa aa aa
w

a
dd s
a s
then behave like dictionary values and
a s
a s
xxx.yyy ( denotes enter Key to be pressed )
dd
.. Paa
can be accessed with the appropriate
P P
.. aa
2. The last record in the file mayor may
P
w

w
w
key (i.e. fieldname).
Program: w ww wwww not have an ending line break.
w w
w
w

Example:
import csv ppp,qqq
filename = ‘c:\\pyprg\ \sample8.csv’ yyy,xxx
eet t
inputfile =csv.DictReader( opet(filename’r’))
eet t
3. There may be an optional header line

l a
l i
a .iN
.N
for row in inputfile:
lal i
a.iN
.N l a
l i
a .iN
.N
appearing as the first line of the file

assaa Output: aa aa
print(dict(row) ) #dict() to print data
a as
dd ‘48’} s ddaas
with the same format as normal record
s
lines. The header will contain names
P P aa
. .‘Quantity’: ‘52’}
{‘ItemName ‘: ‘Keyboard “ ‘Quantity’: P
.. Paa
corresponding to the fields in the file
ww ww ww
{‘ItemName ‘: ‘Monitor:
ww
{‘ItemName ‘: ‘Mouse “ ‘Quantity’: ‘20’} wwww
and should contain the same number of
fields as the records in the rest of the file.
157

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iNExample:
.N l a i
a
field_name 1,field_name2,field_name3
l .iN
.N l a
l i
a .iN
.N
assaa aadd s
a aa
s
zzz,yyy,xxx CRLF(Carriage Return and Line Feed)
a aa a
dd s
a aa
s
4. Within the header and each record, there may be one or more fields, separated by commas.

w P
.. P ww P
..P
Spaces are considered part of a field and should not be ignored. The last field in the record
w
wwww
must not be followed by a comma.
Example: wwww
Red, Blue
5. Each field mayor may not be enclosed in double quotes. If fields are not enclosed with
et
e t eet t
double quotes, then double quotes may not appear inside the fields

lalai.iN
.NExample.
l a
l i
a .iN
.N l a
l i
a .iN
.N
assaa saa s aa
“Red”,”Blue”,”Green” #Field data with”
aa s aas

et
PP add
Black,White,Yellow #Field data without double quotes
a P Padd
a
ww.. w
w ..
6. Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in

wwww wwww
double-quotes.

.N
Example:
Red, Blue, Green

ai
7. If double-quotes are used to enclose fields, then a double-quote appearing inside a field
eet t eet
must be preceded with another double quote. t
l a
l i
a .iN
.N
Example: al l a
l i
a .iN
.N l a
l i
a .iN
.N
assaa “Red, “ “Blue”, “Green”
aassaa aassaa
as
P
PART
Paadd- ADDITIONAL QUESTIONS PPaadd
II
w
w .. ww..
wwww I. Choose the best answer (1 Marks)wwww
ad

1. Which of the following gives the python programmer the ability to parse CSV files?
a) sys module b) CSV module c) parse module d) CSV flat file
t t
.P

ee t e e t Ans : b) CSV module

l a
l i
a .2.iN
.NAbbreviation of CSV l a
l i
a.iN
.N l a
l i
a.iN
.N
assaa a)c) Condition aab) Colomn separated values aa
w

systematic values
Comma solution values d aass
d d) Comma separated aass
ddvalues
..P Pa a values
P
.. Paa
w

Ans : d) Comma separated


ww ww aw
w w
w of fields,
ww
3. A ___________is w
a human readable text file where each line hasw number
w

separated by commas or some other delimiter.


a) CSV file b) Colomn separated values

eet t
c) CSV sheet
t t
d) Condition systematic values Ans : a) CSV file
e e
l a
l i
a .iN
.N
4.
lal i
a.iN
.N
Which of the following can protect if the data itself contains commas in CSV file?
l a
l i
a .iN
.N
assaa 5. In CSV file, each record is a aa aa
a) ‘ ‘ b) , , c) “ “ d) ‘ Ans : c) “ “
aas s
ddlocated on a separate line, delimited by aP a
dd bys
a s
pressing P P a
to be
.b). ESV key c) Tab key .. Pa a
line break

a) Enter key w ww w w
www wwww Ans : a) Enter key
d) Shift key

158

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t e e t t
lalai .iN
6.
.N There are _______ ways to read a CSV file.
a) 2 b) 3
l a
l ai .iN
.N
c) 4 d) Only one Ans : a) 2 ai.i.N
l l a N
assaa 7. ______________ built-in functiona
aa dd() a
iss saato open a file in Python .
used
d) openfile () a a dd aas s aa
a) readfn ()
w w P
.. P
b) open c) reader ()
ww P P
. .Ans : b) open ()
wwwcan
8. ________ mode
wbe used when CSV files dealing with non-text wwww files.
a) Write mode b) Binary mode c) Octal mode d) Write mode
Ans : b) Binary mode
9. eet t
The default file open mode is_____________. eet t
lalai.iN
.N a) rt b) x
l a
l ai
c) a .iN
.N d) rw Ans : a) rt ai.iN
l l a .N
assaa 10. The a as saathe help of the method a as saa

et
CSV file contents can be read with
a) read ( )
P a a
b) open
P dd( ) c) with open ( ) d) reader ( )
P Pa add
w w .. ww .Ans. : d) reader ( )
ww ww w

.N
11. ___________ ww
function is designed to take each line of the file w
and make a list of all
columns?

ai
a) read ( ) b) reader ( ) c) row ( ) d) list ( ) Ans : b) reader ( )
eet t e t
e t
12. __________ describes the format of the CSV file that is to be read.
l a
l i
a .iN
.N a) line space al
b) dialect
l a
l ai .iN
.N
c) whitespace d) delimiter
l a
l i
a .iN
.N
assaa a s
a aa
s aa ssaa
Ans : b) dialect
as
P a ddstore and re-use various formatting parameters
a
13. _______________allows to create,
P P Paadford CSV
file in reading andw
w ..
writing. w w..
a) class w www b) dialect wd)ww w ) Ans : b) dialect
ad

c) write( ) read(
14. __________is used to add elements in CSV .
a) update ( ) b) write ( ) c) append ( ) d) addition ( )
t t
.P

e e t e e t Ans : c) append ( )

l a
l i
a .15.iN
.NIn CSV file, ___________function is usedltoalasorti.iNN than one column .
.more l a
l i
a.iN
.N
assaa a) sorter ( ) a(a) c) itemsort ( ) d) morecolumns ( ) aassaa
w

d d as s
b) multiplesort
a Ans : a)dd
.P
. a a
P a writer object which converts the user’sww .P. a asorter
P delimited
()
w

16. ___________ method


the w
strings on w
w wreturns
w file-like object. ww data into
w given w
w
w

a) csv.writer( ) b) csv.write user ( ) c) csv.writes ( ) d) csv_writer ( )


Ans : a) csv.writer( )

t t t t
17. __________ method writes a row of data into the specified CSV file.
ee e e
l a
l i
a .iN
.N a) rows ( ) b) writerow ( )
lal i
a.iN
.N
c) row_data ( ) d) row_write ( )
l a
l i
a .iN
.N
assaa 18. ___________ function is usedaadtodprint aadata in dictionary format without order. aassaa
Ans : b) writerow ( )
aassthe dd
P P d) print_dictP)a
(P a
a) diet ( )
ww..
b) dictionarys ( ) c) read_dict ( )
ww . .Ans : a) diet ( )
wwww wwww
159

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iN a) freeup ( )
l a
l ai .iN
19. _______ method will free up the resources that were tied with the file.
.N .N
b) open_res ( )
l a
l i
a .iN
.N
assaa c) resource_close ( )
aa dda assaa d) close ( )] Ans : d) closes
a add aa (saa
)]

a) xlsx w w P
20. Excel files are saved with extension_______.
P
.b). CSV c) Wordpad w w P
d) Notepad P
. . Ans : a) xlsx
ww with extension_______.
wwsaved
21. CSV files are wwww
a) .CV b) .CSV c) .CVSC d) .CSE Ans : b) .CSV
22. _________ command arranges a CSV file list value in descending order
eet t
a) listname.sort ( ) eet t
b) listname.ascd ( )

lalai.iN
.N c) list_name. sort(reverse))
l a
l ai .iN
.N l a
d) sorting ( ) Ans : c) list_name. sort(reverse))
l i
a .iN
.N
assaa 23. Aa) _________ a assaalines produced by writer.
is a string used to terminate
aas saa

et
Linefeed a
PP a dd b) Delimiters P Paadd
c) Line Terminator . . Ans : c).Line
.
w w d) SingleQuotes
w w Terminator
wwwwused to sort by more than one column . wwww

.N
24. _______________is
a) Colsort b) itemgetter ( ) c) sor_ter ( ) d) more_item ( )

ai
Ans : b) itemgetter ( )

e et t II. Answer the following questionse(2t and


e t 3 Marks)

l a
l i
a .1.iN
.NCompare: text mode and binary mode. lalai.iN
al .N l a
l i
a .iN
.N
assaa aas saa a assaa
as
Text mode d d dd
P P a a
.in. text mode.
Binary mode
P
.. P a a
w w
The default is reading
w w w
Binary mode returns bytes.
ww w
ww This is the modew w
ad

In this mode, while reading from the file to be used when dealing
the data would be in the format of strings. with non-text files like image or exe files
2. How can you handle CSV file data contains comma?
t t
.P

ee t e e t
• If the fields of data in your CSV file contain commas,we can protect them by enclosing

l a
l i
a .iN
.N those data fields in double-quotes (“).
l a
l i
a.iN
.N l a
l i
a.iN
.N
assaa aa aa
w

dd a ass dd a as
• The commas that are part of your data will then be kept separate from the commas which
s
P P a
delimit the fields themselves.
. .file? a P
.. P aa
w

What is the use ofw w


3.
w w
CSV
wwfile format used to store tabular data, such was aw
w
w or database.
w
• CSV is a simple
w spreadsheet
w

• Since they’re plain text, they’re easier to import into a spreadsheet or another storage
database, regardless of the specific software
4.
eet t
Define: Garbage collector
eet t
l a
l i
a .iN
.N l a
l a.iN
.N
• Python has a garbage collector to clean up unreferenced objects but, the user must not
i l a
l i
a .iN
.N
assaa 5. method? aaspace at the beginning using register dialect( a) a
rely on it to close the file.

dd
How to read from CSV file that a ass
contains dd a as s
..PPa a P
.. P a a
ww w w can be removed, by registering new dialectsww
• The whitespaces
dialect (w
)w
class of csv module. wwww using csvregister ,

160

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

N eet
Atdialect describes the format of the csv file that
N e istto
e t
lalai .
i N •
l i
a .i N
. “skipinitialspace” is used for removinglawhitespaces be r 4, In dialects the parameter
. after the delimiter. l a
l i
a .iN
.N
assaa 6. •Define: dialect
A dialect is a class of csv aa a assaa
dd which helps to define parameters for reading a a aas saa
ddwriting
CSV. ww ..P P module
ww P
.. P and

ww
• It allows towcreate, wwwwfor data.
w store, and re-use various formatting parameters
7. Compare: sort( ) and sorted ( ).
• The sorted ( ) method sorts the elements of a given item in a specific order - ascending or

et
e t descending.
ee t t
lalai.iN
.N l a
l ai .iN
.N
• sort ( ) method performs the same way as sorted ( ).
l a
l i
a .iN
.N
assaa aa aa
• Only difference, sort ( ) method doesn’t return any value and changes the original list
a ass aas s

et
itself.
P P a a dinto
d a dictionary? P P a a dd
8. . . a dictionary can be done by using DictReaderww
Explain How to read CSV
w w file . . of csv module
wwwwsimilar to the reader ( )class but creates an w wwwwhich maps data to a

.N
• To read a CSV file into class
which works object
dictionary.

ai
• The keys are given by the fieldnames as parameter.
9.eet t
Define: Ordered Dict? e et t
l a
l i
a .iN
.N al l a
l i
a .iN
.N
• DictReader ( ) gives OrderedDict by default in its output.
l a
l i
a .iN
.N
assaa s
a aa
s a s
a aa
s
• An OrderedDict is a dictionary subclass which saves the order in which its contents are
a
as
added.
P Paadd P Paadd
w w..
• To remove the OrderedDict use dict ( ).
w
w ..
wwww in between writerow() and writerows() method.
10. Give the difference
wwww
ad

writerow() writerows()
The writerow() method writes one writerows() method writes all the data at once
t t
.P

e t
row at a time.
e e t
to the new CSV file.
e
l a
l i
a .iN
.NThe writerow() method writes one
a ai.iN
. N method writes multi
The writerows()
l l l a
l i
a.iN
.N
a a aa aa
w

s
a s dimensional data.
dd aa ss dimensional data.
dd a as s
11. Define:Modification .P. Pa a P Paa
. . data is called
w

w w ww
wwww
• Making some changes in the data of the existing file or
wwww
adding more
w

modification.
12. Write a note on Line Terminator.
• A Line Terminator is a string used to terminate lines produced by writer.
eet t eet t
l a
l i
a .iN
.N lal a i.iN
.N
registering new dialects using csv. register_dialect ( ) class of csv module.
l a
l i
a .iN
• The default value is \r or \n. We can write csv file with a line terminator in Python by
.N
assaa 13. Explain How to write Dictionarya
a add ass
into
aCSV
a file with custom dialects?
add
a aassaa
Coding :
w w P
.. P w w P
.. P
wwww wwww
import csv
csv.registecdialect(‘myDialect’, delimiter = ‘I; quoting=csv.QUOTE_ALL)

161

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t e et t
lalai .iN
.Nwith open(‘c:\\pyprg\\ch13\\ vgrade.
csv, ‘w’) as csvfile
l a
l i
a .
i N
. N
Fileobject :
passes the path and the
l a
l i
a .iN
.N
assaa aa aa
mode of the file.

dd s
a s
fieldnames = [‘Name’, ‘Grade’]
a an optional daas s
dstandard
parameter
.. Paa
writer = csv.DictWriter(csvfile,
P
fieldnames = fieldnames. dialect P
..
containing P a
thea
ww Delimiter :
w w
wwww
=”myDialect”)
writer.writeheader() www w
dilects
omitted.
like , | etc can be

writer.writerows([{‘Grade’: ‘B’, ‘Name’: optional parameter


Anu’}, which help to override

N et
e t
{‘Gra dee:’ ‘nA,’ ‘Name’: ‘Beena,’}
N eet t the default values

lalai.i. N{Grade’: ‘C: ‘Name’: ‘Tarun’}])


l a
l i
a .
i . N
Fmtparams :
i .iN
.N
of the dialects like
l a
l a
assaa saa s aa
print(“writing completed”)
aa s aa s
skipinitialspace,quoting

et
“Name” aa
”Grade”
P P dd P Padd
etc. can be omitted.
a
w
w . . ”B” Coding:
w w..
wwww ”A” wwww‘Age’], [‘Dhanush’,
”Anu”

.N
import csv
”Beena” csvData = [[‘Student’,
“Tarun” ”C” ‘17’], [‘Kalyani’, ‘18’], [‘Ram’, ‘15’]]

ai
with open(‘c:\\pyprg\\ch13\\Pupil.csv’,

eet t
14. How will you create CSV in text editor? eet t ‘w’) as CF:

l a
l i
a .iN
.N al l a i .iN
.N
• To create a CSV file in Notepad,First
l a l a
l a .iN
writer = csv.writer(CF)
i
# CF is the file object
.N
assaa open a new file using
a s
a aa
s a s
a aa
s
writer.writerows(csvData)
as

P P add
File →New or ctrl +N
a P Paadd
# csvData is the List name
w ..
• Then enter the data separating each
w ww..
CF.close()
wwww
value with a comma and each row with
wwww
ad

a new line. 16. Explain how to write CSV Files With


• Example: consider the following details Quotes
Topicl, Topic2, Topic3 • We can write the csv file with quotes,
t t
.P

ee t
one, two, three
ee t by registering new dialects using

l a
l i
a .iN
.N Examplel, Example2, Example3
l a
l i
a.iN
.N module. l a
l a.iN
.N
csv.register_dialect() class of csv
i
assaa aa aa
• Save this content in a file with the
w

extension.csv. a
dd s
a s Coding: a
dd s
a s
P
.. Paa P aa
import csv
.. P
w

15. Explain how to create a new normal CSV


w
w ww
info = [[‘SNO’, ‘Person’, ‘DOB’],
wwww
file to store data PTA - 4
wwww[‘1’, ‘Madhu’, ‘18/12/2001’],
w

• The csv.writer() method returns a


writer object which converts the user’s [‘2’, ‘Sowmya’,’19/2/1998’],
data into delimited strings on the given [‘3’, ‘Sangeetha’,’20/3/1999’],

eet t
file-like object.
eet t [‘4’, ‘Eshwar’, ‘21/4/2000’],

l a
l i
a .iN
.N lal i
a.iN
.N
• The writerow() method writes a row of
l a
l i
a .iN
.N
[‘5’, ‘Anand’, ‘22/5/2001’]]

assaa aa aa
data into the specified file. csv.register_
a
dd s
a s
• The syntax for csv.writer() is csv.
ddaass
dialect(‘myDialect’,quoting=csv.QUOTE_
P
..Paa
writer(fileobject,delimiter,fmtparams)
P
..
ALL)Paa
where , ww ww
wwww wwww with open(‘c:\\pyprg\\ch13\\person.
csv’, ‘w’) as f:
162

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iN
.N for row in info:
l a
l i
a .iN
writer = csv.writer(f, dialect=’myDialect’)
.N 2.
l a
l
store it in a list. i
a .iN
Explain how to read the CSV file and
.N
assaa writer.writerow(row)
f.close() a add aassaa
aadd s
a
it in a list:
aa
s
Coding for reading the CSV file and store
a
When you open w
 w PP
. . file, we
“person.csv”
P
..P
import csv
ww
ww
get followingwoutput
w : w
w ww
# other way of declaring the filename
inFile= ‘c:\\pyprg\\sample.csv’
“SNO”,”Person”,”DOB” F=open (inFile,’ r’)
”1”,”Madhu”,”18/12/2001” reader = csv.reader(F)
et
e t
”2”,”Sowmya”,”19/2/1998”
eet t
# declaring array

lalai.iN
.N ”3”,”Sangeetha”,”20/3/1999”
l a
l i
a .iN
.N
arrayValue = [ ]
l a
l i
a .iN
.N
assaa s aa s aa
# displaying the content of the list for row
”4”,”Eshwar”,”21/4/2000”
aa s aas

et
“5”,”Anand”,”22/5/2001”
PP a add in reader:
P Padd
a
w w .. w
w ..
arrayValue.append(row)
ww
III. Answer the following questions (5 Marks) print(row)
ww

.N
1.
ww
Explain how to read a specific column in
w
w
F.close( )
a CSV file. OUTPUT :

ai
[‘Topic1’, ‘Topic2’, ‘Topic3’]
eet
Coding for printing the selected column:
t e et t [‘ one’, ‘two’, ‘three’]

l a
l i
a .iN
.N import csv
al l a
l i
a .iN
.N
#opening the csv file which is in different
[‘Example1’, ‘Example2’, ‘Example3’]
l a
l i
a .iN
.N
assaa location with read mode
a s
a aa 3. the
s Explain how to read the CSV file and s
aa saa
sort
as
aadd
f=open(“c:\\pyprg\\13ample5.csv”,’r’)
P P
data in a particular
a
column.
P P add
.

w .. Coding for reading the .


#reading the File with the help of csv.
w w w .
CSV file and sort
reader( )
wwww the data in a ww given by user
wwcolumn
particular column:
ad

readFile=csv.reader(f) # sort a selected


#printing the selected column leaving the header column
for col in readFile: import csv
t t
.P

ee t
print col[0],col[3] ee t # other way of declaring the filename

l a
l i
a .iN
.N f.close ( )
l a
l i
a.iN
.N l a i.iN
.N
inFile= ‘c:\\pyprg\\sample6.csv’
l a
assaa aa aa
w

Sample5.csv File in Excel


a
dd s
a s a
dd s
a s
# opening the csv file which is in the same
location of this
A
P
.. Paa
B C D
P
.. Paa
w

python file
w
w
1 item Name Cost-Rs Quantity Profit ww
wwww wwww F=open(inFile:r’)
w

2 Keyboard 480 12 1152 # reading the File with the help of csv.
readerO
3 Monitor 5200 10 10400
reader = csv.reader(F)

e et t
4 Mouse 200 50 2000
eet t # skipping the first row(heading)

l a
l i
a .iN
.Nitem Name
OUTPUT
lal i
a.iN
. N next(reader)
l a
l i
a .
i N
.N
assaa aa aa
# declaring a list
Keyboard
Profit
1152 a
dd s
a s ddaass
arrayValue = [ ]
Monitor P
..
10400 Paa P
.. Paa
a = int(input (“Enter the column number 1
ww ww to 3:-”))
Mouse
wwww2000
wwww # sorting a particular column-cost

163

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iNfor row in reader:
.N
arrayValue.append(row[ a])
l a
l i
a .iN
.N 5.
l a i
a .iN
Write a program to set data at runtime
.N
and writing it in a CSV file.
l
assaa arrayValue.sort ( )
aa a
dd
for row in arrayValue:
s
a aa
s a s
Coding:
aadd a aa
s
import csv
print (row)
ww P
.. P ww P
..Pwith open (‘c\\pyprg\\ch13\\

w www
Eclose ( )
w
w ww vdynamicfile.csv’, ‘w’) as f:
w = csv.writer(f)
OUTPUT:
ans= ‘y’
Enter the column number 1 to 3:- 2
while (ans==’y’):
50
e
12et t eet t
name=input(“Name?:”)

lalai.iN
.N
10
l a
l i
a .iN
.N
date=input(“Date of birth:”)
Place=input (“Place:”) l a
l i
a .iN
.N
assaa 4. Terminator.
Explain how to read CSV file withs
a a asaa
line
as
asaa
W.writerow([name, date, place])

et
P P a add P Padd
a
ans=input(“Do you want to enter more
Coding: ww .. y/n?:”)
w
w ..
ww ww

.N
F=open(‘c:\\pyprg\\ch13\\dynamicfile.
import csvw w csv”,r’) w
w
Data = [[‘Fruit’, ‘Quantity’], [Apple, ‘5’], reader=csv.reader(F)

ai
[Banana, ‘7’]’ [‘Mango: ‘8’]] for row in reader:
t t
csv.register_dialect(‘myfrialect; delimiter
ee e et t print (row)

l a
l i
a .iN
.N al
= ‘|’, lineterminator = ‘\n’)
l a
l i
a .iN N :
.OUTPUT F.close()
l a
l i
a .iN
.N
assaa aa Name?: Nivethitha aa
with open(‘c:\\pyprg\\ch3\\line.csv:
a s
a s aass
as
‘w’) as f:
P Paadd Date of birth: 12/12/2001
P P aadd
w ..
writer = csv.writer(f, dialect=’myDialect’)
w Place: Chennai
w w . .y/n?: y
wwww
writer.writerows(Data) Do you want
Name?: Leenaw www
to enter more
ad

f.close ( )
Output: Date of birth: 15/10/2001
Place: Nagercoil
t t
.P

ee t Fruit Quantity
ee t Do you want to enter more y/n?: y

l a
l i
a .iN
.N Apple 5
l a
l i
a.iN
.N Name?: Padma
l a
l i
a.iN
.N
assaa aa aa
Date of birth: 18/08/2001
w

Banana 7
ddaass a
dd s
a s
Place: Kumbakonam
Mango 8 a a aa
Do you want to enter more y/n?: n
P
.. P P
.. P
w

w
w ww [‘Nivethitha’, ‘12/12/2001’, ‘Chennai’]
wwww wwww []
w

[‘Leena’, ‘15/10/2001’, ‘Nagercoil’]


[]
[‘Padma’, ‘18/08/2001’, ‘Kumbakonam’]
eet t eet t
l a
l i
a .iN
.N lal i
a.iN
.N l a
l i
a .iN
.N
assaa aa a
dd s
a

a☺a☺ ☺ ☺
s
add
a aassaa
ww P
..P ww P
.. P
wwww wwww
164

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

N ee t t N e et t
lalai .
i . N GOVT. QUESTION
l
PAPER
a
l i
a .
i . N
– MARCH 2023
l a
l i
a .iN
. N
s
a a
sa Time : 3.00 Hours
aas saa Maximum Marks : 70a
a ass a
a a ddpaper for fairness of printing. If there is anyPlack
Instructions: (1) Check the question
P P P aa dfairness,
of d
w w . .Hall Supervisor immediately.
inform the
w w ..
wwwwBlue or Black ink to write and underline andww
(2) Use
PART - I
wwto draw diagrams.
pencil

Note : (i) Answer all the questions. 15×1=15

N e et t ee t t
(ii) Choose the most appropriate answer from the given four alternatives and write the

lalai i.N_______ members are accessible from outside


.1.  l a
l i
a.theiN
option code and the corresponding answer. N
.class. l a
l i
a .iN
.N
assaa a) Secured members b) Public aa ssaa c) Private members d) Protected members as
asaa

et
P
2. Which of the following is.notP aaadd in Python?
members
P Padd
a
w w . keyword
d)w
w ..
wwwwof code that are used to perform a particular task iswcalled:
www

.N
a) continue b) break c) operator while
3. The small sections
a) Pseudo code b) Subroutines c) Modules d) Files

ai
4. The number of important control structures in Python:
e e
a) 5t t b) 3 c) 6
e e t t d) 4

l a
l i
a .5.iN al ai .iN
.NClass members are accessed through _____laloperator.
.N l a
l i
a .iN
.N
assaa 6. The a) # b) &
a s
a aa c) %
s d) .
a s
a aa
s
as
a add the Parent-Child relationship:
database Model which represents
P P P Paadd
a) Hierarchical
w w.b).Relational c) Object
w w ..
d) Network
w w wwis used for concatenation? w w ww
ad

7. The operator which


a) * b) + c) = d) &
8. Importing C++ program in a Python program is called ______
t t
.P

t
a) Interconnecting
e e b) Wrapping c) Parsing
ee t d) Downloading

l a
l i
a .9.iN
.N______ command is used to remove a table from
l a
l i
a N
.theNdatabase.
.iALTER l a
l i
a.iN
.N
assaa 10. The function that returns theaalargest aaof the selected column is: aa
b) DROP TABLE c) TABLE d) DELETE
w

a) DELETE ALL
d(d)a as s dda as s
P
.. P
value
..P Pa a
w

a) HIGH ( ) b) MAX c) MAXIMUM ( ) d) LARGE ( )


ww
ww representation is known are called: wwww w w
w w
11. The datatype whose
w

a) Concrete datatype b) Built-in datatype c) Abstract datatype d) Derived datatype


12. A Function which calls itself, is called as:

e et t
a) Lambda b) Built-in c) Returntstatement d) Recursion
e e t
l a
l i
a .13.iN
.NThe i.iN
.
mode which is used when dealing with non-text
lal a Nfiles like image or exe files: l a
l i
a .
i N
.N
s
a a
sa a) xls mode b) Text mode
aas sa a c) csv mode d) Binary mode
a as aa
s
14. In dynamic programming, the
P P aa d d
technique a
of storing the previously calculated values
P P d
a d
is called:
a) Memoization
w w .. b) Saving value property
w w ..
c) Mapping
wwww d) Storing value
wwww
property

222

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

eet t eet t
lalai .iN
15. Let set A = {3, 6, 9}, set B = {1, 3, 9}.
.N l a
l i
a .iN
.N
The result of the following snippet Print (set A | set B)
l a
l i
a .iN
.N
assaa a) {1}
a dd
a a a ssaa
b) {3, 6, 9, 1, 3, 9} d) {3, 9}
c) {1, 3, 6, 9}
aa a
dd s
a aa
s
Note : Answer any six ww P P
. . Q.No. 24 is compulsory.
PART - II
w w P P
. . 6×2=12
wwww
16. What is a Tuple?
questions.
Give an example. wwww Chapter-2
17. What is a scope? Chapter-3
18. How will you delete a string in Python? Chapter-8
19. ee t t
Write note on range ( ) in loop. eet t
lalai.iN
.N
20. What is class?
l al i
a .iN
.N l a
l i
a .iN
.NChapter-6
Chapter-10

assaa 21. a
What is Data Manipulation Language?
a ss aa as
asaa

et
Chapter-12
22. Mention the default modes a
P P ofa
ddFile.
the
P Padd
a
w w
23. List the general types
.
of.data visualization. w
w .. Chapter-13

wwwforwx in range (1, 11)] wwww


Chapter-16

.N
24. Squares = [x**2
print (squares) Chapter-9

ai
What will be output of the following Python code?
ee t t eet t
l a
l i
a .iN
.Nprint (squares) al
Squares = [x**2 for x in range (1, 11)]
l a
l i
a .iN
.N l a
l i
a .iN
.N
assaa a as saa a s
a aa
s
as
P P aadd No. 33 is compulsory.6×3=18
PART - III
P Paadd
w w . . of Interface.
Note : Answer any six questions. Question
w
w ..
ww by Dynamic Programming? wwww
25. Mention the characteristics
wunderstand
26. What do youw
Chapter-1
ad

Chapter-4
27. Explain Ternary operator with an example. Chapter-5

t t
.P

28.
ee t
Write the syntax of while loop.
ee t Chapter-6

l a
l i
a .iN
.N
29.
30. l a
l a.iN
.N
Differentiate - ceil ( ) and floor ( ) function.
i l a
l i
a.iN
.N Chapter-7
What is the difference between csv reader ( ) method and DictReader ( ) class? Chapter-13
assaa aa aa
w

31. a s
a s
Differentiate fetchone ( ) and fetchmany ( ).
dd a
dd s
a s Chapter-15
32.
P Paa
Write a Python program to display the given pattern.
.. P
.. Paa
w

C O M P U T E R w
w ww
wwww wwww
Chapter-8
COMPUTE
w

COMPUT
COMPU

e t t
COMP
e eet t
l a
l i
a .iN
.N COM
lal i
a.iN
.N l a
l i
a .iN
.N
assaa aa aa
CO
C a
dd s
a s ddaass
P
..Paa P
.. Paa
33. Write about the steps of Python program executing C++ program using control statement.
ww ww
wwww wwww
 Chapter-14

223

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]
wwww wwww
ww
www.Padasalai.Net
ww www.CBSEtips.in

Loyola EC – 12th Computer Science

N e et t eet t
lalai .N all the questions.
.Answer
i
Part - IV
l a
l ai .iN
.N [5 × 5 = 25] la
l i
a .iN
.N
assaa 34. a) What is a List? Why List,acanadd aa
be ssaaas pairs? Explain with suitable example.ddaassaa
called
a a
Chapter-2

w w
b) Discuss about Linear . P
. P
Search
(OR)
Algorithm. w w .P.P Chapter-4

ww
35. a) Discuss in
w w
details about Token in Python. ww w w Chapter-5
(OR)
b) Explain the following built-in function. Chapter-7

et
e t
(i) id ( )
eet t
lalai.iN
.N (ii) chr ( )
l a
l i
a .iN
.N l a
l i
a .iN
.N
assaa (iii) round ( )
aasaa
s as
asaa

et
(iv) type ( )
PPaadd P Padd
a
(v) pow ( )
ww.. w
w ..
wwww wwww

.N
36. a) What is Nested Tuple? Explain with an example. Chapter-9
(OR)
b) Explain the different types of relationship mapping.

ai
Chapter-11
37. a) Write the syntax for getopt ( ) and explain its arguments and return values.
eet t eet t Chapter-14

l a
l i
a .iN
.N al l a
l
b) Differentiate DBMS and RDBMS. i
a .iN
.N (OR)
l a
l i
a .iN
.N Chapter-11

assaa a s
a aa
s a s
a
38. a) Explain about differences between Histogram and Bar Graph. aa
s Chapter-16
as
P Paadd (OR)
P Paadd
w
w ..
b) Explain ‘continue’ statement with an example.
ww.. Chapter-6

wwww wwww
ad

*****

t t
.P

ee t ee t
l a
l i
a .iN
.N l a
l i
a.iN
.N l a
l i
a.iN
.N
assaa aa aa
w

a
dd s
a s a
dd s
a s
P
.. Paa P
.. Paa
w

w
w ww
wwww wwww
w

eet t eet t
l a
l i
a .iN
.N lal i
a.iN
.N l a
l i
a .iN
.N
assaa aa a
dd s
a aa
s
add
a aassaa
ww P
..P ww P
.. P
wwww wwww
224

eet t eet t
aai.iN
.N aai.iN.N aai.iN
.N
Kindly send me your study materials to [email protected]

You might also like