You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
10
10
11
11
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.
12
12
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.
14
14
15
15
:::program
16
16
```python
17
17
first = ["python"]
18
18
second = first
19
19
second.append("workers")
20
-
21
20
print(first)
22
21
print(second)
23
22
24
23
text ="python"
25
-
text.upper()
24
+
upper_text =text.upper()
26
25
print(text)
26
+
print(upper_text)
27
+
28
+
numbers = [3, 1, 2]
29
+
ordered =sorted(numbers)
30
+
print(ordered)
31
+
print(numbers)
27
32
```
28
33
:::
29
34
30
35
:::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.
34
37
35
38
```python
36
39
first = ["python"]
37
40
second = first
38
41
second.append("workers")
39
-
40
42
print(first)
41
43
print(second)
42
44
```
@@ -48,20 +50,39 @@ print(second)
48
50
:::
49
51
50
52
:::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.
52
54
53
55
```python
54
56
text ="python"
55
-
text.upper()
57
+
upper_text =text.upper()
56
58
print(text)
59
+
print(upper_text)
57
60
```
58
61
59
62
```output
60
63
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]
61
81
```
62
82
:::
63
83
64
84
:::note
65
85
- Lists and dictionaries are mutable; strings and tuples are immutable.
66
86
- 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.
Copy file name to clipboardExpand all lines: src/example_sources/none.md
+36-12Lines changed: 36 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,15 @@
2
2
slug = "none"
3
3
title = "None"
4
4
section = "Basics"
5
-
summary = "None represents the absence of a value."
5
+
summary = "None represents expected absence, distinct from missing keys and errors."
6
6
doc_path = "/library/constants.html#None"
7
7
+++
8
8
9
9
`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.
10
10
11
11
Because `None` is a singleton, idiomatic Python checks it with `is None` or `is not None`. This avoids confusing identity with value equality.
12
12
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.
14
14
15
15
:::program
16
16
```python
@@ -23,13 +23,20 @@ def find_score(name):
23
23
returnNone
24
24
25
25
score = find_score("Grace")
26
-
if score isNone:
27
-
print("missing score")
26
+
print(score isNone)
27
+
28
+
profile = {"name": "Ada"}
29
+
print(profile.get("timezone", "UTC"))
30
+
31
+
try:
32
+
int("python")
33
+
exceptValueError:
34
+
print("invalid number")
28
35
```
29
36
:::
30
37
31
38
:::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.
33
40
34
41
```python
35
42
result =None
@@ -42,9 +49,7 @@ True
42
49
:::
43
50
44
51
:::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.
48
53
49
54
```python
50
55
deffind_score(name):
@@ -53,16 +58,35 @@ def find_score(name):
53
58
returnNone
54
59
55
60
score = find_score("Grace")
56
-
if score isNone:
57
-
print("missing score")
61
+
print(score isNone)
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
+
exceptValueError:
79
+
print("invalid number")
58
80
```
59
81
60
82
```output
61
-
missing score
83
+
UTC
84
+
invalid number
62
85
```
63
86
:::
64
87
65
88
:::note
66
89
- 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.
Copy file name to clipboardExpand all lines: src/example_sources/sets.md
+29-22Lines changed: 29 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,37 +2,40 @@
2
2
slug = "sets"
3
3
title = "Sets"
4
4
section = "Collections"
5
-
summary = "Sets store unique values and support set algebra."
5
+
summary = "Sets store unique values and make membership checks explicit."
6
6
doc_path = "/tutorial/datastructures.html#sets"
7
7
+++
8
8
9
9
Sets store unique hashable values. Use them when membership and de-duplication matter more than order.
10
10
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.
12
12
13
13
Because sets are unordered, examples often wrap output in `sorted()` so the display is deterministic.
14
14
15
15
:::program
16
16
```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)
19
24
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))
25
29
```
26
30
:::
27
31
28
32
:::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.
30
34
31
35
```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))
36
39
```
37
40
38
41
```output
@@ -41,35 +44,39 @@ print(sorted(languages))
41
44
:::
42
45
43
46
:::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.
45
48
46
49
```python
47
-
print("python"in languages)
50
+
allowed = {"python", "rust"}
51
+
print("python"in allowed)
52
+
print("ruby"in allowed)
48
53
```
49
54
50
55
```output
51
56
True
57
+
False
52
58
```
53
59
:::
54
60
55
61
:::cell
56
62
Union, intersection, and difference describe relationships between groups without manual loops.
57
63
58
64
```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))
62
69
```
63
70
64
71
```output
65
72
['go', 'python', 'rust']
66
-
['go']
73
+
['rust']
67
74
['python']
68
75
```
69
76
:::
70
77
71
78
:::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.
74
81
- Sets are unordered, so sort them when examples need deterministic display.
0 commit comments