List & Tuples
Listss
Section titled “Listss”- Definition: A flexible, ordered collection of arbitrary/hetro objects.
- Syntax: Elements enclosed in square brackets
[]and separated by commas. - Core Characteristics:
- Ordered: The sequence matters (
[1, 2] != [2, 1]). - Heterogeneous: Can contain any mix of data types (integers, strings, other lists).
- Accessible: Elements are accessed via 0-based indexing and slicing.
- Nestable: Can be nested to arbitrary depths (e.g.,
x[1][2]accesses the third element of the list located at index 1 of the parent list). - Mutable: Elements can be changed in-place without creating a new object.
- Update:
list1[2] = 2001 - Delete:
del list1[2]
- Update:
- Ordered: The sequence matters (
Built-in List Methods
Section titled “Built-in List Methods”These methods perform in-place operations on the list object:
append(value): Adds a single item to the extreme end of the list.count(value): Returns the integer count of how many timesvalueappears.extend(iterable): Appends all items from another iterable (like another list) to the end.index(value, [start, [stop]]): Returns the first index position ofvalue. Raises aValueErrorif the item does not exist.insert(index, value): Injects an item at the specified integer position, shifting subsequent elements to the right.pop([index=-1]): Removes and returns the item at the specified index. Defaults to the last item (1) if no index is provided.remove(value): Deletes the first occurrence of the specified value.reverse(): Inverts the order of the list elements in-place.sort(cmp=None, key=None, reverse=False): Sorts the list in-place. Defaults to ascending order. Setreverse=Truefor descending order.
Tuples
Section titled “Tuples”- Definition: A collection of objects that is ordered but immutable (unchangeable).
- Syntax: Elements enclosed in parentheses
()or simply comma-separated without brackets.- Examples:
tup1 = (1, 2, 3)ortup2 = "a", "b", "c"
- Examples:
- Access & Slicing: Identical logic to lists. You can extract substrings using
t[start:stop:step](e.g.,t[::-1]to reverse a tuple). - Immutability Logic:
- Attempting to update an item (
tup1[0] = 100) strictly raises aTypeError. - To “modify” a tuple, you must create a completely new object in memory using concatenation (
tup3 = tup1 + tup2).
- Attempting to update an item (
Tuple Operations Reference
Section titled “Tuple Operations Reference”Based on the table logic provided in your image, here are the standard expressions and behaviors for tuple objects:
| Expression | Result | Description |
|---|---|---|
len((1,2,3)) | 3 | Length: Evaluates the total number of top-level items. |
(1,2,3)+(4,5,6) | (1,2,3,4,5,6) | Concatenation: Merges tuples into a new single tuple. |
("O",)*3 | ("O", "O", "O") | Reiteration: Multiplies the tuple sequence. (Note: A trailing comma is required for a single-element tuple). |
3 in (1,2,3) | True | Membership: Checks if the element exists inside the tuple structure. |
for x in (1,2,3): print(x) | ||
| or | ||
for x in 1,2,3: print(x) | 1 | |
2 | ||
3 | Iteration: Loops through the tuple sequentially. The parentheses are optional for execution. |