From 977b8af130f3296ec6d0ce18714d09be6d1d3ceb Mon Sep 17 00:00:00 2001 From: David Herbert <49239555+davidherbert2@users.noreply.github.com> Date: Mon, 5 Oct 2020 13:08:05 +0100 Subject: [PATCH 1/4] adding clarification aboutr references (#370) --- episodes/03-types-conversion.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/episodes/03-types-conversion.md b/episodes/03-types-conversion.md index f20125577..3785cb6b9 100644 --- a/episodes/03-types-conversion.md +++ b/episodes/03-types-conversion.md @@ -180,6 +180,23 @@ three squared is 9.0 the former updates automatically. - This does **not** happen in programming languages. +Most of the time, variable assignment creates a new storage location for the value, so in this simple example: + +```python +a = 1 +b = a +a = 2 +print('a is', a, 'and b is', b) +``` + +```output +a is 2 and b is 1 +``` + +`b` is a **copy** of `a`, a separate storage location, so subsequent assignment of a new value to `a` leaves `b` completely unaffected. + +Here is a slightly more complicated example involving computation on the second value: + ```python variable_one = 1 variable_two = 5 * variable_one @@ -196,6 +213,8 @@ first is 2 and second is 5 - Afterwards, the value of `variable_two` is set to the new value and *not dependent on `variable_one`* so its value does not automatically change when `variable_one` changes. +Sometimes, however (and this can happen with Python list types, described in more detail in Episode 11:Lists), variables can point to the same storage location. Think of a cell in a spreadsheet which depends on another cell. Updating the latter will automatically update the former. The dependent cell is actually a **reference** to the original, not a distinct copy of it. + ::::::::::::::::::::::::::::::::::::::: challenge ## Fractions From b681598dd8827418b508ad77695df6b2c2bee948 Mon Sep 17 00:00:00 2001 From: David Herbert <49239555+davidherbert2@users.noreply.github.com> Date: Mon, 5 Oct 2020 13:10:00 +0100 Subject: [PATCH 2/4] Slightly better wording --- episodes/03-types-conversion.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/episodes/03-types-conversion.md b/episodes/03-types-conversion.md index 3785cb6b9..8a25b8495 100644 --- a/episodes/03-types-conversion.md +++ b/episodes/03-types-conversion.md @@ -213,7 +213,7 @@ first is 2 and second is 5 - Afterwards, the value of `variable_two` is set to the new value and *not dependent on `variable_one`* so its value does not automatically change when `variable_one` changes. -Sometimes, however (and this can happen with Python list types, described in more detail in Episode 11:Lists), variables can point to the same storage location. Think of a cell in a spreadsheet which depends on another cell. Updating the latter will automatically update the former. The dependent cell is actually a **reference** to the original, not a distinct copy of it. +Sometimes, however (and this can happen with Python list types, described in more detail in Episode 11:Lists), more than one variable can point to the same storage location. Think of a cell in a spreadsheet which depends on another cell. Updating the latter will automatically update the former. The dependent cell is actually a **reference** to the original, not a distinct copy of it. ::::::::::::::::::::::::::::::::::::::: challenge From 3ab4d933b26cb78e3cf537a000008ee8b45fe3f0 Mon Sep 17 00:00:00 2001 From: David Herbert <49239555+davidherbert2@users.noreply.github.com> Date: Mon, 5 Oct 2020 15:18:20 +0100 Subject: [PATCH 3/4] Wording of references/relevance to lists (#370) --- episodes/03-types-conversion.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/episodes/03-types-conversion.md b/episodes/03-types-conversion.md index 8a25b8495..145b12a26 100644 --- a/episodes/03-types-conversion.md +++ b/episodes/03-types-conversion.md @@ -213,7 +213,7 @@ first is 2 and second is 5 - Afterwards, the value of `variable_two` is set to the new value and *not dependent on `variable_one`* so its value does not automatically change when `variable_one` changes. -Sometimes, however (and this can happen with Python list types, described in more detail in Episode 11:Lists), more than one variable can point to the same storage location. Think of a cell in a spreadsheet which depends on another cell. Updating the latter will automatically update the former. The dependent cell is actually a **reference** to the original, not a distinct copy of it. +Sometimes, however (e.g. with Python list types, described in more detail in Episode 11:Lists - under section 'Copying (or Not)'), more than one variable *can* point to the same storage location. Think of a cell in a spreadsheet which depends on another cell. Updating the latter will automatically update the former. The dependent cell is actually a **reference** to the original, not a distinct copy of it. ::::::::::::::::::::::::::::::::::::::: challenge From 93d77be9cf766b51aed31d29db36e818daa6b857 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 7 Aug 2025 14:06:42 +0200 Subject: [PATCH 4/4] Clarify explanation of mutable references (#370) --- episodes/03-types-conversion.md | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/episodes/03-types-conversion.md b/episodes/03-types-conversion.md index 145b12a26..64cc863e1 100644 --- a/episodes/03-types-conversion.md +++ b/episodes/03-types-conversion.md @@ -173,14 +173,21 @@ half is 0.5 three squared is 9.0 ``` -## Variables only change value when something is assigned to them. - -- If we make one cell in a spreadsheet depend on another, - and update the latter, - the former updates automatically. -- This does **not** happen in programming languages. - -Most of the time, variable assignment creates a new storage location for the value, so in this simple example: +## Assignment changes the value of a variable, it does not create links between variables. + +- In the spreadsheet context, + - If we use a formula to connect one cell to another, + and update the latter, + the former updates automatically. + - In contrast, if we copy one cell and paste its contents into another cell, + the second cell will not update if the first cell changes. + To update the second cell, we would need to copy and paste again. +- Assignment (`=` operator) in python works like copy and paste in spreadsheets + not like a spreadsheet formula connecting the two cells. + In other words, after a variable is used to assign a value to another variable, + reassigning the first variable does not change the second variable. + +To demostrate this: ```python a = 1 @@ -193,7 +200,7 @@ print('a is', a, 'and b is', b) a is 2 and b is 1 ``` -`b` is a **copy** of `a`, a separate storage location, so subsequent assignment of a new value to `a` leaves `b` completely unaffected. +When `b = a` is run, `a`'s value (`1`) is assigned to `b`, but no ongoing link is created between `a` and `b`. Here is a slightly more complicated example involving computation on the second value: @@ -213,7 +220,7 @@ first is 2 and second is 5 - Afterwards, the value of `variable_two` is set to the new value and *not dependent on `variable_one`* so its value does not automatically change when `variable_one` changes. -Sometimes, however (e.g. with Python list types, described in more detail in Episode 11:Lists - under section 'Copying (or Not)'), more than one variable *can* point to the same storage location. Think of a cell in a spreadsheet which depends on another cell. Updating the latter will automatically update the former. The dependent cell is actually a **reference** to the original, not a distinct copy of it. +Some data types that we haven't encountered yet (e.g. lists) have links inside them so they behave differently than described above. An example of this is shown in [Episode 11: Lists](../11-lists.md#copying-or-not). Assigning one of these values to a new variable is a bit like copying and pasting a formula from one cell to another. If the cells referenced in the formula change, then both cells that contain the formula will also change. However, unlike in a spreadsheet, these variables ("formula cells") can be used to change the referenced data (analogous to cells referenced by the formula). ::::::::::::::::::::::::::::::::::::::: challenge