CH 2
CH 2
1. position
2. integer position
3. index
4. location
Answer
index
Reason — The index is the numbered position of a letter in the string.
Question 2
1. exists
2. in
3. into
4. inside
Answer
in
Reason — in is membership operator which returns True if a character or a substring
exists in the given string else returns False.
Question 3
1. integer
2. mutable
3. immutable
4. any of these
Answer
immutable
Reason — Dictionaries are indexed by keys and its keys must be of any immutable
type.
Question 4
Following set of commands is executed in shell, what will be the output?
>>>str = "hello"
>>>str[:2]
>>>
1. he
2. lo
3. olleh
4. hello
Answer
he
Reason — str[:2] here slicing operation begins from index 0 and ends at index 1.
Hence the output will be 'he'.
Question 5
1. list
2. dictionary
3. array
4. tuple
Answer
list
Reason — A list can store a sequence of values belonging to any data type and they
are depicted through square brackets.
Question 6
1. list
2. dictionary
3. array
4. tuple
Answer
tuple
Reason — For creating a tuple, enclosing the elements inside parentheses is optional.
Even if parentheses are omitted as shown here, still this statement will create a tuple.
Question 7
To store values in terms of key and value, what core data type does Python provide ?
1. list
2. tuple
3. class
4. dictionary
Answer
dictionary
Reason — Dictionaries are mutable with elements in the form of a key:value pair that
associate keys to values.
Question 8
1. (6.0, 27.0)
2. (6.0, 9.00)
3. (6, 27)
4. [6.0, 27.0]
5. [6, 27]
Answer
(6.0, 27.0)
Reason — The value of expression is in round brackets because it is tuple.
3 + 3.00 = 6.0
3**3.0 = 3 x 3 x 3 = 27.0
Question 9
1. del AL[2]
2. AL[2:3] = []
3. AL[2:2] = []
4. AL[2] = []
5. AL.remove(3)
Answer
del AL[2]
AL[2:3] = []
AL.remove(3)
Reason — del AL[2] — The del keyword deletes the element from the list AL from
index 2.
AL[2:3] = [] — The slicing of the list AL[2:3] begins at index 2 and ends at index 2.
Therefore, the element at index 2 will be replaced by an empty list [].
AL.remove(3) — The remove() function removes an element from the list AL from index
3."
Question 10
1. This is a string
2. 'This is a string'
3. (This is a string)
4. "This is a string"
Answer
'This is a string'
"This is a string"
Reason — Strings are enclosed within single or double quotes.
Question 11
1. my work
2. work
3. mywork
4. my
Answer
Output
mywork
Reason — The + operator creates a new string by joining the two operand strings.
Question 12
1. mywork
2. MY Work
3. myWORK
4. My Work
Answer
Output
myWORK
Reason — string.upper() method returns a copy of the string converted to uppercase.
The + operator creates a new string by joining the two operand strings.
Question 13
1. "one" + 'two'
2. 1+2
3. "one" + "2"
4. '1' + 2
Answer
'1' + 2
Reason — The + operator has to have both operands of the same type either of
number type (for addition) or of string type (for concatenation). It cannot work with one
operand as string and one as a number.
Question 14
1. "7"
2. "34"
3. 34
4. 24
Answer
Output
34
Reason — The + operator concatenates two strings and int converts it to integer type.
int("3" + "4")
= int("34")
= 34
Question 15
1. Line 3
2. Line 2
3. Line 4
4. Line 1
Answer
Line 4
Reason — Index 5 is out of range because list has 5 elements which counts to index
4.
Question 16
Answer
Day = {1:'Monday', 2:'Tuesday', 3:'wednesday'}
Reason — The syntax of dictionary declaration is:
<dictionary-name> = {<key>:<value>, <key>:<value>}
According this syntax, Day = {1:'Monday', 2:'Tuesday', 3:'wednesday'} is the correct
answer.
Question 17
Answer
list
Reason — A list can store a sequence of values belonging to any data type and
enclosed in square brackets.
Question 18
Suppose a tuple T is declared as T = (10, 12, 43, 39), which of the following is
incorrect ?
1. print(T[1])
2. T[2] = -29
3. print(max(T))
4. print(len(T))
Answer
T[2] = -29
Reason — Tuples are immutable. Hence we cannot perform item-assignment in
tuples.
Question 1
Question 2
Question 3
Question 4
Part of a string containing some contiguous characters from the string is called string
slice.
Question 5
The * operator when used with a list/string and an integer, replicates the list/string.
Question 6
Question 7
Question 8
Question 9
The del statement can remove an individual item or a slice from a list.
Question 10
Question 11
Question 12
Question 13
Question 14
The values() function returns all values from Key : value pair of a dictionary.
Question 15
The items() function returns all the Key : value pairs as (key, value) sequences.
True/False Questions
Question 1
Question 2
Answer
False
Reason — A list can store any data types and even list can contain another list as
element.
Question 3
True
Reason — The list can be of any size.
Question 4
Answer
False
Reason — A list is a standard data type of python that can store a sequence of values
belonging to any data type.
Question 5
Answer
True
Reason — List can have duplicate values.
Question 6
True
Reason — Dictionaries are indexed by keys. Hence its keys must be of any non-
mutable type.
Question 7
You can combine a numeric value and a string by using the + symbol.
Answer
False
Reason — The + operator has to have both operands of the same type either of
number type (for addition) or both of string type (for concatenation). It cannot work with
one operand as string and one as a number.
Question 8
The clear( ) removes all the elements of a dictionary but does not delete the empty
dictionary.
Answer
True
Reason — The clear() method removes all items from the dictionary and the dictionary
becomes empty dictionary post this method. del statement removes the complete
dictionary as an object.
Question 9
The max( ) and min( ) when used with tuples, can work if elements of the tuple are all
of the same type.
Answer
True
Reason — Tuples should contain same type of elements for max() and min() method
to work.
Question 10
For any index n, s[:n] + s[n:] will give you original string s.
Answer
True
Reason — s[:n] — The slicing of a string starts from index 0 and ends at index n-1.
s[n:] — The slicing of a string starts from index n and continues until the end of the
string.
So when we concatenate these two substrings we get original string s.
Question 12
Question 1
Assertion. Lists and Tuples are similar sequence types of Python, yet they are two
different data types.
Reason. List sequences are mutable and Tuple sequences are immutable.
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of
Assertion.
Explanation
Lists and tuples are similar sequence types in python, but they are distinct data types.
Both are used to store collections of items, but they have different properties and use
cases. Lists are mutable, meaning you can add, remove, or modify elements after the
list is created. Tuples, on the other hand, are immutable, meaning once a tuple is
created, its contents cannot be changed.
Question 2
Assertion. Modifying a string creates another string internally but modifying a list does
not create a new list.
Reason. Strings store characters while lists can store any type of data.
Answer
(b)
Both Assertion and Reason are true but Reason is not the correct explanation of
Assertion.
Explanation
In Python, strings are immutable, meaning once they are created, their contents
cannot be changed. Whenever we modify a string, python creates a new string object
to hold the modified contents, leaving the original string unchanged. On the other
hand, lists in python are mutable, meaning we can modify their contents after they
have been created. When we modify a list, python does not create a new list object.
Instead, it modifies the existing list object in place. Strings are sequences of
characters, and each character in a string can be accessed by its index. Lists, on the
other hand, can store any type of data, including integers, floats, strings.
Question 3
Assertion. Modifying a string creates another string internally but modifying a list does
not create a new list.
Reason. Strings are immutable types while lists are mutable types of python.
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of
Assertion.
Explanation
In Python, strings are immutable, meaning once they are created, their contents
cannot be changed. Whenever we modify a string, python creates a new string object
to hold the modified contents, leaving the original string unchanged. On the other
hand, lists in python are mutable, meaning we can modify their contents after they
have been created. When we modify a list, python does not create a new list object.
Instead, it modifies the existing list object in place.
Question 4
Assertion. Dictionaries are mutable, hence its keys can be easily changed.
Reason. Mutability means a value can be changed in place without having to create
new storage for the changed value.
Answer
(d)
Assertion is false but Reason is true.
Explanation
Dictionaries are indexed by keys and each key must be immutable and unique.
However, the dictionary itself is mutable, meaning that we can add, remove, or modify
key-value pairs within the dictionary without changing the identity of the dictionary
object itself. Mutability refers to the ability to change a value in place without creating a
new storage location for the changed value.
Question 5
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of
Assertion.
Explanation
A dictionary is a unordered set of key : value pairs and are indexed by keys. The
values of a dictionary can change but keys of dictionary cannot be changed because
through them data is hashed. Hence dictionaries are mutable but keys are immutable
and unique.
Question 6
Question 1
Question 2
Write a python script that traverses through an input string and prints its characters in
different lines - two characters per line.
Answer
name = input("Enter name:")
for i in range(0, len(name), 2):
print(name[i:i+2])
Output
Enter name:python
py
th
on
Question 3
Question 4
What do you understand by mutability ? What does "in place" task mean ?
Answer
Mutability means that the value of an object can be updated by directly changing the
contents of the memory location where the object is stored. There is no need to create
another copy of the object in a new memory location with the updated values.
Examples of mutable objects in python include lists, dictionaries.
In python, "in place" tasks refer to operations that modify an object directly without
creating a new object or allocating additional memory. For example, list methods like
append(), extend(), and pop() perform operations in place, modifying the original list,
while string methods like replace() do not modify the original string in place but instead
create a new string with the desired changes.