9.4. List operationsΒΆ
The +
operator concatenates lists:
Similarly, the *
operator repeats a list a given number of times:
The first example repeats four times. The second example repeats the list three times.
- [4, 2, 8, 6, 5, 999]
- You cannot concatenate a list with an integer.
- Error, you cannot concatenate a list with an integer.
- Yes, in order to perform concatenation you would need to write alist+[999]. You must have two lists.
- [[4, 2, 8, 6, 5], 999]
- You cannot concatenate a list with an integer. This would cause an error, not create a new list.
- [4, 2, 8, 6, 5]
- This will cause an error, but alist will remain unchanged.
Q-3: What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
alist = alist + 999
print(alist)
- 6
- Concatenation does not add the lengths of the lists.
- [1, 2, 3, 4, 5, 6]
- Concatenation does not reorder the items.
- [1, 3, 5, 2, 4, 6]
- Yes, a new list with all the items of the first list followed by all those from the second.
- [3, 7, 11]
- Concatenation does not add the individual items.
Q-4: What is printed by the following statements?
alist = [1, 3, 5]
blist = [2, 4, 6]
print(alist + blist)
- 9
- Repetition does not multiply the lengths of the lists. It repeats the items.
- [1, 1, 1, 3, 3, 3, 5, 5, 5]
- Repetition does not repeat each item individually.
- [1, 3, 5, 1, 3, 5, 1, 3, 5]
- Yes, the items of the list are repeated 3 times, one after another.
- [3, 9, 15]
- Repetition does not multiply the individual items.
Q-5: What is printed by the following statements?
alist = [1, 3, 5]
print(alist * 3)
You have attempted of activities on this page