mirror of
https://github.com/davrot/pytutorial.git
synced 2025-04-16 04:16:35 +02:00
Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
parent
f62b1cb6d3
commit
bf37d9cf52
1 changed files with 41 additions and 1 deletions
|
@ -142,7 +142,7 @@ print(sum(primes)) # -> 17
|
|||
print(max(primes)) # -> 7
|
||||
```
|
||||
|
||||
### [append()](), [pop()](), and [remove()]()
|
||||
### [append()](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists), [pop()](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists), and [remove()](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists)
|
||||
|
||||
```python
|
||||
collection_of_strings = [
|
||||
|
@ -165,7 +165,47 @@ print(collection_of_strings.pop()) # -> II
|
|||
print(
|
||||
collection_of_strings
|
||||
) # -> ['AA', 'BB', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG', 'HH']
|
||||
|
||||
collection_of_strings.remove("BB")
|
||||
print(collection_of_strings) # -> ['AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG', 'HH']
|
||||
|
||||
collection_of_strings.remove("BB")
|
||||
print(collection_of_strings) # -> ['AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG', 'HH']
|
||||
|
||||
collection_of_strings.remove("BB")
|
||||
print(collection_of_strings) # -> ValueError: list.remove(x): x not in list
|
||||
```
|
||||
|
||||
## [Index](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists)
|
||||
|
||||
```python
|
||||
collection_of_strings = [
|
||||
"AA",
|
||||
"BB",
|
||||
"CC",
|
||||
"DD",
|
||||
"EE",
|
||||
"FF",
|
||||
"GG",
|
||||
"HH",
|
||||
]
|
||||
|
||||
print(collection_of_strings.index("CC")) # -> 2
|
||||
print(collection_of_strings.index("XX")) # -> ValueError: 'XX' is not in list
|
||||
```
|
||||
|
||||
## [del](https://docs.python.org/3/tutorial/datastructures.html#the-del-statement) and [insert](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists)
|
||||
|
||||
```python
|
||||
collection_of_strings = []
|
||||
collection_of_strings.append("BB")
|
||||
collection_of_strings.append("CC")
|
||||
collection_of_strings.append("DD")
|
||||
print(collection_of_strings) # -> ['BB', 'CC', 'DD']
|
||||
|
||||
collection_of_strings.insert(0, "AA")
|
||||
print(collection_of_strings) # -> ['AA', 'BB', 'CC', 'DD']
|
||||
|
||||
del collection_of_strings[1]
|
||||
print(collection_of_strings) # -> ['AA', 'CC', 'DD']
|
||||
```
|
||||
|
|
Loading…
Add table
Reference in a new issue