More notes from Learn Python The Hard Way (continued after exercises 1–19). And again; These are my notes. You can read them if you want — but I make no attempt at this being useful for anyone else.


20: Functions and Files

seek() is built-in function that:

  • can change the file object’s position
  • will complain if it doesn’t get at least one argument
  • 1st arg is offset (number of characters from a reference point)
  • 2nd arg is from_what that works like this:
    • 0 is the beginning of the file (this is the default if omitted)
    • 1 is the current file position
    • 2 is the end of the file

Except… hm. It seems that “only seeks relative to the beginning of the file are allowed” for text files. Which explains why I couldn’t get the end of the file reference to work when playing around in the console with this.

Python doesn’t need to separatly unpack arguments when I write a function. Which is what I did in the last exercise — but apparently it confused me again now, so let’s repeat:

def whatever_function(anything_argument):
# and the argument is now ready to be used as a variable

✅ “rewrite the script to use the shorthand notation +=Yes. Did that. It’s working! \o/


21: Functions Can Return Something

def add(a, b):
    print(f"ADDING {a} + {b}") # this is just print
    return a + b # this is where the math actually happens

# define a variable and run the function with two arguments
# the variable then gets a value returned from the function
age = add(30, 10)

Wohooo! Had to sleep on it, but nailed the tricky extra credit puzzle.

Math (and Python) operation order repetition 🎓

(parentheses first, then) do the multiplication and division in one step, from left to right, then do the addition and subtraction in one step from left to right


22: What Do You Know So Far?

This is a memorization exercise. I’ve got a lot of the symbols down fine, so I’m going to focus on the specific Python use descriptions that are less fluent for me yet:

Symbols / characters

# write a comment
() group some data (this is called a tuple!)
, separate multiple arguments
" make a string
' also make a string, but maybe a shorter one
= assign the value on the right to a variable on the left
% calculate the remainder from the division (modulus operator)
>= test if a value is more than or equal
<= test if a value is less than or equal
{} embed a variable in a string
): close a function definition

\n add a newline character
\t add a tab character
\r add a carriage return (reset to beginning of line)

Built-in functions that are always available

✏️ print() output stuff
🔨 format() apply format to a string that is already created
🚢 float() return a floating point number (with a decimal point, not an integer)
✂️ round() round off decimal numbers
input() ask questions in the console
💯 int() convert a number as a string to an integer
📂 open() open a file and return a file object

Methods of file objects

Also built-in, but not always available. I can use them after a file object has been created.

👀 read() read content from the file
🧐 readline() read just a single line from the file
✏️ write() write the content of a string to the file
🔎 seek() change the file object’s position
🚫 close() close the file and forget everything about it

Hmmmmm… It seems that in writing up these lists with proper headings, I also stumbled upon learning the difference between functions and methods in Python. Neat! 🎉

Statements

🙋‍♀️ def define my own function and give it a useful name
💁‍♀️ return return a value from the function

(Very pleased with these emoji!)

More stuff

f"here are some words and a {variable}" # this is an f-string
# from the sys module, import a function for taking arguments
from sys import argv
# add one more argument in addition to the script itself
script, filename = argv
open(filename)      # without the optional string for mode
open(filename, 'r') # r for read mode (which is default)
open(filename, 'w') # w for write mode (careful! can delete)

Some observations…

…after this week of working on the Learn Python The Hard Way exercises. 💪

  • I’ve picked up a lot after just some hours each day for seven days.
  • Learning thoroughly like this different from how I’ve often learnt before. And I’m really enjoying it! This book is encouraging me to be patient and to practice intentionally.
  • I am building a language to describe what I’m learning, and this is different from how I (even now after all these years) can talk about HTML/CSS. This is useful in all sorts of ways!!

Difference explained, with my current cocktail as an example:

  • 🙃 “I’m having a red-ish drink with a cool name I spotted in the menu. It’s got ice!”
  • 🤓 “This is a negroni. It’s an apértif, and served in a short tumbler. The ingredients are equal parts Campari, Vermouth (rosso) and gin. The first one had orange peel, but the other bartender forgot that from this second one. (It’s still pretty good.)”

🍸