Skip to content

Commit

Permalink
Adding episode 02 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
wood-chris committed Feb 24, 2024
1 parent 06682e9 commit c5a6b92
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions _episodes/02-short-introduction-to-Python.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,18 @@ a_list = [1, 2, 3]
> 4. What information does the built-in function `len()` provide?
Does it provide the same information on both tuples and lists?
Does the `help()` function confirm this?
>
> > ## Solution
> > 1. The second value in a_list is replaced with 5.
> > 2. There is an error:
> > ~~~
> > TypeError: 'tuple' object does not support item assignment
> > ~~~~
> > {: .language-python}
> > As a tuple is immutable, it does not support item assignment. Elements in a list can be altered individually.
> > 3. `<class 'tuple'>`; The function tells you that the variable *a_tuple* is an object of the class *tuple*.
> > 4. `len()` tells us the length of an object. It works the same for both lists and tuples, providing us with the number of entries in each case.
> {: .solution}
{: .challenge}
Expand Down Expand Up @@ -444,6 +456,28 @@ for key in rev.keys():
> 2. Reassign the value that corresponds to the key `second` so that it no longer
> reads "two" but instead `2`.
> 3. Print the value of `rev` to the screen again to see if the value has changed.
>
> > ## Solution
> > 1.
> > ~~~
> > print(rev)
> > ~~~
> > {: .language-python}
> > ~~~
> > {'first': 'one', 'second': 'two', 'third': 'three'}
> > ~~~
> > {: .output}
> > 2 & 3:
> > ~~~
> > rev['second'] = 2
> > print(rev)
> > ~~~
> > {: .language-python}
> > ~~~
> > {'first': 'one', 'second': 2, 'third': 'three'}
> > ~~~
> > {: .output}
> {: .solution}
{: .challenge}
Expand Down

0 comments on commit c5a6b92

Please sign in to comment.