Skip to content

Commit 926f46a

Browse files
Merge pull request #167 from alexgeer/main
lesson 1 and 2 docs: str.split(), list.sort(), open()
2 parents 7bff856 + a09aa3e commit 926f46a

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

docs/python/python-language.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,28 @@ Example Output:
681681
🍔🍔🍔
682682
```
683683

684+
#### Splitting strings
685+
686+
You can split strings into a `list` based on a separator string using the `str.split()` method. The separator is not included in the results:
687+
688+
```python
689+
sentence = "I love coding so much!"
690+
691+
# splitting on spaces makes a list of words
692+
words = sentence.split(" ")
693+
694+
print(words) # ["I", "love", "coding", "so", "much!"]
695+
696+
697+
# splitting on a longer separator
698+
words2 = sentence.split("love")
699+
700+
print(words2) ["I ", " coding so much!"]
701+
702+
```
703+
704+
705+
684706
_Further Reading_
685707

686708
- [The Python Library Reference - Text Sequence Type](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str)
@@ -846,6 +868,49 @@ alecg is in the 'names' list
846868
samh is NOT in the 'names' list
847869
```
848870

871+
872+
#### Sorting a `list`
873+
874+
To sort the values in a list, use the `.sort()` method. The sort is in-place, which means it reorders the original list instead of making a copy:
875+
876+
```python
877+
names = ["danielj", "alecg", "dimas"]
878+
names.sort()
879+
880+
print(names) # ['alecg', 'danielj', 'dimas']
881+
```
882+
883+
You can pass keyword arguments to the `list.sort()` method to customize the way the `list` is sorted.
884+
885+
For example, the `key` argument can be a function to run on each item of the `list` before sorting:
886+
887+
```python
888+
names = ["Danielj", "alecg", "Dimas"]
889+
names.sort()
890+
891+
# Notice how these aren't sorted correctly? Uppercase letters are "smaller"
892+
# than lowercase letters in the sorting algorithm that `sort()` uses!
893+
print(names) # ['Danielj', 'Dimas', 'alecg']
894+
895+
names.sort(key=str.lower)
896+
897+
# Now, everything is sorted correctly, and the original values haven't been
898+
# changed. `sort()` only uses the `key` function during the sorting process.
899+
print(names) # ['alecg', 'Danielj', 'Dimas']
900+
```
901+
902+
The `reverse` keyword argument of `list.sort()` is used to sort from high-to-low instead of low-to-high. It expects a `bool` value:
903+
904+
```python
905+
names = ["danielj", "alecg", "dimas"]
906+
names.sort(reverse=True)
907+
908+
print(names) # ['dimas', 'danielj', 'alecg']
909+
```
910+
911+
To see an alternative way of sorting, look up the built in `sorted()` function.
912+
913+
849914
_Further Reading_
850915

851916
- [The Python Library Reference - Common Sequence Operations](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations)
@@ -1240,6 +1305,46 @@ jobs = list(staff.values())
12401305
print(jobs) # ['Curriculum Developer', 'Curriculum Instructor', 'Designer']
12411306
```
12421307

1308+
##### `open()`
1309+
1310+
The `open()` function opens a file and returns it as a file object. It has two parameters `file` and `mode`.
1311+
1312+
```python
1313+
#opening "my_file.txt" in read mode
1314+
file = open("my_file.txt", "r")
1315+
1316+
contents = file.read()
1317+
1318+
print(contents)
1319+
1320+
file.close()
1321+
```
1322+
1323+
| Mode | Meaning |
1324+
|------|--------------------------------------------------------------------------------------------|
1325+
| `"r"` | Read-only mode |
1326+
| `"w"` | Write mode. Will overwrite all data in the file or create a new one if it doesn't exist |
1327+
| `"a"` | Append mode. Will add data to the end of the file, or create a new one if it doesn't exist |
1328+
1329+
After you are finished with a file, call the `.close()` method.
1330+
1331+
###### Looping through a file line by line
1332+
1333+
To process the file contents line by line, you can use a `for` loop to go throught the `file` object
1334+
1335+
```python
1336+
file = open("recipes.txt", "r")
1337+
1338+
counter = 0
1339+
for line in file:
1340+
counter += 1
1341+
if "taco" in line:
1342+
print(f"found taco on line {counter}")
1343+
1344+
print(f"there are {counter} lines in the file")
1345+
file.close()
1346+
```
1347+
12431348
##### `print()`
12441349

12451350
The `print()` function displays text on the screen:

0 commit comments

Comments
 (0)