Lists, Tuples, Sets and Dictionary: book back answers
23 solved questions from the end of this chapter, Class 12 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.
← Notes and quiz for Lists, Tuples, Sets and DictionaryChoose the best answer (1 Marks)
Pick odd one in connection with collection data type
- (a) List
- (b) Tuple
- (c) Dictionary
- (d) Loop
Try it, then show the answerAnswer
(d) Loop
List, tuple and dictionary are collection data types; a loop is not one of them.
From the book: “Python programming language has four collections of data types such as List, Tuples, Set and Dictionary.”
Let list1=[2,4,6,8,10], then print(List1[-2]) will result in
- (a) 10
- (b) 8
- (c) 4
- (d) 6
Try it, then show the answerAnswer
(b) 8
Negative indexing counts backward from the end. Index -2 selects the second-to-last value, 8.
From the book: “python sets -1 as the index value for the last element in list and -2 for the preceding element”
Which of the following function is used to count the number of elements in a list?
- (a) count()
- (b) find()
- (c) len()
- (d) index()
Try it, then show the answerAnswer
(c) len()
The len() function returns the number of elements in a list.
From the book: “The len() function in Python is used to find the length of a list. (i.e., the number of elements in a list).”
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]
Try it, then show the answerAnswer
(c) [10,20,35,40,50]
List indices begin at zero, so index 2 refers to the third element. Assignment replaces 30 with 35.
From the book: “A list element or range of elements can be changed or altered by using simple assignment operator =.”
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]
Try it, then show the answerAnswer
(b) [17,23,41,10,32]
append() adds a single element to the end of the existing list.
From the book: “append() function is used to add a single element”
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()
Try it, then show the answerAnswer
(c) extend()
extend() adds multiple elements to an existing list.
From the book: “append() function is used to add a single element and extend() function is used to add more than one element to an existing list.”
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]
Try it, then show the answerAnswer
(b) [0,1,4,9,16]
range(5) generates 0 through 4; squaring those values gives 0, 1, 4, 9 and 16.
From the book: “The range() is a function used to generate a series of values in Python.”
What is the use of type() function in python?
- (a) To create a Tuple
- (b) To know the type of an element in tuple.
- (c) To know the data type of python object.
- (d) To create a list.
Try it, then show the answerAnswer
(c) To know the data type of python object.
The type() function reports the data type of a Python object.
From the book: “Type ( ) function is used to know the data type of a python object.”
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.
Try it, then show the answerAnswer
(d) The extend() function is used in tuple to add elements in a list.
The chapter describes extend() as a function for adding elements to an existing list, not a tuple.
From the book: “append() function is used to add a single element and extend() function is used to add more than one element to an existing list.”
Let setA = {3,6,9}, setB = {1,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}
- (d) {1,3,6,9}
Try it, then show the answerAnswer
(d) {1,3,6,9}
The | operator takes the union of the sets, including each distinct element from either set.
From the book: “In python, the operator | is used to union of two sets.”
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
Try it, then show the answerAnswer
(a) Symmetric difference
Symmetric difference contains the elements in either set but excludes elements common to both.
From the book: “It 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 keys in Python, dictionary is specified by
- (a) =
- (b) ;
- (c) +
- (d) :
Try it, then show the answerAnswer
(d) :
A colon separates each dictionary key from its value.
From the book: “The keys in a Python dictionary is separated by a colon ( : )”
Part - II — Answer the following questions (2 Marks)
What is List in Python?
Try it, then show the answerAnswer
A list in Python is an ordered collection of values enclosed within square brackets. Its elements can be of different types and can be changed.
Each value in a list is an element, and list elements can be replaced, added or removed.
From the book: “It is an ordered collection of values enclosed within square brackets [ ].”
How will you access the list elements in reverse order?
Try it, then show the answerAnswer
Use negative index values, starting with -1 for the last element, -2 for the preceding element, and so on.
Negative indexing counts backward from the end of the list.
From the book: “python sets -1 as the index value for the last element in list and -2 for the preceding element”
Differentiate del with remove() function of List.
Try it, then show the answerAnswer
del deletes list elements when their index is known, and it can delete a range of elements or the entire list. remove() deletes an element by its value when its index is unknown.
del uses an index or index range, while remove() names the element to be removed.
From the book: “del statement is used to delete elements whose index is known whereas remove() function is used to delete elements of a list if its index is unknown.”
Write the syntax of creating a Tuple with n number of elements.
Try it, then show the answerAnswer
Tuple_Name = (element1, element2, ..., elementn)
A tuple contains values separated by commas and may be enclosed within parentheses.
From the book: “Tuples consists of a number of values separated by comma and enclosed within parentheses.”
What is set in Python?
Try it, then show the answerAnswer
A set is a mutable, unordered collection of elements without duplicates.
Repeated values are not retained in a Python set.
From the book: “A Set is a mutable and an unordered collection of elements without duplicates.”
Part - III — Answer the following questions (3 Marks)
What are the difference between list and Tuples?
Try it, then show the answerAnswer
A list is mutable, while a tuple is immutable. List elements are enclosed in square brackets, whereas tuple elements are enclosed in parentheses.
Thus, list elements can be changed but tuple elements cannot be changed.
From the book: “The elements of a list are changeable (mutable) whereas the elements of a tuple are unchangeable (immutable)”
Write a shot note about sort().
Try it, then show the answerAnswer
sort() sorts the elements in a list. By default it sorts in ascending order; setting reverse=True sorts in descending order. Sorting affects the original list.
The key argument is optional and can specify a user-defined sorting criterion.
From the book: “Note: sort( ) will affect the original list.”
What will be the output of the following code? list = [2**x for x in range(5)] print(list)
Try it, then show the answerAnswer
[1, 2, 4, 8, 16]
range(5) gives x values 0 to 4. Evaluating 2**x for these values gives 1, 2, 4, 8 and 16.
From the book: “The range() is a function used to generate a series of values in Python.”
Explain the difference between del and clear() in dictionary with an example.
Try it, then show the answerAnswer
del Dict['Mark1'] deletes the specified key and its value. Dict.clear() deletes all elements but retains the dictionary; del Dict removes the dictionary itself.
For example, del Dict['Mark1'] removes only the Mark1 entry, while Dict.clear() leaves an empty dictionary.
From the book: “The clear() function is used to delete all the elements in a dictionary. To remove the dictionary, you can use del keyword with dictionary name.”
List out the set operations supported by python.
Try it, then show the answerAnswer
Union, intersection, difference and symmetric difference.
These operations combine sets, find shared elements, find one-sided elements, or find elements not shared by both sets.
From the book: “the python is also supports the set operations such as Union, Intersection, difference and Symmetric difference.”
What are the difference between List and Dictionary?
Try it, then show the answerAnswer
A list is an ordered set of elements, accessed using index values. A dictionary matches keys with values, and its key represents the index used to access an element.
A dictionary key may be a number or a string.
From the book: “List is an ordered set of elements. But, a dictionary is a data structure that is used for matching one element (Key) with another (Value).”