# 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:

```python
list_a = [1]
list_b = list_a
list_b[0] = 0
print(list_a[0] == list_b[0])
```

As the slice is a copy of the source list, the following snippet prints `False` to the screen:

```python
list_a = [1]
list_b = list_a[:]
list_b[0] = 0
print(list_a[0] == list_b[0])
```

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:

```python
the_list = []
the_list.append(1)
print(the_list)
```

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:

```python
the_list = [1]
the_list.insert(0, 2)
print(the_list)
```

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:

```python
the_list = [1]
del the_list[0]
print(the_list)
```

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:

```python
the_list = [1, 'a']
print('a' in the_list, 1 not in the_list)
```

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:

```python
the_list = [1,2,3]
for element in the_list:
    print(element, end=' ')
```

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:

```python
the_list = [x for x in range(1,4)]
print(the_list)
```

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://roxy-wong.gitbook.io/pcep-30-02-practice-test-compendium/part-2-or-study-pages-and-exam-section-questions/exam-block-3-data-collections-tuples-dictionaries-lists-and-strings/module-01/1.2.1-pcep-30-02-practice-test-compendium-lists-and-strings/1.2.1.2-pcep-30-02-practice-test-compendium-lists-and-strings.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
