Page 49 - PYTHON-11
P. 49
85. Write any one difference between insert() and append() function.
Ans. insert() function adds elements at the specified index of list while append() function adds single element
at the end of the list.
86. Give methods which can be used with both list and tuple.
Ans. len(), max(), min(), sum(), sorted()
87. What do you mean by concatenation of list? Explain with example.
Ans. Concatenation of list means combining the two lists.
For example,
>>>a = [1,2,3,4]
>>>b = [5,6,7,8]
>>> a+b
The output will be: [1,2,3,4,5,6,7,8]
88. What is the purpose of using list (sequence) method in Python?
Ans. This method converts a sequence (usually a tuple) passed as a parameter to it into a list.
89. Which method is used to sort the list elements?
Ans. sort() in ascending order and sort(reverse=True) in descending order.
90. Which operators can be used with integers as well as with strings?
Ans. The addition (‘+’) operator, when used with integers, adds numbers whereas when used with strings, it
concatenates the given strings.
The multiplication (‘*’) operator, when used with integers, multiplies the numbers whereas when used with
strings, it replicates the strings.
91. Differentiate between lists and tuples.
Ans. The differences between the two are:
(a) Lists are mutable while tuples are immutable in nature.
(b) Tuples are more memory-efficient than lists.
92. Differentiate between pop() and remove() function.
Ans. pop(): This function removes the element from the specified index and also returns the element which
has been deleted.
remove(): This function removes the first matching value and not a specific index. It does not return the
deleted element.
93. What is a Python module?
Ans. A module is a chunk of Python code that exists in its own (.py) file and is intended to be used by Python code
outside itself. Modules can be ‘imported’ in other programs so that the functions and other definitions in
imported modules become available to code that imports them.
94. Name any two built-in modules and two functions belonging to each category.
Ans. Built-in modules such as math, statistics and their functions:
(a) math: sqrt(), pow()
(b) statistics: mean(), median()
95. How can you generate random numbers in Python?
Ans. To generate random numbers in Python, we need to import command as:
>>> import random
>>> random.random()
0.7537949667499043
This returns a random floating-point number in the range [0,1).
96. Name the function used to create an empty dictionary.
Ans. dict()
97. What is the use of abs() function in Python?
Ans. The abs() function returns the absolute (positive) value of the given number. It is the same as fabs()
method. For example, abs(-88) or fabs(–88) shall return 88 as the output.
V.7