Tamilnadu State Board New Syllabus Samacheer Kalvi 12th Computer Science Guide Pdf Chapter 9 Lists, Tuples, Sets and Dictionary Text Book Back Questions and Answers, Notes.

Tamilnadu Samacheer Kalvi 12th Computer Science Solutions Chapter 9 Lists, Tuples, Sets and Dictionary

12th Computer Science Guide Lists, Tuples, Sets and Dictionary Text Book Questions and Answers

I. Choose the best answer (1 Marks)

Question 1.
Pick odd one in connection with collection data type
a) List
b) Tuple
c) Dictionary
d) Loop
Answer:
d) Loop

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 2.
Let list l=[2,4,6,8,10], then print(Listl[-2]) will result in
a) 10
b) 8
c) 4
d) 6
Answer:
b) 8

Question 3.
Which of the following function is used to count the number of elements in a list?
a) count()
b) find()
c) len()
d) index()
Answer:
c) len()

Question 4.
If List= [10,20,30,40,50] then List[2]=35 will result ~
a) [35,10,20,30,40,50]
b) [10,20,30,40,50,35]
c) [10,20,35,40,50)
d) [10,35,30,40,50]
Answer:
c) [10,20,35,40,50]

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 5.
If List= [17,23,41,10] then List.append (32) will result
a) [32,17,23,41,10]
b) [17,23,41,10,32]
c) [10,17,23,32,41]
d) [41,32,23,17,10]
Answer:
b) [17,23,41,10,32]

Question 6.
Which of the following Python function can be used to add more than one element within an existing list?
a) append ()
b) append_more()
c) extend ()
d) more()
Answer:
c) extend()

Question 7.
What will be the result of the following Python code?
S=[x**2 for x in range(5)]
print(S)
a) [0,1,2,4,5]
b) [0,1,4,9,16]
c) [0,1,4,9,16,25]
d) [1,4,9,16,25]
Answer:
b) [0,1,4,9,16]

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 8.
What is the use of type() function in python?
a) To create a Tuple
b) To know the type of an element in the tuple
c) To know the data type of python object
d) To create a list.
Answer:
c) To know the data type of python object

Question 9.
Which of the following statement is not correct?
a) A list is mutable
b) A tuple is immutable.
c) The append () function is used to add an element.
d) The extend () function is used in tuple to add elements in a list.
Answer:
d) The extend() function is used in tuple to add elements in a list.

Question 10.
Let setA={3,6,9], setB={l,3,9}. What will be the result of the following snippet? print(setA | setB)
a) {3,6,9,1,3,9}
b) {3,9} c) {1}
c) {1}
d) {1,3,6,9}
Answer:
d) [1,3,6,9}

Question 11.
Which of the following set operation includes all the elements that are in two sets but not the one that are common to two sets?
a) Symmetric difference
b) Difference
c) Intersection
d) Union
Answer:
a) Symmetric difference

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 12.
The keys in Python, the dictionary is specified by
a) =
b) ;
c) +
d) :
Answer:
d) :

II. Answer the following questions (2 Marks)

Question 1.
What is List in Python?
Answer:
A list in Python is known as a “sequence data type” like strings. It is an ordered collection of values enclosed within square brackets [ ]. Each value of a list is called an element.

Question 2.
How will you access the list elements in reverse order?
Answer:

  • Python enables reverse or negative indexing for the list elements.
  • Thus, python lists index in the opposite order.
  • The python sets -1 as the index value for the last element in the list and -2 for the preceding element and so on. This is called Reverse Indexing.

Example: Age = [15,20,29,45,60]

Age 15 20 29 45 60
Positive index 0 1 2 3 4
Negative index -5 -4 -3 -2 -1

Question 3.
What will be the value of x in the following python code?
Answer:
List1=[2, 4, 6, [1, 3, 5]]
x=len(List1)
Ans: 4

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 4.
Differentiate del with remove() the function of List.
Answer:

del statement remove () function
1. del is a statement used to delete the known elements of a list remove() the function is used to delete elements of a list if its index is unknown.
2. The del command can also be used to delete the entire list. The remove () function can be used to delete one or more elements if the index value is not known.

Question 5.
Write the syntax of creating a Tuple with n number of elements.
Answer:
# Tuple with n number elements
Tuple _ Name = (E1, E2, E2 ……………… En)
# Elements of a tuple without parenthesis
Tuple_Name = E1, E2, E3 …………………. En

Question 6.
What is set in Python?
Answer:

  • Set is another type of collection data type.
  • A Set is a mutable and unordered collection of elements without duplicates.

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

III. Answer the following questions (3 Marks)

Question 1.
What are the advantages of Tuples over a list?
Answer:

  • The elements of a list are changeable (mutable) whereas the elements of a tuple are unchangeable (immutable), this is the key difference between tuples and list.
  • The elements of a list are enclosed within square brackets. But, the elements of a tuple are enclosed by parentheses.
  • Iterating tuples is faster than a list.

Question 2.
Write a shot note about sort().
Answer:
Function : Sort ()
Syntax:
List.sort( reverse=True | False, key=my Func)

  • Both arguments are optional.
  • If reverse is set as True, list sorting is in descending order.
  • Ascending is default.

Description:
Sort () function sorts the element in list

Example:
MyList=[‘Thilothamma’, ‘Tharani’, ‘Anitha’, ‘SaiSree’, ‘Lavanya’]
MyList.sort()
print(MyList)
MyList.sort(reverse=True)
print(MyList)
Output:
[‘Anitha’, ‘Lavanya’, ‘SaiSree’, ‘Tharani’, ‘Thilothamma’]
[‘Thilothamma’, ‘Tharani’, ‘SaiSree’, ‘Lavanya’, ‘Anitha’]

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 3.
What will be the output of the following code?
list = [2**x for x in range(5)] print(list)
Output:
[1,2,4,8,16]

Question 4.
Explain the difference between del and clear() in the dictionary with an example.
Answer:

del clear()
1. del keyword is used to delete a particular element of a dictionary The clear() function is used to delete all the elements in a dictionary
2. del keyword can be used to remove the dictionary The clear() function is used to delete all the elements in a dictionary not a dictionary structure.
3. Example: del Dict[‘MarkT] del Diet Example: Dict.clear()

Question 5.
List out the set operations supported by python.
Answer:
The python supports the set operations such as
Union, Intersection, Difference, and Symmetric difference.

Union:

  • The union includes all elements from two or more sets.
  • In python, the operator | (pipeline) is used to the union of two) sets.
  • The function union() is also used to join two sets in python.

Intersection:

  • Intersection includes the common elements in two sets.
  • The operator & is used to intersect two sets in python.
  • The function intersection() is also used to intersect two sets in python.

Difference:

  • The difference includes all elements that are in the first set (say set A) but not in the second set (say set B).
  • The minus (-) operator is used to difference set operation in python.
  • The function difference() is also used to difference operation.

Symmetric difference:

  • The symmetric difference includes all the elements that are in two sets (say sets A and B) but not the one that are common to two sets.
  • The caret (A) operator is used to symmetric difference set operation in python.
  • The function symmetric_difference() is also used to do the same operation.

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 6.
What are the differences between List and Dictionary?
Answer:

List Dictionary
List is an ordered set of elements Dictionary is a data structure that is used for matching one element (Key) with another (Value).
The index values can be used to access a particular element. In dictionary, key represents an index and key may be a number of a string.
Lists are used to look up a value Dictionary is used to take one value and look up another value.

IV. Answer the following questions (5 Marks)

Question 1.
What the different ways to insert an element in a list. Explain with suitable example.
Answer:
append( ) function in Python is used to add more elements to a list. But, it includes elements at the end of a list. If you want to include an element at your desired position, you can use the insert () function is used to insert an element at any position of a list.
Syntax:
List, insert (position index, element)
Example:
>>> MyList=[34,98,47, ‘Kannan’, ‘Gowrisankar’, ‘Lenin’, ‘Sreenivasan’ ]
>>> print(MyList)
[34, 98, 47, ‘Kannan’, ‘Gowrisankar’, ‘Lenin’, ‘Sreenivasan’]
>>> MyList.insert(3, ‘Ramakrishnan’)
>>> print(MyList)
[34, 98, 47, ‘Ramakrishnan’, ‘Kannan’, ‘Gowrisankar’, ‘Lenin’, ‘Sreenivasan’]
In the above example, insertf) function inserts a new element ‘Ramakrishnan’ at the index value 3, ie. at the 4th position. While inserting a new element in between the existing elements, at a particular location, the existing elements shifts one position to the right.

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 2.
What is the purpose of range()? Explain with an example.
Answer:
(i) The range( ) is a function used to generate a series of values in Python. Using the range( ) function, you can create list with series of values. The range( ) function has three arguments.
Syntax of range( ) function:
range (start value, end value, step value)
where,

  • start value – beginning value of series. Zero is the default beginning value.
  • end value – the upper limit of series. Python takes the ending value as upper limit – 1.
  • step value – It is an optional argument, which is used to generate different intervals of values.

Example: Generating whole numbers upto 10
for x in range (1, 11):
print(x)
Output
1
2
3
4
5
6
7
8
9
10

(ii) Creating a list with series of values
Using the range( ) function, you can create a list with series of values. To convert the result of range( ) function into list, we need one more function called list( ). The list( ) function makes the result of range( ) as a list.
Syntax:
List_Varibale = list ( range ( ) )
Note
The list( ) function is all so used to create list in python.
Example
>>> Even_List = list(range(2,11,2))
>>> print(Even_List)
[2, 4, 6, 8, 10]
In the above code, list( ) function takes the result of range( ) as Even List elements. Thus, Even _List list has the elements of first five even numbers.

(iii) We can create any series of values using the range( ) function. The following example explains how to create a list with squares of the first 10 natural numbers.
Example: Generating squares of first 10 natural numbers
squares = [ ]
for x in range(1,11):
s = x ** 2
squares.append(s)
print (squares)
Output
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Question 3.
What is a nested tuple? Explain with an example.’
Answer:

  • In Python, a tuple can be defined inside another tuple; called a Nested tuple.
  • In a nested tuple, each tuple is considered as an element.
  • The for loop will be useful to access all the elements in a nested tuple.

Example:
Toppers = ((“Vinodini”, “XII-P”, 98.7),
Ç’Soundarya”, “XII-H”, 97.5),
(“Tharani”, “XII-P”, 95.3),
(“Saisri”, “XII-G”, 93.8))
for j in Toppers:
print(i)
Output:
(Vinodini’, ‘XII-F’, 98.7)
(Soundarya’, ‘XII-H’, 97.5)
(Tharani’, ‘XII-F’, 95.3)
(‘Saisri’, ‘XII-G’, 93.8)

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 4.
Explain the different supported by python example.
Answer:
The python supports the set operations such as
Union, Intersection, difference and Symmetric difference.

Union:

  • Union includes all elements from two or more sets.
  • In python, the operator | (pipe line) is used to union of two sets.
  • The function union( ) is also used to join two sets in python.

Example:
Program to Join (Union) two sets using union operator and union function
set_A={2,4,6,8}
set_B={‘A’, ‘B’, ‘C’, ‘D’}
U_set=set_A 1 set_B
print(U _set)
set_ U=set_ A.union(set_ B)
Output:
{2,4,6,8,’A’,’DVC’,’B’}
{‘D’, 2,4, 6,8,’B’,’C’,’A’}

Intersection:

  • Intersection includes the common elements in two sets.
  • The operator & is used to intersect two sets in python.
  • The function intersection() is also used to intersect two sets in python.

Example:
Program to insect two sets using intersection operator and intersection function
set_A={‘A’, 2,4, ‘D’}
set_B={‘A’, ‘B’, ‘C, ‘D’}
print(set_A&set_B)
print( set_A.intersection( set_B)
output:
{‘A’, ‘D’}
{‘A’/D’}

Difference:

  • Difference includes all elements that are in first set (say set A) but not in the second set (say set B).
  • The minus (-) operator is used to difference set operation in python.
  • The function difference() is also used to difference operation.

Example:
Program to difference of two sets using minus operator and difference function
set_A={‘A’, 2,4, ‘D’}
set_B={‘A’, ‘B’, ‘C’, ‘D’}
print(set_A – set B)
print(set_A.difference( set_B))
Output:
{2/4}
{2/4}

Symmetric difference:

  • Symmetric difference includes all the elements that are in two sets (say sets A and B) but not the one that are common to two sets.
  • The caret (^) operator is used to symmetric difference set operation in python.
  • The function symmetric_difference( ) is also used to do the same operation.

Example:
Program to symmetric difference of two
sets using caret operator and symmetric
difference function
set_A={‘A’, 2,4, ‘D’}
set_B={‘A’, ‘B’, ‘C’, ‘D’}
print(set_A ^ set_B)
print( set_A.symmetric_difference(set_B))
Output:
{2,4,’B’,’C’}
{2,4, ‘B’, ‘C’}

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

12th Computer Science Guide Lists, Tuples, Sets and Dictionary Additional Important Questions and Answers

I. Choose the best answer (1 Mark)

Question 1.
A list in python is denoted by ………………………..
(a) [ ]
(b) { }
(c) <>
(d) #
Answer:
(a) [ ]

Question 2.
Which of the following is an ordered collection of values?
a) Tuples
b) List
c) Set
d) Dictionary
Answer:
b) List

Question 3.
Each value of a list is called as
(a) Set
(b) Dictionary
(c) Element
(d) Strings
Answer:
(c) Element

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 4.
In the list, the negative index number begins with
a) 0
b) 1
c) -1
d) 0.1
Answer:
c) -1

Question 5.
To access the list elements in reverse order, ……………………. value has to be given
(a) 0
(b) positive
(c) imaginary
(d) negative
Answer:
(d) negative

Question 6.
Which of the following can be used to access an element in a list?
a) Index value
b) Function
c) Integer
d) Identifier
Answer:
a) Index value

Question 7.
In sim = [4,20,71,89], the negative index value of 20 is
a) 2
b) -2
c) -1
d) -3
Answer:
d) -3

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 8.
Which function is used to set the upper limit in a loop to read all elements of a list?
a) upper ()
b) limit
c) len ()
d) loop ()
Answer:
c) len ()

Question 9.
Which function shows the deleted element as soon as the element is deleted
a) Remove
b) pop
c) Delete
d) erase
Answer:
b) pop

Question 10.
What is the output for the following?
sim = [T’, ‘E’, ‘A’, ‘M’]
for i in sim:
print (sim [2])
a) T
b) A
c) C
d) M
Answer:
b) A

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 11.
…………………….. operator is used to changing the list of elements
(a) =
(b) +
(c) +=
(d) *=
Answer:
(a) =

Question 12.
Which of the following function is used to add more than one element in an existing list?
a) append ()
b) extend ()
c) more ()
d) addmore ()
Answer:
b) extend ()

Question 13.
What is the output for the following,
mylist = [34,45,48]
print(mylist. append (90))
a) 34,45,48,90
b) 90,34,45,48
c) 34,45,90,84
d) 34,45,90,48
Answer:
a) 34,45,48,90

Question 14.
Which of the following function used to include an element in a list at the desired position?
a) append ()
b) extend ()
c) insert ()
d) format ()
Answer:
c) insert ()

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 15.
Write the output,
list = [34, 45, 48]
list.append(90)
(a) [34, 45, 48, 90]
(b) [90, 34, 45, 48]
(c) [34, 90, 45, 48]
(d) [34, 45, 90, 48]
Answer:
(a) [34, 45, 48, 90]

Question 16.
Which function can also be used to delete one or more elements if the index value is not known.
a) push()
b) remove ()
c) delete ()
d) delmore ()
Answer:
b) remove ()

Question 17.
Which of the following command deletes only the elements in the list?
a) erase ()
b) pop ()
c) clear ()
d) remove ()
Answer:
c) clear ()

Question 18.
Creating a Tuple with one element is called a tuple.
a) mono
b) single
c) Singleton
d) Singular
Answer:
c) Singleton

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

II. Answer the following questions (2 and 3 Marks)

Question 1.
Write a note on the nested list?
Answer:
My list = [ “Welcome”, 3.14, 10, [2, 4, 6] ]
In the above example, My list contains another list as an element. A nested list is a list containing another list as an element.

Question 2.
Give short notes on Tuple’s assignment.
Answer:

  • Tuple assignment is a powerful feature in Python.
  • It allows a tuple variable on the left of the assignment operator to be assigned to the values on the right side of the assignment operator.
  • Each value is assigned to its respective variable.

Example;
>>> (a,b;c) = (34, 90, 76)
>>> print(a.b,c)
34 90 76

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 3.
Explain how can you create a set in python with an example.
Answer:

  • A set is created by placing all the elements separated by comma within a pair of curly brackets.
  • The set( ) function can also used to create sets in Python.

Syntax:
Set_ Variable = {E1, E2, E3 En}

Example:
>>> S1 = {1,2,3,’A’,3.14}
>>> print(S1)
{1, 2, 3, 3.14, ‘A’}
>>> S2={1,2,2,’A’,3.14}
>>> print(S2)
{1,2,’A’, 3.14}

Question 4.
What is meant by Reverse Indexing?
Answer:
Python enables reverse or negative indexing for the list elements. Thus, python lists indexes in the opposite order. The python sets -1 as the index value for the last element in list and -2 for the preceding element and so on. This is called Reverse Indexing.

Question 5.
Explain Dictionary Comprehension.
Answer:
In Python, comprehension is another way of creating a dictionary.

Syntax: ,
Diet = {expression for variable in sequence
[if condition]}
The if condition is optional and if specified, only those values in the sequence are evaluated using the expression which satisfy the condition.

Example:
Diet = {x : 2 * x for x in range(1,10)}
Output:
{1: 2,2: 4, 3: 6,4: 8,5:10,6:12,7:14,8:16,9:18}

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 6.
What are the four collections of data types in a python programming language?
Answer:
Python programming language has four collections of data types such as List, Tuples, Set, and Dictionary.

Question 7.
Differentiate clear( ) and del in list?
Answer:
The function clear( ) is used to delete all the elements in list, it deletes only the elements and retains the list. Remember that, the del statement deletes entire list.

Question 8.
Differentiate append () and extend () function..
Answer:

append () extend ()
append() function is used to add a single element extend() the function is used to add more than one element to an existing list.

Question 9.
What will be the output of the following snippet?
Answer:
alpha=list(range(65,70))
for x in alpha:
print(chr(x), end=’\t’)
Output:
A B C D E

Question 10.
What will be the output of the following code?
Answer:
list=[3** x for x in range (5)]
print(list)
Output: [1, 3, 9, 27,81]

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 11.
How to delete elements from the list.
Answer:

  • There are two ways to delete an element from a list viz. del statement and remove() function.
  • del statement is used to delete known elements whereas removing] ) the function is used to delete elements of a list if its index is unknown.
  • The del statement can also be used to delete the entire list.

Syntax:
To delete a particular element:
del List [index of an element]
To delete multiple elements:
del List [index from : index to]
To delete entire list:
del List Syntax: remove ()
# to delete a particular element
List. remove (element)

Question 12.
Write notes on pop () and clear () function.
Answer:
Pop() function:

  • pop () function can also be used to delete an element using the given index value.
  • pop () function deletes and returns the last element of a list if the index is not given.

Syntax : List.pop(index of an element)
clear () function:

  • clear () function is used to delete all the elements in list.
  • It deletes only the elements and retains the list.

Syntax: List.clear()

Question 13.
Define Tuple in python with syntax?
Answer:

  • Tuples consists of a number of values separated by comma and enclosed within parentheses.
  • Tuple is similar to list, values in a list can be changed but not in a tuple.

Syntax:
#Empty tuple:
Tuple_Name = ()
#Tuple with n number elements:
Tuple_Name = () (E1, E2, E3, En)
#Elements of a tuple without parenthesis:
Tuple_Name = E1, E2, E3, …….. En

Question 14.
Write notes on tuple () function? Give example.
Answer:

  • The tuple ( ) function is used to create Tuples from a list.
  • When you create a tuple from a list, the elements should be enclosed within square brackets.

Example:
MyTup3 = tuple( [23,45,90])
>>> print(MyTup3)
(23,45,90)

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 15.
How to create a set using a list or tuple? Give an example.
Answer:

  • A list or Tuple can be converted as set by using set() function.
  • First a List or Tuple can be created then, substitute its variable within set() function as argument.

Example:
MyList= [2,4,6,8,10]
MySet=set(MyList)
print(MySet)
Output:
[2,4, 6,8,10}

Question 16.
Give notes on Dictionary in Python.
Answer:

  • Dictionary is a mixed collection of elements.
  • Unlike other collection data types such as a list or tuple, the dictionary type stores a key along with its element.
  • The keys in a Python dictionary is separated by a colon ( : ) while the commas work as a separator for the elements.
  • The key-value pairs are enclosed with curly braces {}.

Question 17.
Give notes on len() function of a list with
an example.
Answer:

  • The len() function in Python is used to find the length of a list. (i.e., the number of elements in a list).
  • Usually, the len() function is used to set the upper limit in a loop to read all the elements of a list.
  • If a list contains another list as an element, len() returns that inner list as a single element.

Example:
Accessing single element
>>> MySubject = [‘Tamil’, ‘English’,
‘Comp’, ‘Science’, ‘Maths’]
>>>len(MySubject)
4

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 18.
Define List comprehensions.
Answer:
List comprehension is the simplest way of creating a sequence of elements that satisfy a certain condition.
Syntax:
List = [ expression for variable in range ]

Example:
Generating squares of first 10 natural numbers using the concept of List comprehension.
>>> squares = [x ** 2 for x in range(l, 11) ]
>>> print (squares)
Output:
[1,4,9,16,25,36,49,64,81,100]

Question 19.
What is the difference in between List and Tuple.
Answer:

List Tuple
1 In a list, elements are defined within square brackets. Tuples consists of a number of values separated by comma and enclosed within parentheses even defined without parenthesis
2 Values in a list can be changed Values in a tuple cannot be changed

Question 20.
Write execution table for the following Python code.
Answer:
Marks=[10/ 20, 30,40, 50]
i = 0
sum = 0
while i < 4
sum+=Marks[i]
i+=1

Execution Table:

S.No i i<4 Marks Sum i + 1
1 0 0<4 (True) 10 10 1
2 1 1<4 (True) 20 30 2
3 2 2< 4 (True) 30 60 3
4 3 3<4 (True) 40 . 100 4
5 4 4<4 (False)

Question 21.
What will be the output of the following snippet?
Answer:
Mydict={chr(x):x for x in range(97,102)} Print(Mydict)
Output:
{‘a’: 97, ‘b’: 98, ‘c’: 99, ‘d’: 100, ‘e’: 101}

Question 22.
What will be the output of the following snippet?
Answer:
set_A = {‘A’. 2,4, ‘D’}
Set_B = {‘A’, ‘B’, ‘C’, ‘D’}
print(set_A & set_B)
Output:
{‘A’, ‘D’}

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

III. Answer the following questions (5 Marks)

Question 1.
Explain how to access all elements of a list using while ioop with suitable example
Answer:

  • Loops are used to access all elements from a list. 1PTA – 61
  • The initial value of the ioop must be zero.
  • Zero is the beginning index value of a list.

Example:
Marks = [10, 23,41,75] .
i=O
whilei<4:
print (Marks[i])
i=i+1

Output:
10
23
41
75

  • In the above example, Marks list contains four integer elements i.e., 10, 23, 41, 75. Each element has an. index value from 0. The index value of the elements are 0,1, 2, 3 respectively.
  • Here, the while loop is used to read all the elements. The initial value of the loop is zero, and the test condition is < 4, as long as the test condition is true, the loop executes and prints the corresponding output.
  • During the first iteration, the value of i is zero, where the condition is true.
  • Now, the following statement print (Marks [i] gets executed and prints the value of Marks [0] element ie. 10.
  • The next statement i = i + 1 increments the value of i from 0 to 1.
  • Now, the flow of control shifts to the while statement for (vi) checking the test condition.
  • The process repeats to print the remaining elements of Marks list until the test condition of while loop becomes false.

The following table shows that the execution of loop and the value to be print.

Iteration i while
i<4
print
(Marks[i])
i=i +1
1 0 0< 4 True Marks [0] =10 0+1=1
2 1 1 < 4 True Marks [1]=23 1+1=2
3 2 2 < 4 True Marks [2] =41 2+1=3
4 3 3 < 4 True Marks [3]=75 3+1=4
5 4 4 < 4 False

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 2.
Write a python program using list to read marks of six subjects and to print the marks scored in each subject and show the total marks.
Python program to read marks of six subjects and to print the marks scored in each subject and show the total marks
Answer:
marks=[ ]
subjects=[‘Tamir, ‘English’, ‘Physics’, ‘Chemistry’, ‘Comp. Science’, ‘Maths’]
for i in range(6):
m=int(input(“Enter Mark = “))
marks.append(m)
for j in range(len(marks)):
print(“{ }. { } Mark= { } “.format(jl+,subjects[j],marks[j]))
print(“Total Marks = “, sum(marks))
Output:

  • Enter Mark = 45
  • Enter Mark = 98
  • Enter Mark = 76
  • Enter Mark = 28
  • Enter Mark = 46
  • Enter Mark = 15
    1. Tamil Mark = 45
    2. English Mark = 98
    3. Physics Mark = 76
    4. Chemistry Mark = 28
    5. Comp. Science Mark = 46
    6. Maths Mark = 15
    7. Total Marks = 308

Question 3.
Explain remove, pop () and clear () used in list with an example.
Asnwer:
remove():
The remove() function can also be used to delete one or more elements if the index value is not known.
Syntax: List.remove(element) # to delete a particular element
Example: >>>MyList = [12,89,34/Kannan’, ‘Gowrisankar’, ‘Lenin’]
>>> print(MyList)
[12,89,34, ‘Karman’, ‘Gowrisankar’, ‘Lenin’)
>>>MyList.remove(89)
>>>print(MyList)
[12, 34, ‘Karman’, ‘Gowrisankar’, ‘Lenin’]

pop ():

  • pop() function can also be used to delete an element using the given index value.
  • Pop() function deletes and returns the last element of a list if the index is not given.
  • pop() function is used to delete a particular element using its index value, as soon as the element is deleted.
  • The pop() function shows the element which is deleted, pop ( ) function is used to delete only one element from a list.

Syntax:
List, pop (index of an element)

Example:
>>> MyList.pop(1)
34
>>> print (My List)
[12, ‘Kannan’, ‘Gowrisankar’, ‘Lenin’)

clear ():

  • The clear() function is used to delete all the elements in list, it deletes only the elements and retains the list.
  • clear () function removes only the elements and retains the list.
  • When you try to print the list which is already cleared, an empty square bracket is displayed without any elements, which means the list is empty.

Syntax:
List.clear()
Example:
>>>MyList.clear()
>>> print(MyList)
[]

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 4.
Explain the following functions used in list function with an example. max(), min(),sum()
Answer:

Function

Description Syntax

Example

max() Returns the maximum value in a list. max (list) MyList= [21,76,98,23] print(max(MyList))
Output:
98
min() Returns the minimum value in a list. min(list) My List= [21,76,98,23] print (min(My List))
Output:
21
sum() Returns the sum of values in a list. sum(list) My List= [21,76,98,23] print(sum(MyList))
Output:
218

Question 5.
Explain the following functions used in list function with an example. 1. copy () 2. count () 3. index () 4. reverse () 5.sort ()
Answer:

Function Description Syntax Example
copy () Returns a copy of the list List.copy() MyList=[12,12, 36] x = MyList.copy() print(x)
Output:
[12,12, 36]
count () Returns the number of similar elements present in the list. List.

count(value)

MyList=[36,12,12] x = MyList.count(12) print(x)
Output:
2
index () Returns the index value of the first recurring element List.

index (element)

MyList=[36,12,12] x = MyList.index(12) print(x)
Output:
0
reverse () Reverses the order of the element in the list. List.reverse() MyList=[36,23,12] MyList.reverse() print(MyList)
Output:
[12,23,36]
sort () Sorts the element in list List.sort(reverse=True | False, key=myFunc)
Both arguments are optional If the reverse is set as True, list sorting is in descending order.
Ascending is the default.
Key=myFunc; “myFunc” – the name of the user defined function that specifies the sorting criteria.
Note: sort() will affect the original list.
My List= [‘Thilothamma, Tharani’, ‘Anitha, ‘SaiSree, ‘Lavanya’] MyList.sort() print(MyList)
MyList.sort(reverse=True)
print(MyList)
Output:
[‘Anitha7, ‘Lavanya7, ‘SaiSree’, ‘Tharani7, ‘Thilothamma7]
[‘Thilothamma7, ‘Tharani7, ‘SaiSree7, 7Lavanya7, ‘Anitha’]

Question 6.
Answer:
6. What will be the output of the following Python program?
A{x*3 for x in range (1,6))
B{y**2 for y in range (1,10,2))
print(A)
print(B) .
print(A | 13)
print(A-B)
print(A&B)
print (A ^ B)
Output:
A={x*3 for x in range(1,6)}
B={y**2 for y in range(1,10,2)}
print(A) → {3, 6, 9, 12, 15}
print(B) → {1, 9, 25, 49, 81}
print (A I B) → {1, 3, 6, 9, 12, 15, 25, 49, 81}
print(A-B) → {3, 6, 12, 15}
print(A&B) → {9}
print(A”B) → {1, 3, 6, 12, 15, 25, 49, 81}

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 7.
What will be the output of the following Python program?
N = [ ]
for x in range(l,ll):
N.append(x)
Num=tuple(N)
print(Num)
for index, i in enumerate(N): if(i%2==l): .
del N[index]
print(N)
Output:
[2,4, 6, 8,10]

Question 8.
What will be the output of the following Python program?
A ={x*3 for x in range (1,6)}
B={y**2 for y in range (1,10,2)}
Print(A)
Print(B)
Print(A | B)
Print(A-B)
Print(A&B)
Print(AAB)
Output:
>>> A={x*3 for x in range(l,6)}
>>> B={y**2 for y in range(1,10,2)}
>>> print(A)
{3, 6, 9,12,15}
>>>print(B)
{1, 9,81,49,25}
>>> print(A | B)
{1,3, 6, 9,12,15, 81,49, 25}
>>> print(A-B)
{3,12, 6,15}
>>> print(A&B)
{9}
>>> print(A^B)
{1,3, 6,12,15, 81, 25,49}

Samacheer Kalvi 12th Computer Science Guide Chapter 9 Lists, Tuples, Sets and Dictionary

Question 9.
Write a program using a function that returns the area and circumference of a circle whose radius is passed as an argument two values using tuple assignment. Program using a function that returns the area and circumference of a circle whose radius is passed as an argument. Assign two values using tuple assignment:
Answer:
pi = 3.14
def Circle(r):
return (pi*r*r, 2*pi*r)
radius = float(input(“Enter the Radius! “))
(area, circum) = Circle(radius)
print (“Area of the circle = “, area)
print (“Circumference of the circle = “, circum)
Output:
Enter the Radius: 5
Area of the circle = 78.5
Circumference of the circle = 31.400000000000002