1.2.1.2 PCEP-30-02 Practice Test Compendium – Lists and strings
Lists and strings
01) If any of the slice's indices exceeds the allowable range, no exception is raised, and the non-existent elements are not taken into consideration. Therefore, it is possible that the resulting slice is an empty list.
02) Assigning a list to a list does not copy elements. Such an assignment results in a situation when more than one name identifies the same data aggregate.
For example, the following snippet prints True
to the screen:
As the slice is a copy of the source list, the following snippet prints False
to the screen:
03) The .append(element)
method can be used to append an element to the end of an existing list.
For example, the following snippet outputs [1]
to the screen:
04) The .insert(at_index, element)
method can be used to insert the element at the at_index
of the existing list. For example, the following snippet outputs [2, 1]
to the screen:
05) The del the_list[index]
instruction can be used to remove any of the existing list elements.
For example, the following snippet prints []
to the screen:
06) The in
and not in
operators can check whether any value is contained inside the list or not.
For example, the following snippet prints True False
to the screen:
07) Lists can be iterated through (traversed) by the for
loop, which allows the programmer to scan all their elements without the use of explicit indexing. For example, the following snippet prints 1 2 3
to the screen:
08) List comprehension allows the programmer to construct lists in a compact way. For example, the following snippet prints [1,2,3]
to the screen:
09) Strings, like lists, are sequences, and in many contexts they behave like lists, especially when they are indexed and sliced or are arguments of the len()
function.
10) The in
and not in
operators can be applied to strings to check if any string is a part of another string. An empty string is considered a part of any string, including an empty one.
11) Strings are immutable and their contents cannot be changed.
Last updated