Skip to content

Commit f6019d5

Browse files
committed
Place contrast lessons across examples
1 parent fbf313a commit f6019d5

6 files changed

Lines changed: 186 additions & 100 deletions

File tree

src/asset_manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Generated by scripts/fingerprint_assets.py. Do not edit by hand.
22
ASSET_PATHS = {'SITE_CSS': '/site.06e133dcde6a.css', 'SYNTAX_JS': '/syntax-highlight.3b6c7f730d46.js', 'EDITOR_JS': '/editor.dd81f5171b14.js'}
3-
HTML_CACHE_VERSION = 'e289cc4cc5c2'
3+
HTML_CACHE_VERSION = 'd3f04ba18268'

src/example_sources/mutability.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,43 @@
22
slug = "mutability"
33
title = "Mutability"
44
section = "Data Model"
5-
summary = "Some objects can change in place, and names can share one object."
5+
summary = "Some objects change in place, while others return new values."
66
doc_path = "/reference/datamodel.html#objects-values-and-types"
77
+++
88

9-
Objects in Python can be mutable or immutable. Mutable objects such as lists can change in place, while immutable objects such as strings produce new values instead.
9+
Objects in Python can be mutable or immutable. Mutable objects such as lists and dictionaries can change in place, while immutable objects such as strings and tuples produce new values instead.
1010

1111
Names can share one mutable object, so a change through one name is visible through another. This is powerful, but it is also the source of many beginner surprises.
1212

13-
Understanding mutability explains why copying containers, avoiding mutable defaults, and returning new values are recurring Python design choices.
13+
The boundary matters across Python: `append()` mutates a list, string methods return new strings, and `sorted()` returns a new list while `list.sort()` mutates an existing one.
1414

1515
:::program
1616
```python
1717
first = ["python"]
1818
second = first
1919
second.append("workers")
20-
2120
print(first)
2221
print(second)
2322

2423
text = "python"
25-
text.upper()
24+
upper_text = text.upper()
2625
print(text)
26+
print(upper_text)
27+
28+
numbers = [3, 1, 2]
29+
ordered = sorted(numbers)
30+
print(ordered)
31+
print(numbers)
2732
```
2833
:::
2934

3035
:::cell
31-
Objects in Python can be mutable or immutable. Mutable objects such as lists can change in place, while immutable objects such as strings produce new values instead.
32-
33-
Names can share one mutable object, so a change through one name is visible through another. This is powerful, but it is also the source of many beginner surprises.
36+
Mutable objects can change in place. `first` and `second` point to the same list, so appending through one name changes the object seen through both names.
3437

3538
```python
3639
first = ["python"]
3740
second = first
3841
second.append("workers")
39-
4042
print(first)
4143
print(second)
4244
```
@@ -48,20 +50,39 @@ print(second)
4850
:::
4951

5052
:::cell
51-
Understanding mutability explains why copying containers, avoiding mutable defaults, and returning new values are recurring Python design choices.
53+
Immutable objects do not change in place. String methods such as `upper()` return a new string, leaving the original string unchanged.
5254

5355
```python
5456
text = "python"
55-
text.upper()
57+
upper_text = text.upper()
5658
print(text)
59+
print(upper_text)
5760
```
5861

5962
```output
6063
python
64+
PYTHON
65+
```
66+
:::
67+
68+
:::cell
69+
Some APIs make the boundary explicit. `sorted()` returns a new list, while methods such as `append()` and `list.sort()` mutate an existing list.
70+
71+
```python
72+
numbers = [3, 1, 2]
73+
ordered = sorted(numbers)
74+
print(ordered)
75+
print(numbers)
76+
```
77+
78+
```output
79+
[1, 2, 3]
80+
[3, 1, 2]
6181
```
6282
:::
6383

6484
:::note
6585
- Lists and dictionaries are mutable; strings and tuples are immutable.
6686
- Aliasing is useful, but copy mutable containers when independent changes are needed.
87+
- Pay attention to whether an operation mutates in place or returns a new value.
6788
:::

src/example_sources/none.md

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
slug = "none"
33
title = "None"
44
section = "Basics"
5-
summary = "None represents the absence of a value."
5+
summary = "None represents expected absence, distinct from missing keys and errors."
66
doc_path = "/library/constants.html#None"
77
+++
88

99
`None` represents the absence of a value. It is the usual sentinel when a function has no result to return but the absence itself is meaningful.
1010

1111
Because `None` is a singleton, idiomatic Python checks it with `is None` or `is not None`. This avoids confusing identity with value equality.
1212

13-
A function that reaches the end without a `return` statement returns `None`, so explicit `None` results should be documented by names and control flow.
13+
Absence has several nearby shapes in Python. A function can return `None`, a dictionary lookup can supply a default for a missing key, and an invalid operation can raise an exception.
1414

1515
:::program
1616
```python
@@ -23,13 +23,20 @@ def find_score(name):
2323
return None
2424

2525
score = find_score("Grace")
26-
if score is None:
27-
print("missing score")
26+
print(score is None)
27+
28+
profile = {"name": "Ada"}
29+
print(profile.get("timezone", "UTC"))
30+
31+
try:
32+
int("python")
33+
except ValueError:
34+
print("invalid number")
2835
```
2936
:::
3037

3138
:::cell
32-
`None` is Python's value for “nothing here.” It is commonly used as a sentinel when a real result is unavailable.
39+
`None` is Python's value for “nothing here.” Check it with `is None` because it is a singleton identity value.
3340

3441
```python
3542
result = None
@@ -42,9 +49,7 @@ True
4249
:::
4350

4451
:::cell
45-
Functions often return `None` when lookup or parsing fails without raising an exception.
46-
47-
Check for `None` with `is None`. That tests identity with the singleton object instead of asking for value equality.
52+
Functions often return `None` when absence is expected and callers can continue. The function name and surrounding code should make that possibility clear.
4853

4954
```python
5055
def find_score(name):
@@ -53,16 +58,35 @@ def find_score(name):
5358
return None
5459

5560
score = find_score("Grace")
56-
if score is None:
57-
print("missing score")
61+
print(score is None)
62+
```
63+
64+
```output
65+
True
66+
```
67+
:::
68+
69+
:::cell
70+
A missing dictionary key is another absence boundary. Use `get()` when the mapping can supply a default, and use exceptions for invalid operations that cannot produce a value.
71+
72+
```python
73+
profile = {"name": "Ada"}
74+
print(profile.get("timezone", "UTC"))
75+
76+
try:
77+
int("python")
78+
except ValueError:
79+
print("invalid number")
5880
```
5981

6082
```output
61-
missing score
83+
UTC
84+
invalid number
6285
```
6386
:::
6487

6588
:::note
6689
- Use `is None` rather than `== None`; `None` is a singleton identity value.
67-
- A function that reaches the end without `return` also returns `None`.
90+
- Use `None` for expected absence that callers can test.
91+
- Use dictionary defaults for missing mapping keys and exceptions for invalid operations.
6892
:::

src/example_sources/sets.md

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,40 @@
22
slug = "sets"
33
title = "Sets"
44
section = "Collections"
5-
summary = "Sets store unique values and support set algebra."
5+
summary = "Sets store unique values and make membership checks explicit."
66
doc_path = "/tutorial/datastructures.html#sets"
77
+++
88

99
Sets store unique hashable values. Use them when membership and de-duplication matter more than order.
1010

11-
The `in` operator is the everyday membership test. Set algebra then expresses how groups relate to each other.
11+
A list can answer membership with `in`, but a set communicates that membership is the main operation. Set algebra then expresses how groups relate to each other.
1212

1313
Because sets are unordered, examples often wrap output in `sorted()` so the display is deterministic.
1414

1515
:::program
1616
```python
17-
languages = {"python", "go", "python"}
18-
compiled = {"go", "rust"}
17+
languages = ["python", "go", "python"]
18+
unique_languages = set(languages)
19+
print(sorted(unique_languages))
20+
21+
allowed = {"python", "rust"}
22+
print("python" in allowed)
23+
print("ruby" in allowed)
1924

20-
print(sorted(languages))
21-
print("python" in languages)
22-
print(sorted(languages | compiled))
23-
print(sorted(languages & compiled))
24-
print(sorted(languages - compiled))
25+
compiled = {"go", "rust"}
26+
print(sorted(allowed | compiled))
27+
print(sorted(allowed & compiled))
28+
print(sorted(allowed - compiled))
2529
```
2630
:::
2731

2832
:::cell
29-
Creating a set automatically removes duplicates. The repeated `python` value appears only once.
33+
Creating a set removes duplicates. Keep a list when order and repeated values matter; convert to a set when uniqueness is the point.
3034

3135
```python
32-
languages = {"python", "go", "python"}
33-
compiled = {"go", "rust"}
34-
35-
print(sorted(languages))
36+
languages = ["python", "go", "python"]
37+
unique_languages = set(languages)
38+
print(sorted(unique_languages))
3639
```
3740

3841
```output
@@ -41,35 +44,39 @@ print(sorted(languages))
4144
:::
4245

4346
:::cell
44-
Membership checks are the most common set operation.
47+
Membership checks are the everyday set operation. A list can also use `in`, but a set says that membership is central to the data shape.
4548

4649
```python
47-
print("python" in languages)
50+
allowed = {"python", "rust"}
51+
print("python" in allowed)
52+
print("ruby" in allowed)
4853
```
4954

5055
```output
5156
True
57+
False
5258
```
5359
:::
5460

5561
:::cell
5662
Union, intersection, and difference describe relationships between groups without manual loops.
5763

5864
```python
59-
print(sorted(languages | compiled))
60-
print(sorted(languages & compiled))
61-
print(sorted(languages - compiled))
65+
compiled = {"go", "rust"}
66+
print(sorted(allowed | compiled))
67+
print(sorted(allowed & compiled))
68+
print(sorted(allowed - compiled))
6269
```
6370

6471
```output
6572
['go', 'python', 'rust']
66-
['go']
73+
['rust']
6774
['python']
6875
```
6976
:::
7077

7178
:::note
72-
- Sets remove duplicates and support fast membership tests.
73-
- Set algebra operators make union, intersection, and difference explicit.
79+
- Use lists when order and repeated values matter.
80+
- Use sets when uniqueness and membership are the main operations.
7481
- Sets are unordered, so sort them when examples need deterministic display.
7582
:::

src/example_sources_data.py

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)