Skip to content

Commit 9bb5afa

Browse files
committed
differences for PR #700
1 parent a42c14c commit 9bb5afa

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

03-types-conversion.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,29 +173,55 @@ half is 0.5
173173
three squared is 9.0
174174
```
175175

176-
## Variables only change value when something is assigned to them.
176+
## Assignment changes the value of a variable, it does not create links between variables.
177+
178+
- In the spreadsheet context,
179+
- If we use a formula to connect one cell to another,
180+
and update the latter,
181+
the former updates automatically.
182+
- In contrast, if we copy one cell and paste its contents into another cell,
183+
the second cell will not update if the first cell changes.
184+
To update the second cell, we would need to copy and paste again.
185+
- Assignment (the `=` operator) in python works like copy and paste in spreadsheets
186+
not like a spreadsheet formula connecting the two cells.
187+
In other words, after a variable is used to assign a value to another variable,
188+
reassigning the first variable does not change the second variable.
189+
190+
To demonstrate this:
177191

178-
- If we make one cell in a spreadsheet depend on another,
179-
and update the latter,
180-
the former updates automatically.
181-
- This does **not** happen in programming languages.
192+
```python
193+
a = 1
194+
b = a
195+
a = 2
196+
print('a is', a, 'and b is', b)
197+
```
198+
199+
```output
200+
a is 2 and b is 1
201+
```
202+
203+
When `b = a` is run, `a`'s value (`1`) is assigned to `b`, but no ongoing link is created between `a` and `b`.
204+
205+
Here is a slightly more complicated example involving computation on the second value:
182206

183207
```python
184208
variable_one = 1
185209
variable_two = 5 * variable_one
186210
variable_one = 2
187-
print('first is', variable_one, 'and second is', variable_two)
211+
print('variable_one is', variable_one, 'and variable_two is', variable_two)
188212
```
189213

190214
```output
191-
first is 2 and second is 5
215+
variable_one is 2 and variable_two is 5
192216
```
193217

194-
- The computer reads the value of `variable_one` when doing the multiplication,
218+
- Python reads the value of `variable_one` when doing the multiplication,
195219
creates a new value, and assigns it to `variable_two`.
196-
- Afterwards, the value of `variable_two` is set to the new value and *not dependent on `variable_one`* so its value
220+
- Afterwards, `variable_two` is set to this new value and *is not dependent on `variable_one`* so its value
197221
does not automatically change when `variable_one` changes.
198222

223+
Some data types that we haven't encountered yet (e.g. _lists_, _dictionaries_, and _objects_) have "links" inside them so they behave somewhat differently when you assign values to their *contents*. An example of this is shown in [Episode 12: Lists](../11-lists.md#copying-or-not). Assigning a list value to a new variable is like copying and pasting a formula from one cell to another. When you update an item in that list with the new value, you're updating that canonical list.
224+
199225
::::::::::::::::::::::::::::::::::::::: challenge
200226

201227
## Fractions

md5sum.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"links.md" "e64df47952f39ef6e4439db795b92c15" "site/built/links.md" "2024-12-10"
77
"episodes/01-run-quit.md" "ca5737c8a9dc8c8e16f8320550a93787" "site/built/01-run-quit.md" "2024-04-15"
88
"episodes/02-variables.md" "9dacd8cd9968b5d0185f0c244a55eb84" "site/built/02-variables.md" "2023-05-02"
9-
"episodes/03-types-conversion.md" "9e3a08116a2124cd8e23d1d7c5dba432" "site/built/03-types-conversion.md" "2023-05-02"
9+
"episodes/03-types-conversion.md" "fc12a602e26eb6faf533c4e73000b538" "site/built/03-types-conversion.md" "2025-10-31"
1010
"episodes/04-built-in.md" "e91afeb3e74fb132970be01eaae403ca" "site/built/04-built-in.md" "2025-04-01"
1111
"episodes/05-coffee.md" "c7616ec40b9e611c47b2bac1e11c47d2" "site/built/05-coffee.md" "2023-05-02"
1212
"episodes/06-libraries.md" "96899c58843e51f10eb84a8ac20ebb90" "site/built/06-libraries.md" "2023-05-02"

0 commit comments

Comments
 (0)