Home | | Computer Science 12th Std | Python Lists, Tuples, Sets And Dictionary : Book Back Questions and Answers

Python - Python Lists, Tuples, Sets And Dictionary : Book Back Questions and Answers | 12th Computer Science : Chapter 9 : Python Modularity and OOPS : Lists, Tuples, Sets And Dictionary

Chapter: 12th Computer Science : Chapter 9 : Python Modularity and OOPS : Lists, Tuples, Sets And Dictionary

Python Lists, Tuples, Sets And Dictionary : Book Back Questions and Answers

Choose the best answer, Answer the following questions

Computer Science : Python Modularity and OOPS : Lists, Tuples, Sets And Dictionary

Evaluation


Part – I

Choose the best answer           (1 Marks)


1.Pick odd one in connection with collection data type

(a) List

(b) Tuple

(c) Dictionary

(d) Loop


2.Let list1=[2,4,6,8,10], then print(List1[-2]) will result in    

(a) 10

(b) 8

(c) 4

(d) 6


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()


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]


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]


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()


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]


8. What is the use of type() function in python? x

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.


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.


10.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}


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


12.The keys in Python, dictionary is specified by

(a) =

(b) ;

(c)+

(d) :


Part – II

Answer the following questions (2 Marks)


1. What is List in Python?

Ans. (i) A list in Python is known as a “sequence data type” like strings. It is an ordered collection of values enclosed within square brackets [].

(ii) Each value of a list is called as element. It can be of any type such as numbers, characters, strings and even the nested lists as well.


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

Ans. Python enables reverse or negative indexing for the list elements. Thus, python lists index in 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 as Reverse Indexing.


3. What will be the value of x in following python code?

List1=[2,4,6[1,3,5]]

x=len(List1)

Ans. x = 4.


4. Differentiate del with remove( ) function of List.

Ans.


del function

(1) del statement is used to delete known elements whereas

(ii)  The del statement can also be used to delete entire list.

remove () function

(i) remove() function is used to delete elements of a list if its index is unknown.

 (ii) The remove () function can be used to delete one or more elements if the index value is not known.


5. Write the syntax of creating a Tuple with n number of elements.

Ans. Syntax:

Tuple_Name = tuple( [list elements] )


6. What is set in Python?

Ans. (i) In Python, a Set is another type of collection data type. A Set is a mutable and an unordered collection of elements without duplicates.

(ii) That means the elements within a set cannot be repeated. This feature used to include membership testing and eliminating duplicate elements.


Part – III

Answer the following questions       (3 Marks)


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

Ans. (i) 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.

(ii) The elements of a list are enclosed within square brackets. But, the elements of a tuple are enclosed by parenthesis.

(iii) Iterating tuples is faster than list.


2. Write a shot note about sort( ).

Ans. Sort (): Sorts the element in list

Both arguments are optional

(i) If reverse is set as True, list sorting is in descending order.

(ii) Ascending is default.

(iii)  Key=myFunc; “myFunc” - the name of the user defined function that specifies the sorting criteria.

(iv) sort() will affect the original list.

Example:

List.sort(reverse=True|False, key=myFunc)

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']


3. What will be the output of the following code?

list = [2**x for x in range(5)]

print(list)

Ans. Output: [1, 2,4, 8,16]


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

Ans. In Python dictionary, del keyword is used to delete a particular element. The clear() function is used to delete all the elements in a dictionary.


5. List out the set operations supported by python.

Ans. The python supports the set operations such as Union, Intersection, difference and Symmetric difference.


6. What are the difference between List and Dictionary?

Ans.


List

(i) List is an ordered set of elements

(ii) The index values can be used to access a particular element.

(iii) Lists are used to look up a value whereas.

Dictionary

(i) But, a dictionary is a data structure that is used for matching one element (Key) with another (Value).

(ii) The remove function can be used to delete one or more elements if the index value is not known.

(iii) dictionary is used to take one value and look up another value.


Part – IV

Answer the following questions       (5 Marks)


1. What the different ways to insert an element in a list. Explain with suitable example.

Ans. Adding more elements in a list:

(i) In Python, append( ) function is used to add a single element and extend() function is used to add more than one element to an existing list.

(ii) Syntax:

List.append (element to be added)

List.extend ( [elements to be added])

(iii) In extend( ) function, multiple elements should be specified within square bracket as arguments of the function.

(iv) Example :

>>> Mylist=[34,45, 48]

>>> Mylist.append(90)

>>>> print(Mylist)

[34, 45, 48, 90]

(v) In the above example, Mylist is created with three elements. Through >>> Mylist. append(90) statement, an additional value 90 is included with the existing list as last element, following print statement shows all the elements within the list MyList.

Inserting elements in a list:

(i) Append( ) function in Python is used to add more elements in a list. But, it includes elements at the end of a list.. To include an element at your desired position, use insert ( ) function. The insert( ) function is used to insert an element at any position of a list.

(ii) Syntax:

List.insert (position index, element)

(iii) 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']


2. What is the purpose of range( )? Explain with an example.

Ans. The range() is a function used to generate a series of values in Python. Using 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,

(i) start value - beginning value of series. Zero is the default beginning value.

(ii) end value - upper limit of series. Python takes the ending value as upper limit - 1.

(iii) step value - It is an optional argument, which is used to generate different interval of values.

Generating first 10 even numbers :

for x in range (2,11,2):

print(x)

Output:

2

4

6

8

10

Creating a list with series of values :

(i) Using the range( ) function, a list can be created with series of values. To convert the result of range() function into list, one more function called list is needed( ). The list(). function makes the result of range() as a list.

(ii) Syntax: List_Varibale = list ( range () )

(iii) Example :

>>> Even_List = list(range(2,11,2))

>>> print(Even_List)

[2,4, 6, 8,10]

(iv) 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.

Similarly, we can create any series of values using range( ) function. The following example explains how to create a list with squares of 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)


3. What is nested tuple? Explain with an example.

Ans.

Nested Tuples :

(i) In Python, a tuple can be defined inside another tuple; called 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.

(ii) Example :

Toppers = (("Vinodini", "XII-F", 98.7), ("Soundarya", "XII-H", 97.5), ("Tharani", "XII-F", 95.3), ("Saisri", "XII-G", 93.8))

for i in Toppers: print(i)

(iii) Output:

('Vinodini', 'XII-F, 98.7)

('Soundarya', 'XII-H', 97.5)

('Tharani', 'XII-F', 95.3)

('Saisri', 'XII-G', 93.8)


4. Explain the different set operations supported by python with suitable example.

Ans. The python supports the set operations such as Union, Intersection, difference and Symmetric difference.

Union:

It includes all elements from two or more sets


(i) In python, the operator | is used to union of two sets. The function union() is also used to join two sets in python.

(ii) Example : Program to Join (Union) two  sets using union operator

set_A={2,4,6,8}

set_B={'A', 'B', 'C', ’D'}

U_set=set_A | set_B

 print(U_set)

Output:

{2,4,6, 8, 'A', 'D', 'C', 'B'}

(iii) Example : Program to Join (Union) two sets using union function set_A={2,4,6,8}

set_B={'A', 'B', 'C', 'D'}

set_U=set_A.union(set_B)

print(set_U)

Output: {'D', 2,4, 6, 8, 'B', 'C', 'A'}

Intersection :

(i) It includes the common elements in two sets


(ii) The operator & is used to intersect two sets in python. The function intersection ( ) is also used to intersect two sets in python.

(iii) Example : Program to insect two sets using intersection operator

set_A={'A', 2,4, 'D'}

set_B={'A', 'B', 'C', 'D'}

print(set_A & set_B)

Output:

{'A','D'}

(iv) Example : Program to insect two sets using intersection function

set_A={'A', 2,4, 'D'}

set_B={'A', 'B', 'C', 'D'}

print(set_A.intersection(set_B))

Output:

{'A', 'D']

Difference:

(i) It includes all elements that are in fi rst set (say set A) but not in the second set (say set B)


(ii) The minus (-) operator is used to difference set operation in python. The function difference( ) is also used to difference operation.

(iii) Example : Program to difference of two sets using minus operator

set_A={'A', 2,4, 'D']

 set_B={'A', 'B', 'C', 'D'J]

print(set_A - set_B)

Output: {2,4}

(iv) Example: Program to difference of two sets using difference function set_A={'A', 2, 4, ’D']

set_B={'A', 'B', 'C', 'D’]

print(set_A.diff erence(set_B))

Output: {2,4}

Symmetric difference:

(i)  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.


(ii) The caret (^) operator is used to symmetric difference set operation in python. The function symmetric_difference( ) is also used to do the same operation.

(iii) Example: Program to symmetric difference of two sets using caret operator set_A={'A', 2,4, ’D’}

set_B={'A', ’B’, 'C', 'D'}

print (set_A ^ A set_B)

Output:

{2,4,'B', 'C'}

(iv) Example : Program to difference of two sets using symmetric difference function

set_A={'A', 2,4, D’}

set_B={'A', 'B', 'C', 'D'}

 print(set_A.symmetric_difference(set_B))

Output:

{2,4, 'B', 'C'}



 

HANDS ON EXPERIENCE

 

1. Write a program to remove duplicates from a list.

Ans.

mylist = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

r= []

for i in mylist:

if i not in res:

res. append (i)

 print (res)

 

2. Write a program that prints the maximum value in a Tuple.

Ans. tuple 1 = (5,17,15,20,7, 3)

 print (“Maximum value”, max (tuple 1))

3. Write a program that finds the sum of all the numbers in a Tuples using while loop.

Ans. li = [15,25,17]

s = 0

i = 0

while i < len (i):

s = s + li [i]

 i + = 1

print (s)

 

4.Write a program that finds sum of all even numbers in a list.

Ans. list = [1, 2, 3, 4, 5, 6, 7, 8, 9,10]

e = 0

for i in list:

if i % 2 = = 0:

e = e + i

print (“Sum of even number”, e)

 

5. Write a program that reverse a list using a loop.

Ans. list = [1, 2, 3, 4, 5]

i = -l

while i > = - 5:

print (list [i])

i=i+-l

 

6. Write a program to insert a value in a list at the specified location.

Ans. list = [1, 2, 4, 5]

list insert (2, 3)

print (list)

 

7. Write a program that creates a list of numbers from 1 to 50 that are either divisible by 3 or divisible by 6.

Ans. a = [ ]

for i in range (1, 51):

if i % 3 = = 0 or i % 6 = = 0

(a) append (i)

print (a)

 

8. Write a program to create a list of numbers in the range 1 to 20. Then delete all the numbers from the list that are divisible by 3.

 Ans. a = [ ]

for x in range (1,21):

(a) append (i)

for n, i enumerate (a):

if (i % 3 = = 0):

del a [n]

print (a)

 

9. Write a program that counts the number of times a value appears in the list. Use a loop to do the same.

Ans. list = [8, 6, 8, 10, 8, 20, 10, 8, 8]

x = 8

x = li. count (x)

 print (x)

 

10. Write a program that prints the maximum and minimum value in a dictionary.

Ans. diet = { ‘a’ : 10, ‘b’: 20, ‘c’: 5}

v = diet. values ()

print (“Maximum = ”, max (v))

print (“Minimum = ”, min (v)) 

Tags : Python , 12th Computer Science : Chapter 9 : Python Modularity and OOPS : Lists, Tuples, Sets And Dictionary
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 9 : Python Modularity and OOPS : Lists, Tuples, Sets And Dictionary : Python Lists, Tuples, Sets And Dictionary : Book Back Questions and Answers | Python


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.