Namma Kalvi 12th Computer Science Unit 3 Sura Guide Em (1)
Namma Kalvi 12th Computer Science Unit 3 Sura Guide Em (1)
-
UNIT III MODULARITY AND OOPS
www.nammakalvi.org
9 AND DICTIONARY
CHAPTER SNAPSHOT
9.1 Introduction to List
9.2 Tuples
ns
9.3 Sets
9.4 Dictionaries
Evaluation
io
at
5. If List=[17,23,41,10] then List.append(32) will
Part - I
ic
result
Choose the best answer (1 mark) (a) [32,17,23,41,10] (b) [17,23,41,10,32]
bl
1. Pick odd one in connection with collection (c) [10,17,23,32,41] (d) [41,32,23,17,10]
Pu
will result in
[Ans. (c) extend()]
(a) 10 (b) 8 (c) 4 (d) 6
[Ans. (b) 8] 7. What will be the result of the following Python
code?
3. Which of the following function is used to
count the number of elements in a list? S=[x**2 for x in range(5)]
(a) count() (b) find() print(S)
(c) len() (d) index() (a) [0,1,2,4,5] (b) [0,1,4,9,16]
[Ans. (c) len()] (c) [0,1,4,9,16,25] (d) [1,4,9,16,25]
[Ans. (b) [0,1,4,9,16]]
4. If List=[10,20,30,40,50] then List[2]=35 will
result 8. What is the use of type() function in python?
(a) [35,10,20,30,40,50] (a) To create a Tuple
(b) [10,20,30,40,50,35] (b) To know the type of an element in tuple
(c) [10,20,35,40,50] (c) To know the data type of python object
(d) [10,35,30,40,50][Ans. (c) [10,20,35,40,50]] (d) To create a list.
[Ans. (c) To know the data type of python
[109] object]
9.
www.surabooks.com
Sura’s
This material only for sample
www.nammakalvi.org
What will be the value of x in following python
code?
(a) A list is mutable List1=[2,4,6[1,3,5]]
(b) A tuple is immutable x=len(List1)
(c) The append() function is used to add an Ans. x = 4.
element 4. Differentiate del with remove( ) function of
(d) The extend() function is used in tuple to add List.
elements in a list Ans.
[Ans. (d) The extend() function is used in del function remove ( )
tuple to add elements in a list]
function
10. Let setA={3,6,9}, setB={1,3,9}. What will be (i) del statement is remove( ) function
the result of the following snippet? used to delete is used to delete
print(setA|setB) known elements elements of a
list if its index is
(a) {3,6,9,1,3,9} (b) {3,9} whereas unknown.
(c) {1} (d) {1,3,6,9}
ns
(ii) The del statement The remove ()
[Ans. (d) {1,3,6,9}] function can be
can also be used to
11. Which of the following set operation includes delete entire list. used to delete one
io
or more elements
all the elements that are in two sets but not the if the index value
one that are common to two sets? at is not known.
(a) Symmetric difference (b) Difference
5. Write the syntax of creating a Tuple with n
(c) Intersection (d) Union
ic
number of elements.
[Ans. (a) Symmetric difference]
Ans. Syntax:
bl
12. The keys in Python, dictionary is specified by Tuple_Name = tuple( [list elements] )
(a) = (b) ; (c) + (d) : 6. What is Set in Python?
Pu
duplicates.
(2 marks) (ii) That means the elements within a set cannot
be repeated. This feature used to include
Su
110
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
2.
www.surabooks.com
Sura’s
6.
www.surabooks.com
ns
'SaiSree', 'Lavanya']
(iii) Lists are used to dictionary is used
MyList.sort( ) look up a value to take one value
print(MyList) whereas. and look up another
io
MyList.sort(reverse=True) at value.
print(MyList)
Part - IV
Output:
Answer the following questions
ic
['Anitha', 'Lavanya', 'SaiSree', 'Tharani',
(5 marks)
bl
'Thilothamma']
['Thilothamma', 'Tharani', 'SaiSree', 'Lavanya',
Pu
existing list.
4. Explain the difference between del and clear( ) (ii) Syntax:
in dictionary with an example. List.append (element to be added)
Ans. In Python dictionary, del keyword is used to List.extend ( [elements to be added])
delete a particular element. The clear( ) function (iii) In extend( ) function, multiple elements
should be specified within square bracket
is used to delete all the elements in a dictionary.
as arguments of the function.
5. List out the set operations supported by (iv) Example :
python. >>> Mylist=[34, 45, 48]
Ans. The python supports the set operations such as >>> Mylist.append(90)
Union, Intersection, difference and Symmetric >>> print(Mylist)
difference. [34, 45, 48, 90]
111
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit III - Chapter 9
www.surabooks.com
Sura’s
(v)
➠ XII Std - Computer Science
In the above example, Mylist is created
with three elements. Through >>> Mylist.
This material only for sample
Output :
2
www.surabooks.com
www.nammakalvi.org
ns
(iii) Example : >>> print(Even_List)
>>> MyList=[34,98,47,'Kannan', [2, 4, 6, 8, 10]
'Gowrisankar', 'Lenin', 'Sreenivasan' ]
io
(iv) In the above code, list( ) function takes the
>>> print(MyList) at result of range( ) as Even_List elements.
[34, 98, 47, 'Kannan', 'Gowrisankar', Thus, Even_List list has the elements of first
'Lenin', 'Sreenivasan'] five even numbers.
ic
>>> MyList.insert(3, 'Ramakrishnan') Similarly, we can create any series of values
>>> print(MyList) using range( ) function. The following
bl
[34, 98, 47, 'Ramakrishnan', 'Kannan', example explains how to create a list with
squares of first 10 natural numbers.
'Gowrisankar', 'Lenin', 'Sreenivasan']
Pu
for x in range(1,11):
of values in Python. Using range( ) function, you s = x ** 2
can create list with series of values. The range( )
Su
squares.append(s)
function has three arguments.
print (squares)
Syntax of range ( ) function:
range (start value, end value, step value) 3. What is nested tuple? Explain with an example.
where, Ans. Nested Tuples :
(i) start value – beginning value of series. (i) In Python, a tuple can be defined inside
Zero is the default beginning value. another tuple; called Nested tuple. In a
(ii) end value – upper limit of series. Python nested tuple, each tuple is considered as
takes the ending value as upper limit – 1. an element. The for loop will be useful to
access all the elements in a nested tuple.
(iii) step value – It is an optional argument,
which is used to generate different interval (ii) Example :
of values. Toppers = (("Vinodini", "XII-F", 98.7),
Generating first 10 even numbers : ("Soundarya", "XII-H", 97.5),
for x in range (2, 11, 2): ("Tharani", "XII-F", 95.3), ("Saisri",
print(x) "XII-G", 93.8))
for i in Toppers:
print(i)
112
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
www.surabooks.com
(iii) Output :
('Vinodini', 'XII-F', 98.7)
This material only for sample
Sura’s
www.surabooks.com
ns
Output :
{'A', 'D'}
io
Difference :
(i)In python, the operator | is used to union of (i) It includes all elements that are in fi rst set
two sets. The function union( ) is also used
at (say set A) but not in the second set (say set B)
to join two sets in python.
ic
(ii) Example : Program to Join (Union) two Set A Set B
bl
113
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit III - Chapter 9
www.surabooks.com
www.nammakalvi.org
Write a program that prints the maximum
value in a Tuple.
sets (say sets A and B) but not the one that Ans. tuple 1 = (5, 17, 15, 20, 7, 3)
are common to two sets. print (“Maximum value”, max (tuple 1))
3. Write a program that finds the sum of all the
Set A Set B
numbers in a Tuples using while loop.
Ans. li = [15, 25, 17]
s=0
i=0
while i < len (i):
(ii) The caret (^) operator is used to symmetric s = s + li [i]
difference set operation in python. The i+=1
ns
function symmetric_difference( ) is also print (s)
used to do the same operation.
4. Write a program that finds sum of all even
(iii) Example : Program to symmetric
io
numbers in a list.
difference of two sets using caret operator
Ans. list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
set_A={'A', 2, 4, 'D'}
at e=0
set_B={'A', 'B', 'C', 'D'} for i in list:
ic
print(set_A ^ set_B) if i % 2 = = 0:
bl
Output : e=e+i
{2, 4, 'B', 'C'} print (“Sum of even number”, e)
Pu
print(set_A.symmetric_difference(set_B))
while i > = – 5:
Output :
print (list [i])
{2, 4, 'B', 'C'}
i=i+–1
Hands on Experience 6. Write a program to insert a value in a list at the
specified location.
1. Write a program to remove duplicates from a
list. Ans. list = [1, 2, 4, 5]
Ans. mylist = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] list insert (2, 3)
r = [] print (list)
for i in mylist:
if i not in res:
res. append (i)
print (res)
114
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
7.
www.surabooks.com This material only for sample
ns
for n, i enumerate (a):
if (i % 3 = = 0): print (“Minimum = ”, min (v))
del a [n]
io
print (a)
[Ans. (b) 4]
7. Which of the following can be used to access
2. Which of the following is not datatype in an element in a list?
Python? (a) Subscript
ra
115
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
www.surabooks.com This material only for sample www.surabooks.com
Chapter PYTHON CLASSES AND
10 OBJECTS
www.nammakalvi.org
ns
CHAPTER SNAPSHOT
10.1 Introduction
io
10.2 Defining classes
at
10.3 Creating Objects
10.4 Accessing Class Members
ic
10.5 Class Methods
10.6 Constructor and Destructor in Python
bl
[133]
Evaluation
This material only for sample www.surabooks.com
www.nammakalvi.org
self.name=name
Part - I S=Student(“Tamil”)
Choose the best answer (1 mark) (a) Error (b) Tamil
(c) name (d) self
1. Which of the following are the key features of
[Ans. (b) Tamil]
an Object Oriented Programming language?
(a) Constructor and Classes 9. Which of the following is the private class
(b) Constructor and Object variable?
(c) Classes and Objects (a) __num (b) ##num
(d) Constructor and Destructor (c) $$num (d) &&num
[Ans. (c) Classes and Objects] [Ans. (a) __num]
2. Functions defined inside a class: 10. The process of creating an object is called as:
(a) Functions (b) Module (a) Constructor (b) Destructor
ns
(c) Methods (d) section (c) Initialize (d) Instantiation
[Ans. (c) Methods] [Ans. (d) Instantiation]
io
3. Class members are accessed through which
operator?
Part - II
Answer the following questions
(a) & (b) . (c) # (d) %
at
[Ans. (b) . ] (2 marks)
ic
4. Which of the following method is automatically 1. What is class?
bl
executed when an object is created? Ans. A class is a way of binding data members and
(a) __object__( ) (b) __del__( ) member function together. Class is a template
Pu
(a) __ (b) && (c) ## (d) ** or instance of that class. This process of creating
[Ans. (a) __] object is called as “Class Instantiation”.
Su
6. Which of the following method is used as 3. What is the output of the following program?
destructor? class Sample:
(a) __init__( ) (b) __dest__( ) __num=10
(c) __rem__( ) (d) __del__( ) def disp(self):
[Ans. (d) __del__( )] print(self.__num)
7. Which of the following class declaration is S=Sample()
correct? S.disp()
(a) class class_name print(S.__num)
(b) class class_name<> Ans. 10
(c) class class_name:
(d) class class_name[ ] 10
[Ans. (c) class class_name:] 4. How will you create constructor in Python?
8. Which of the following is the output of the Ans. General format of __init__ method
following program? (Constructor function)
class Student: def __init__(self, [args ……..]):
def __init__(self, name): <statements>
134
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
5.
www.surabooks.com
Sura’s
www.surabooks.com
ns
2. Write a class with two private class variables is executed automatically when the object is
and print the sum using a method. created.
Ans. Class sum: (iii) This constructor function can be defined
io
– a = 10 with or without arguments. This method is
used to initialize the class variables.
– b = 20
at General format of __init__ method (Construc
tor function)
def disp (self):
def __init__(self, [args ……..]):
ic
print (“sum =”, (self. – a + self. - b))
<statements>
s = sum ()
Part - IV
bl
s. disp ()
Find the error in the following program to get Answer the following questions
Pu
3.
the given output? (5 marks)
class Fruits: 1. Write a menu driven program to add or delete
stationary items. You should use dictionary to
def __init__(self, f1, f2):
store items and the brand.
ra
self.f1=f1
Ans. Class stationary:
self.f2=f2
Su
135
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit III - Chapter 10
www.surabooks.com
www.nammakalvi.org
A = area ()
b = int (input (“enter base”))
1. Write a program using class to store name and h = int (input (“enter height”))
marks of students in list and print total marks.
(a) area (b, h)
Ans. Class total :
name = [] 3. Write a menu driven program to read, display,
mark = [] add and subtract two distances.
def get (self): Ans. def add (d1, d2)
num = input (“How many students? :”) return d1 + d2
for i in self. num; def subtract (d1, d2)
self. n = input (“Enter student name”)
return d1 – d2
self name. append (self. n)
self. M = int inp (“enter mark of the student”) “/. Read \n”, “2. Display \n”,
mark = append (self. m) print (“Menu \n, 3. Add \n”, “4. Subtract \n”)
def display (self): ch = int (input (“enter your choice”))
ns
for i in self. Num if (ch = = 1):
print (name [i] “:”, mark [i]) x = int (input (“enter distance 1”))
t = t + mark [i]
io
y = int (input (“enter distance 2”))
print (“Total marks”, t)
dif (ch = = 2):
t = total ()
t. get ()
at print (“Distance 1”, x)
t. display () print (“Distance 2”, y)
ic
2. Write a program using class to accept three dif (ch = = 3):
bl
Choose the Correct Answer 1 MARK 3. Functions of the class are called as
(a) Methods (b) Members
1. Which of the following is not an object (c) Variables (d) Loog
oriented language? [Ans. (a) Methods]
(a) C (b) C++
(c) Java (d) Python 4. In Python, every class name followed by
[Ans. (a) C] (a) ; (b) : (c) : : (d) .
2. Which of the following called as instances of a [Ans. (b) :]
class or class variable? 5. Which of the following with a valid class
(a) Methods definition?
(b) Objects (a) Class classname () statement_1
(c) Functions (b) Class classname : : statement_1
(d) Datatypes (c) Class classname statement_1
[Ans. (b) Objects] (d) Class classname statement_1
www.nammakalvi.org [Ans. (c) Class classname statement_1]
136
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757