Skip to content

📔 Reading

✏️ Section undergoing re-write. Come back soon.

👀 Reading Python

Code is more often read than written

- Guido van Rossum (creator of the Python programming language)

Python is famous for being a readable language. The words we use in Python, and the way we string words together, is very reminiscent of English.

For example, if we wanted to tell our friend to open Google Maps for various countries, we could write:

Repeat the following for each country in this list of countries:

"Canada", "Italy", "Cambodia", "Kenya", and "Japan"

  1. Type https://www.google.com/maps/place/ (the Google Maps url) into a new browser tab
  2. Add the country to the end of the url, like https://www.google.com/maps/place/Canada
  3. Hit enter to go to the website
maps.py
import webbrowser

maps_url = "https://www.google.com/maps/place/"
countries = ["Canada", "Italy", "Cambodia", "Kenya", "Japan"]

for country in countries:
    webbrowser.open_new_tab(maps_url + country)

Try it yourself

Try typing this example into Thonny and running it using the green 'run' button.

Now, although Python is a readable language, this example is still probably confusing. Take some time to study the example, and come up with 3 things that confuse you.

Possibly confusing things
  • What does import webbrowser mean?
  • What's with the underscore in maps_url?
  • Why is webbrowser.open_new_tab(MAPS_URL + country) indented?
  • Why do we need the . and the round brackets ( and )
  • What's with the colon (:), or the square brackets ([ and ])?

To develop our fluency, we will practice translating between English and Python.

Examples

For each example:

  1. Explain in English what you think the code does.
  2. Then type the code into the Thonny code editor (avoid copy-pasting, it is essential to develop muscle memory for writing Python)
  3. Finally, run it to see if you were right

Type everything

Type all the examples to better remember Python


Variables

There are 60 seconds per minute, and 60 minutes per hour, so there are 60 * 60 = 3600 seconds in one hour

"""There are 60 seconds per minute, and 60 minutes per hour, so there
are 60 * 60 = 3600 seconds in one hour
"""
seconds_per_minute = 60
minutes_per_hour = 60

seconds_per_hour = seconds_per_minute * minutes_per_hour

print(f"There are {seconds_per_hour} seconds in one hour")

While Loop

It's a birthday tradition to repeatedly chant "Are you 1? Are you 2? Are you ..." all the way up to the actual age of the birthday-person. Write a program that simulates this.

"""It's a birthday tradition to repeatedly chant "Are you 1? Are you 2? Are you ..."
all the way up to the actual age of the birthday-person.
Write a program that simulates this.
"""
age = 12
age_chant = 1

while age_chant < age:
    print(f"Are you {age_chant}?")
    age_chant = age_chant + 1

print(f"Happy Birthday! You are {age_chant}!")

If Elif Else

Write a program that will calculate income before and after taxes based on this bracketed tax scheme:

Tax bracket Percent tax
> $220,000 33%
> $150,000 29%
> $100,000 26%
> $50,000 20%
<= $50,000 15%
"""Write a program that will calculate income before and after taxes based on this bracketed tax scheme:

| Tax bracket | Percent tax |
| --- | --- |
| > $220,000 | 33% |
| > $150,000 | 29% |
| > $100,000 | 26% |
| > $50,000 | 20% |
| <= $50,000 | 15% |
"""
income = 57456.34
tax = None

if income > 220_000:
    tax = 0.33
elif income > 150_000:
    tax = 0.29
elif income > 100_000:
    tax = 0.26
elif income > 50_000:
    tax = 0.20
elif income >= 0:
    tax = 0.15
else:
    print("Invalid income!")

if tax:
    print(f"Income before taxes: {income}")
    print(f"Income after deducting {tax*100}% taxes: {income * (1.0 - tax)}")

For Loop

Honey, can you get the groceries? We need:

Item Amount
Milk 2 bags
Eggs 1 carton
Strawberry Shortcake 1 cake

Oh, and do keep the cake a surprise!

"""Honey, can you get the groceries? We need:

| Item| Amount |
| --- | --- |
| Milk | 2 bags |
| Eggs | 1 carton |
| Strawberry Shortcake | 1 cake |

Oh, and do keep the cake a surprise!
"""
groceries = {
    "milk": "2 bags",
    "eggs": "1 carton",
    "strawberry shortcake": "1",
}

print("Honey, we need:")

for item, amount in groceries.items():
    print(f" {amount} {item}")
    if "cake" in item:
        print("(Keep it a surprise!)")

Structural Pattern Matching

I have a list of files that I want to categorize. Write a program that will say the kind of each file.

"""I have a list of files that I want to categorize. Write a program that will say the kind of each file."""
files = [
    "cute-puppies.png",
    "national-security.pdf",
    "pinball3000.exe",
    "mywebsite.html",
    "birthday.py",
    "mysterious-file.cia",
]

for file in files:
    match file.split("."):
        case [filename, ("png" | "jpg" | "webp" | "svg")]:
            print(f"'{filename}' is an image file")
        case [filename, ("csv" | "txt" | "md")]:
            print(f"'{filename}' is a plain-text file")
        case [filename, ("exe" | "pkg")]:
            print(f"'{filename}' is an executable")
        case [filename, ("pdf" | "docx")]:
            print(f"'{filename}' is a document file")
        case [filename, "py"]:
            print(f"'{filename}' is Python source code!")
        case _:
            print(f"Not sure what kind of file '{file}' is...")

Functions

I have an audio speaker, and if the volume is over 100 dBs it's super loud. Over 70 dBs is still pretty loud. Just be careful not to go over 120, it will definitely distort the audio.

Oh, and make sure the speaker is actually on before you use it!

"""I have an audio speaker, and if the volume is over 100 dBs it's super loud.
Over 70 dBs is still pretty loud.
Just be careful not to go over 120, it will definitely distort the audio.

Oh, and make sure the speaker is actually on before you use it!
"""
import random

def jumble(s: str) -> str:
    """Jumbles each word in a string"""
    return " ".join([
        ''.join(random.sample(word, len(word))) for word in s.split(" ")
    ])

def speaker(audio: str, volume=40.0, on=True) -> None:
    """Simulates a speaker

    :param audio: The audio to play
    :param volume: In decibels (dBs)
    :param on: The state of the speaker (on or off)
    """
    if not on:
        return
    dB = 1
    DISTORTION = 120.0*dB
    SUPER_LOUD = 100.0*dB
    LOUD = 70.0*dB

    if volume > DISTORTION:
        print(jumble(audio))
    elif volume > SUPER_LOUD:
        print(" ".join(list(audio.upper())) + " ! ! !")
    elif volume > LOUD:
        print(audio.upper())
    else:
        print(audio.lower())

speaker("Hello is this thing on?", on=False)
speaker("Oh, wait, now it's working!")
speaker("Never gonna give you up...", volume=65.0)
speaker("Never gonna let you down...", volume=20.2)
speaker("Never gonna run around and desert you", volume=9001.0)