From c5a6b92c94701e71b1c2eeabe60d0dc88d4132b4 Mon Sep 17 00:00:00 2001 From: Chris Wood Date: Sat, 24 Feb 2024 16:35:08 +0000 Subject: [PATCH] Adding episode 02 solutions --- _episodes/02-short-introduction-to-Python.md | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/_episodes/02-short-introduction-to-Python.md b/_episodes/02-short-introduction-to-Python.md index 4990b40cb..d8a1fdc24 100644 --- a/_episodes/02-short-introduction-to-Python.md +++ b/_episodes/02-short-introduction-to-Python.md @@ -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. ``; 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} @@ -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}