Updated Headers Lists.md

This commit is contained in:
aCyberVoid 2024-11-27 03:22:18 -06:00
parent 10cb611211
commit 99368b1e52

View file

@ -4,7 +4,7 @@ Pythons list is a versatile compound data type used to group multiple values.
>>> squares
[1, 4, 9, 16, 25]
```
#### Indexing and Slicing
### Indexing and Slicing
Lists support indexing and slicing, similar to strings:
```python
>>> squares[0] # First item
@ -14,13 +14,13 @@ Lists support indexing and slicing, similar to strings:
>>> squares[-3:] # Slice of last three items
[9, 16, 25]
```
#### Concatenation
### Concatenation
Lists can be concatenated using the `+` operator:
```python
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
```
#### Mutability
### Mutability
Unlike strings, lists are mutable, allowing modification of their content:
```python
>>> cubes = [1, 8, 27, 65, 125] # Incorrect value
@ -36,7 +36,7 @@ New items can be added using the `append()` method:
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
```
#### Slicing and Shallow Copies
### Slicing and Shallow Copies
All slice operations return a new list containing the requested elements. This means slices create a [shallow copy](https://docs.python.org/3/library/copy.html#shallow-vs-deep-copy) of the list:
```python
>>> rgb = ["Red", "Green", "Blue"]
@ -51,7 +51,7 @@ All slice operations return a new list containing the requested elements. This m
> [!important] Additional Reading
> Datacamp: [Python Copy List: What You Should Know](https://www.datacamp.com/tutorial/python-copy-list)
> Geeks for Geeks: [Difference Between Deep and Shallow Copy](https://www.geeksforgeeks.org/difference-between-shallow-and-deep-copy-of-a-class/)
#### Assigning to Slices
### Assigning to Slices
Slices can be used to modify, remove, or clear items:
```python
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
@ -61,7 +61,7 @@ Slices can be used to modify, remove, or clear items:
# Returns ['a', 'b', 'f', 'g']
>>> letters[:] = [] # Clear list
```
#### Length
### Length
The `len()` function returns the number of items in a list:
```python
>>> letters = ['a', 'b', 'c', 'd']
@ -69,7 +69,7 @@ The `len()` function returns the number of items in a list:
4
```
#### Nesting
### Nesting
Lists can also contain other lists (nesting):
```python
>>> a = ['a', 'b', 'c']