Engineering Python : UNIT IV : Lists, Tuples, Dictionaries : Anna University Two Marks Questions & Answers
Two Marks Questions with Answers
Q.
1 What are Lists in Python ? Give example.
AU : Jan.-18
Ans. : Lists is a sequence of values enclosed
within a square bracket. For example
[10,20,30,40]
Q.
2 What is the use of * operator in association with List in Python ?
Ans. : The * operator is used to repeat
the list number of times. For example [1,2,3]*3 will give [1,2,3,1,2,3,1,2,3]
Q.
3 What is the purpose of extend method in python ?
Ans. : The extend method takes new list as
argument and append it with old list. Thus using extend two lists can be
combined.
Q.
4 Suppose you have given a list a=[1,2,3,4], how will you iterate through this
list and print each element of it ?
Ans. : Following Python code is used for
iterating through the list and display of the numbers
>>> a = [10,20,30,40]
>>>
for i in range(len(a)):
print(a[i])
Q.
5 What is mutability property ? List is mutable or immutable.
Ans. : Mutability is a property indicating
whether we can change the element of the sequence or not. List is mutable. That
means we can change the element of list.
Q.
6 What is aliasing?
Ans. : An object with more than one
reference has more than one name, so we say that the object is aliased. For
example - >>> x=[10,20,30] >>> y=x >>> y is x True
Q.
7 What is tuple ? How literals of type tuple are written give example.
AU
: Jan-18
Ans. : Tuple is a sequence of values. It
is similar to list but there lies difference between tuple and list.
Examples
of tuples
T1=
(10,20,30,40)
T2
= ('a','b','c','d')
Q.
8 Differentiate between tuple and list. AU : Jan-18
Ans. :
Tuple
: List
Tuple
use parenthesis - List use square brackets
Tuples
can not be change - Lists can be changed.
Q.
9 How can we pass variable number of arguments to tuple ?
Ans. : In python, it is possible to have
variable number of arguments. In that case the parameter name begins with *.
Then these variable number of arguments are gathered into a tuple. For example –
def
student_Info(*args):
print(args)
Q.
10 What is dictionary?
Ans. : In Python, dictionary is unordered
collection of items. These items are in the form of key-value pairs.
•
The dictionary contains the collection of indices called keys and collection of
values.
•
Each key is associated with a single value.
•
The association of keys with values is
called key-value pair or item.
•
For example
My_dictionary
= {1:'AAA',2:'BBB’,3:'CCC'}
Q.
11 What is the difference between list, tuple and dictionary?
Ans. : A list can store a sequence of objects
in a certain order such that we can iterate over the list. List is a mutable
type meaning that lists can be modified after they have been created.
A
tuple is similar to a list except it is immutable. In tuple the elements are
enclosed with in parenthesis. Tuples have structure, lists have order.
A
dictionary is a key-value store. It is not ordered.
Q.
12 Explain what is range() function and how it is used in lists ?
Ans. : The range function returns an immutable
sequence object of integers between the given start integer to the stop integer.
range(start,stop,[step])
>>>
for I in range(1,10,2):
print(i,end="")
13579
Q.
13 How lists are updated in Python ?
Ans. : The append() method is used to add
elements to a list.
Syntax
: list.append(obj) List=[123,'VRB']
List.append(2017)
Print(“Updated
List:”,List)
Output:
Updated List: [123,'VRB',2017]
Q.14
Write a few methods that are used in Python Lists.
Ans. :
a)
append()- add an element to end of list
b)
insert()- insert an item at the defined index
c)
remove()- removes an item from the list
d)
clear()- removes all items from the list
e)
reverse()- reverse the order of items in the list
Q.
15 What are the advantages of Tuple over List ?
Ans. :
•
Tuple is used for heterogeneous data types and list is used for homogeneous
data types.
•
Since tuple are immutable, iterating through tuple is faster than with hist.
•
Tuples that contain immutable elements can be used as key for dictionary.
•
Implementing data that doesn't change as a tuple remains write-protected
Q.
16 What is indexing and negative indexing in Tuple ?
Ans. : The index operator is used to access an
item in a tuple where index starts from 0.
Python
also allows negative indexing where the index of – 1 refers to the last item, -
2 to the second last item and so on.
>>>
my_tuple=('p','y','t','h','o','n')
>>>
print(my_tuple[5] ) n
>>>
print(my_tuple(- 6)) p
Q.
17 What is the output of print tuple[1:3] if tuple = ('abcd', 786 , 2.23,
'john', 70.2) ?
Ans. : In the given command, tuple[1:3] is
accessing the items in tuple using indexing.
It
will print elements starting from 2nd till 3rd. Output will be (786, 2.23).
Q.
18 What are the methods that are used in Python Tuple ?
Ans. : Methods that add items or remove items
are not available with tuple. Only the following two methods are available :
a)
count(x)- returns the number of items that is equal to x
b)
index(x)- returns index of first item that is equal to x
Q.
19 Is tuple comparison possible? Explain how with example.
Ans. : The standard comparisons
('<','>','< =",'>=,=') work exactly the same among tuple
objects. The tuple objects are compared element by element.
>>>
a = (1,2,3,4,5)
>>>
b = (9,8,7,6,5)
>>>a<
b
True
Q.
20 What are the built-in functions that are used in tuple ?
Ans. :
•
all()- returns true if all elements of the tuple are true or if tuple is empty
•
any()- returns true if any element of tuple is true
•
len()- returns the length in the tuple
•
max()- returns the largest item in tuple
•
min()- returns the smallest item in tuple
•
sum()- returns the sum of all elements in tuple
Q.
21 What is the output of print tuple + tinytuple if tuple = ('abcd',786 , 2.23,
'john', 70.2 ) and tinytuple = (123, 'john') ?
Ans. : It will print concatenated tuples.
Output will be ('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john').
Q.
22 Explain what is dictionary and how it is created in Python ?
Ans. : Dictionaries are similar to other
compound types except that they can use any immutable type as an index. One way
to create a dictionary is to start with the empty dictionary and add elements.
The empty dictionary is denoted {}:
>>>
eng2sp = {}
>>>
eng2spl'one'] = 'uno'
>>>
eng2spl'two'] = 'dos'
Q.
23 What is meant by key-value pairs in a dictionary?
Ans. : The elements of a dictionary appear in
a comma-separated list. Each entry contains an index and a value separated by a
colon. In a dictionary, the indices are called keys, so the elements are called
key-value pairs.>>> print eng2sp {'one': 'uno', 'two': 'dos'}
Q.
24 How to slice list in python ? AU : Jan 18
Ans. :
The
: operator used within the square bracket that it is a list slice and not the
index of the list
>>>
a = [10,20,30,40,50,60]
>>>
a [1:4) [20, 30, 40]
>>>
a[:5]
[10,
20, 30, 40, 50)
Q.
25 How to create a list in Python ? Illustrate
the use of negative indexing of list with example. AU : May-19
Ans. : The list is crated by following methods
-
(1)
Mylist = [ ] #creation of empty list
(2)
Mylist = [10,20,30,40] #creation of the list of elements of 10,20,30,40
The
negative indexing is the act of indexing from the end of the list with indexing
starting at - 1 i.e. - 1 gives the last element of list, - 2 gives the second
last element of list and so on.
For
example –
>>>
mylist = [10,20,30,40,50]
>>>
print(mylist(-1])
50
>>>
print(mylist[-2])
40
>>>
print(mylist(-3])
30
>>>
Q.
26 Give the Python code to find the minimum among the list of 10 numbers. AU: May 19
Ans. :
a
= [18, 52, 23, 41, 32, 55, 7, 3,-1, 100]
#
minimum number
min_num
= a[0] if a else None
#
find minimum number
for
i in a:
if
i<min_num:
min_num
= i
print("Minimum
element from the list is: ", min_num)
4
- 52
Q.
27 Demonstrate with simple code to draw the histogram in Python. AU: May 19
Ans. :
import
matplotlib.pyplot as plt
from
numpy.random import normal
gaussian_numbers
= normal(size=1000)
#plt.hist(gaussian_numbers)
plt.hist(gaussian_numbers)
plt.title("Gaussian
Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
Q.
28 Relate strings and lists. AU : Dec.-19
Ans. : String is basically a collection of
characters. This collection of characters is basically a list of characters.
For example –
mystring
= [','n','d','i','a']
Q.
29 Give a function that can take a value and return the first key mapping to
that value in a dictionary. AU : Dec.-19
Ans. :
def
myfun(d,val):
return
[k for k, v in d.items() if v == val]
d
= {
'AAA':
101,
"BBB":
102,
'CCC':
103,
'DDD':
104,
'EEE:
105
}
#
Get the first key in a dictionary
print('First
Key of dictionary:')
print(myfun(d,
101))
Output
First
Key of dictionary:
['AAA')
>>>
Problem Solving and Python Programming: UNIT IV: Lists, Tuples, Dictionaries : Tag: Engineering Python : Lists, Tuples, Dictionaries | Problem Solving and Python Programming - Anna University Two Marks Questions & Answers
Problem Solving and Python Programming
GE3151 1st Semester | 2021 Regulation | 1st Semester Common to all Dept 2021 Regulation