A collection of exercises that have been either removed from or not (yet) added to the main lesson.
Swapping the contents of variables (5 min)
Explain what the overall effect of this code is:
left = 'L' right = 'R' temp = left left = right right = temp
Compare it to:
left, right = right, left
Do they always do the same thing? Which do you find easier to read?
Solution
Turn a String into a List
Use a for-loop to convert the string “hello” into a list of letters:
["h", "e", "l", "l", "o"]
Hint: You can create an empty list like this:
my_list = []
Solution
Reverse a String
Knowing that two strings can be concatenated using the
+
operator, write a loop that takes a string and produces a new string with the characters in reverse order, so'Newton'
becomes'notweN'
.Solution
Fixing and Testing
From: “Defensive Programming”
Fix
range_overlap
. Re-runtest_range_overlap
after each change you make.Solution