-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_4.py
More file actions
44 lines (28 loc) · 3.55 KB
/
Copy pathAssignment_4.py
File metadata and controls
44 lines (28 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
1. `[]` is the empty list, a data structure in Python that can store a collection of values.
2. To assign the value 'hello' as the third value in the list stored in the variable `spam`, you can use the following code:
"""
spam = [2, 4, 6, 8, 10]
spam[2] = 'hello'
"""
3. Assuming `spam` contains the list `['a', 'b', 'c', 'd']`, the value of `spam[int(int('3' * 2) / 11)]` would be `'d'`. Here's the breakdown:
- `'3' * 2` evaluates to `'33'`
- `int('33')` evaluates to `33`
- `33 / 11` evaluates to `3.0`
- `int(3.0)` evaluates to `3`
- `spam[3]` evaluates to `'d'`
4. Assuming `spam` contains the list `['a', 'b', 'c', 'd']`, the value of `spam[-1]` would be `'d'`. Python's negative indexing starts from the end of the list, so `-1` refers to the last element.
5. Assuming `spam` contains the list `['a', 'b', 'c', 'd']`, the value of `spam[:2]` would be `['a', 'b']`. This is a slice that includes elements from the start of the list up to, but not including, index 2.
6. Assuming `bacon` contains the list `[3.14, 'cat', 11, 'cat', True]`, the value of `bacon.index('cat')` would be `1`. The `index()` method returns the index of the first occurrence of the specified element.
7. Assuming `bacon` contains the list `[3.14, 'cat', 11, 'cat', True]`, the expression `bacon.append(99)` would change the list in `bacon` to `[3.14, 'cat', 11, 'cat', True, 99]`.
8. Assuming `bacon` contains the list `[3.14, 'cat', 11, 'cat', True]`, the expression `bacon.remove('cat')` would change the list in `bacon` to `[3.14, 11, 'cat', True]`. The `remove()` method removes the first occurrence of the specified element.
9. The list concatenation operator is `+`, which combines two lists into a new list. The list replication operator is `*`, which repeats a list a specified number of times.
10. The `append()` method adds an element to the end of the list, while the `insert()` method inserts an element at a specified index, shifting the existing elements to make room for the new one.
11. The two methods for removing items from a list are `remove()`, which removes the first occurrence of the specified element, and `pop()`, which removes and returns the element at the specified index (or the last element if no index is specified).
12. List values and string values are both sequences, meaning they can be indexed and sliced. They also support similar operations, such as concatenation and replication.
13. The main difference between tuples and lists is that tuples are immutable (their elements cannot be changed), while lists are mutable (their elements can be changed).
14. To create a tuple with only the integer 42, you would use the following syntax: `(42,)`. The trailing comma is required to differentiate it from the integer 42 enclosed in parentheses.
15. To get a list value's tuple form, you can use the `tuple()` function: `tuple([1, 2, 3])` would give you `(1, 2, 3)`. To get a tuple value's list form, you can use the `list()` function: `list((1, 2, 3))` would give you `[1, 2, 3]`.
16. Variables that "contain" list values actually contain references to the list objects, not the lists themselves. This means that when you assign a list to a variable, you're not copying the list, but rather creating a new reference to the same list object.
17. `copy.copy()` creates a shallow copy of a list, meaning it creates a new list object with references to the same elements as the original list. `copy.deepcopy()` creates a deep copy of a list, meaning it creates a new list object with new copies of all the elements, including nested objects.
"""