Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions episodes/03-types-conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,36 @@ half is 0.5
three squared is 9.0
```

## Variables only change value when something is assigned to them.
## 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:

- 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.
```python
a = 1
b = a
a = 2
print('a is', a, 'and b is', b)
```

```output
a is 2 and b is 1
```

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:

```python
variable_one = 1
Expand All @@ -196,6 +220,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.

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

## Fractions
Expand Down