This holiday I went to Palermo with these goals:

  • see more daylight and less snow than Oslo 🌴
  • have a lot of antipasti, huge plates of pasta and wine 🍷
  • hang out in cafés and start to Learn Python The Hard Way 💻

These are my notes. You can read them if you want — but I make no attempt at this post being useful for anyone else but future me. It’s not an overview of the exercises in the book.


“The Hard Way Is Easier” introduction text is nice. A lot of it I feel like I know well already, and then there’s a part on “Practice and Persistence” I might well return to reread one rainy day.

✅ YES! Go through all the exercies in Command Line Crash Course.


2, 3: Characters and Numbers

“Octothorpe!” Who knew?!

Read what you typed above out loud, including saying each character by its name.

Interesting; I know a lot about quotation marks in a typographic sense, and I know what we typically nickname " and ' when talking about code in Norwegian. But I’m not instinctively 100% sure what " is called in English when talking about code? Just quote?

  • () Round brackets or parentheses
  • {} Curly brackets or braces (Tuborg brackets?! Really, Denmark?)
  • [] Square brackets
  • <> Angle brackets or chevrons

  • <= less-than-equal
  • >= greater-than-equal

  • % modulo operator

the remainder from the division of the first argument by the second

Floating point numbers

Right wow, googling that is… interesting. It’s like the internet has conspired against you to assume that if you’re looking for that — you already know what it is. I get it after a while, but I am still amused by the results this provides. Even the Simple English wiki article is kinda wtf.

An integer is a whole number (not a fraction) that can be positive, negative, or zero. Unlike floating point numbers, integers cannot have decimal places.

That makes sense. This does not:

As the name implies, floating point numbers are numbers that contain floating decimal points.

Implies?! What no, it implies nothing. And wtf is a floating decimal point. 😡 Getting annoyed now.

Later on in the exercise, I’m searching for python floating point number and the explanation I get then is sooo much better.

As a general rule of thumb, floats have a decimal point and integers do not have a decimal point. So even though 4 and 4.0 are the same number, 4 is an integer while 4.0 is a float.

Thank you! 🙌


4, 5, 6: Variables and Names

I’ve heard that some people do not enjoy the instruction style in Learn The Hard Way. I’m not one of those people. I think this is great. And I also very much enjoy the humor and language.

Programmers use these variable names to make their code read more like English and because they have lousy memories. If they didn’t use good names for things in their software, they’d get lost when they tried to read their code again.

But I wonder, what if the tutorial replaced “they” with “we”…? 👌

Popular variable names are also i, x, and j.

round() is a function to round off decimal numbers.

f"here are some words and a {variable}" is an f-string.

format() can apply format to a string that’s already created.


7, 8, 9, 10: Printing

I can use both single and double quotes for making strings.

typically you’ll use single-quotes for any short strings

\n is a new line character (that I recognize from InDesign!😆)

print("""
Hey, this is kinda like a fenced code block in markdown.
But with quotes (either single or double) not backticks.
""")

Examples of escape sequences supported by Python:

\\ one backslash
\" a double-quote
\r reset to beginning of line aka a carriage return
\N{euro sign} a named unicode €
\u20AC" a 16-bit hex value of €

There are more. And Zed really wants me to memorize them all right now, but this time I’m going to rebel and move on.


11, 12, 13: Getting input

input("Where are you? ") is a built-in function and can contain a prompt like this question.

Hello pydoc! 👋

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py Does this mean I‘ve got an older version of pydoc? 🤔

python -V
Python 2.7.10

Yes. I have two versions of Python installed, since I’m now using python3.6 to run scripts. Not digging deeper into this now, just making a note of it and moving on.

Arguments! I know what you are. ✌️

import just what you plan to use, this is neat! I can see how this makes longer scripts more readable for humans.

modules gives us features

argv is the “argument variable”


15, 16, 17: Reading and Writing Files

pydoc hm… version… oh
pydoc3.6 open will get me reading the right version 💪

This exercise teaches reading a file and two different ways of getting the filename:

  • as an argument when you run the script
  • or as input while you run the script

Why is one way of getting the filename better than another?

I think that has something to do with who/how the filename is provided. If the script can know upfront which file to use, then the argument works. But if a person needs to type it in, then the other way has a benefit of the person not having to know in advance that a filename is needed.

close() I’m a bit confused about which variable to use here. Revisit later.

Here are the methods and functions Zed wants me to learn now:

  • close — close the file, apparantly a smart thing to remember
  • read — read files! this is cool
  • readline — read one line of text
  • truncate — this will empty the file?! hm…
  • write('stuff') — ooo, write to the file, mwahahaha
  • seek(0) — move to the beginning of the file

Okay,so open is friendly and will make you explicitly add a w parameter if you want to write to the file. It will also truncate the file first for you, so no need to do that.

r is read mode and the default for the open() function.

Close the current file and forget everything we know about it (including the filename and the current line number).

The current file. Is it one or can it be multiple?

Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery

“invoking the import machinery” 😆

✅ Played around in the console to import datetime and print out the date and time.


18, 19: Functions

  • def creates a function
  • use names with letters, numbers and _
  • open parenthesis right after the function name
  • separate arguments by commas
  • remember to close with ):
  • indent all lines inside the function with 4 spaces
  • dedent after the function

…and then run/call/use the function with
my_function_name(values, anything, something)

Study drill: Wrote my very own palermo function! 😆

def palermo(pasta_count, wine_count, step_count):
    print(f"You’ve probably had pasta {pasta_count} times…")
    print(f"…and {wine_count} glasses of nero d'avola. 🍷")
    print(f"Also walked {step_count} steps in total.\n")

dayz = input("\n> How many days have you been in Palermo now? ")

print(f"🇮🇹  You’ve been here {dayz} days! Lucky you.")
palermo(int(dayz) * 1.5, int(dayz) * 3, int(dayz) * 12000 )

✌️

Yay. This is super fun and it feels like I’m making substantial progress.