Q1

a = [10, 20, 30]
a.append(40)
print(a)
a[3] = 50
print(a)

What is the final output of the list 'a' above?

[10, 20, 30, 40] [10, 20, 30] [10, 20, 30, 50]

Q2

a = [10, 20, 30]
a.append(40)
a[4] = 50
print(a)

What is the output when you run the above set of statements?

[10, 20, 30, 40, 50] [10, 20, 30] IndexError

Q3


a = [10,'20', True]
print(type(a[2]))

What is the output of the above code?

str bool int Python lists can be heterogenous which means each element can be of a different data type. Since the third element is a boolean, it returns bool

Q4


a = (10,'20', True)
a.remove(10)
print(a)

What is the output of the above code?

('20', True) Error (10,True) Tuples are immutable so there is no remove method

Q5


a = [10, 20, 30]
a[0] = 40
print(a)

What is the output of the above code?

[40, 10, 20, 30] [40, 20, 30] Error Yes, you can replace the elements of a list using the index positions

Q6


a = [10, 20]
print(len(a))

What is the output of the above code?

30 2 'len' function computes the length of the collection. To get the sum of all the elements you use the 'sum' function

Q7


a = [10, 'b', [2, 20, 30], False]

In the above code, you see that the third element of list 'a' is another list. You are required to change the second element '20' of this inner list to '50'. I.e., after the change, your list would be
a = [10, 'b', [2, 50, 30], False]
Choose the statement which can make this change

a[1][1] = 50 a[2][1] = 50 a[1][2] = 50 First you get the third element of 'a' by using 'a[2]' and then get the second element of this list using the index position '1' of this expression.

Q8


a = [10, 'b', (2, 20, 30), False]

In the above code, you see that the third element of list 'a' is a tuple. You can change the second element '20' of this tuple to '50' because it is inside a list

True False A tuple object cannot be changed once created.

Q9


a = [10, 'b', (2, 20, 30), False]
a[2] = 40
a

What is the output of the above code segment?

Error [10, 'b', 40, False] [10, 40, (2, 20, 30), False] This does not throw error as you are replacing the entire tuple object with the number 40. Since you are replacing the third element because of using index number '2', the tuple object gets replaced.
[Exercise](https://colab.research.google.com/drive/1YhMIaIh1noCq3xFt1syFyHbDqH9uGq7z)

results matching ""

    No results matching ""