Chapter 13 Lists, Dictionaries and Tuples Solutions
Question - 1 : - How many types of built-in types of sequences in Python.
Answer - 1 : -
Python has six built-in types of sequences.
Question - 2 : - Write the output of the given Python code.
# !/user/bin/python
list1 = [тАШphysicsтАЩ, тАШchemistryтАЩ, 1997, 2000];
list2 = [1,2,3,4,5,6, 7];
print тАЬlist1[0]тАЭ, list1[0]
Answer - 2 : -
list1[0]: physics
Question - 3 : - How can we remove list element ?
Answer - 3 : -
To remove a list element, you can use either the del statement or the remove]) method
Question - 4 : - Why we use cmp(listl, list2) in Python?
Answer - 4 : -
It compares elements of both lists.
Question - 5 : - Describe max(list) method.
Answer - 5 : -
It returns item from the list with max value.
Question - 6 : - Describe min(list) method.
Answer - 6 : -
It returns item from the list with min value.
Question - 7 : - What do you mean by list.count(obj)
Answer - 7 : -
Returns count how many times obj occurs in list
Question - 8 : - Is list.reverse() method return a value?
Answer - 8 : -
This method does not return any value but reverse the given object from the list
Question - 9 : - Define list in Python.
Answer - 9 : -
The list is a most versatile datatype available in Python which can be written as a list of comma- separated values (items) between square brackets. Good thing about a list is that items in a list need not all have the same type. Creating a list is as simple as putting different comma-separated values between square brackets.
For example :
list1 = [тАШphysicsтАЩ, тАШchemistryтАЩ, 1997,2000];
list2 = [1,2, 3,4, 5];
list3 = тАЩ[тАШaтАЭ, тАЬbтАЭ, тАЬcтАЭ, тАЬdтАЭ];
Question - 10 : - Give an example to access values in lists.
Answer - 10 : -
To access value in lists, use the square brackets for slicking along with the index or indices to obtain value available at that index. Following is a simple example :
# !/user/bin/python
list1 = [тАШphysicsтАЩ, тАШchemistryтАЩ, 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7];
print тАЬlist1 [0]:тАЭ,
list1 [0] print тАЬlist2[1 :5]:тАЭ, list2[1:5]
When the above code is executed, it produces the following result:
list1 [0]: physics
list2[1:5] : [2, 3, 4, 5]