Boss lesson · Unit 03
A text toolkit
Small functions, tested and combined
What you practise
- Small functions built and combined: a palindrome check, a vowel counter, a Caesar shift, each one checked with
assertagainst answers you know
Try a question from this lesson
Pick one
You will build three tools for working with words. First, a new trick: for can walk through a string. What does this show?
for ch in "cat":
print(ch)Correct answer
c, a and t, one per line
Why
A string is a row of characters, and for hands them out one at a time, just as it hands out the numbers of a range. Here ch is "c", then "a", then "t".
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
assert
How do you know a function works? Check it against answers you already know.
assert takes a claim. If the claim is true, nothing happens. If it is false, the program stops with an AssertionError.
A few asserts under a function are a test that runs every time the program does.
def double(n):
return n * 2
assert double(4) == 8 # true: silent
assert double(0) == 0
print("all tests passed")Concept
in
in asks whether one string appears inside another. The answer is a bool, so it fits straight into an if.
With a single character on the left, it asks "is this character one of these?".
print("a" in "aeiou") # True
print("b" in "aeiou") # False
print("ell" in "hello") # True10 more exercises in the app
- 4× type the answer
- 2× fill the blanks
- 1× true or false
- 1× order the lines
- 2× multiple choice
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.