As part of my mission to Get Unstuck, I’ve now realized that if I’m not sufficiently inspired to tackle moving forward — backtracking can be great. Moving beats standing still! But I’ve been too impatient to discover how useful it can be to revisit previous chapters in Learn Python the Hard Way. During exercises 27–39, the content felt pretty familiar. I’ve seen lists and loops and ifs before, none of it was brand new magic. But maaaybe I breezed through this part of the book too fast. Let’s revisit!


The Truth Terms

Logic on a computer is all about seeing if some combination of these characters and some variables is True at that point in the program.

andornot!===>=<=TrueFalse

  • Exercise 27 is about memorizing truth tables.
  • Exercise 28 is practicing more complex Boolean logic.
  • “Burn Boolean logic into your brain” 😜

Note to self: those two ☝️ are worth revisiting when I need easy-going backtrack studying.

Arithmetic Operators

These are the basic ones + - * /, along with more complex ones like Modulus % and Exponent ** that I really don’t see myself needing any time soon, but this one is worth noting:

| // | Floor Division | 9//2 = 4 |

Comparison Operators

== equal
!= not equal
<> deprecated in favour of !=
> larger than
< smaller than
>= larger than or equal
<= smaller than or equal

Assignment Operators

| | | | ---- | ----------------------------------------- | ------------------------------------- | | = | Assign value from right side to left side | 👌 | | += | Add AND (“increment by”) | x += 1 does the same as x = x + 1 | | -= | Subtract AND | x -= 1 does the same as x = x - 1 |

if-statement 🔀

Straight line scripts are what we started off with. But time to get more adventurous!
Branches are neat, this means we can make forks in the road and diverge.

if, elif, else will let me write scripts that decide things.
Whoooa! Fork in the road! Wanna go left, right or keep going?!

else

This is my safety valve that will keep me out of trouble. Add an else on every if-statement first, but delete if it turns out to be pointless. Most of the time though, it is justified.


Hello lists and loops 👋

# Basic list and for-loop example
beers = ['an ipa', 'a stout', 'a porter']

for beer in beers:
    print("I’ll have", beer, "plz.")
beers[1] The second beer is a stout, because 0 based indexing.
beers[2] But if we say the beer “at 2” that would be the porter and a cardinal number.
beers[1:] Slicing away the IPA, and getting the stout and the porter.
beers[-1] Negative indexing like this would serve up the last item.
# How to copy a list
beers = ['an ipa', 'a stout', 'a porter']
morebeers = beers[:]

for secondbeer in morebeers:
    print("I’ll have one more", secondbeer, 'thanks.')
  • Using i as a variable is a convention. (Now I know this. But I still remember how often I got confused by that when I first started reading JavaScript.)
  • range() is a built-in function (or actually an immutable sequence type).
  • append() is a method I can use on lists, to add items to it.

For or while?! 🤔

  • for-loops are guaranteed to end, this is called bounded looping.
  • while-loops are not, and with unbounded looping we need to be very sure that a boolean test becomes False at some point or the block of code will keep going forever.

I should use while-loops sparingly. They are easy to grasp, basically an if-statement with a jump back to the beginning. But they are tricky to get right in my scripts.

i = 0
mylist = []

while i < 5:
    i += 1 # an incrementer is often needed in while-loops
    mylist.append(i)

# will make this list [1, 2, 3, 4, 5] using a while-loop
i = 0
anotherlist = []

for i in range(1,6):
    anotherlist.append(i)

# also make the same list [1, 2, 3, 4, 5] with a for-loop 🥇

When I find myself using a while-loop, I should probably try to figure out how to make it a for-loop. But this is a good expection:

while True:
    # this is one way that I probably do want to use a while-loop

Need some guidelines? 🤷‍♀️

Exercise 36 has some rules and tips for designing and debugging. These can be helpful for me to revisit when I’m coding something I can’t get to work.

  • Group if-elif-else like a set.
  • Avoid nesting if-statements more than two levels.
  • Make simple boolean tests. (Do calculations in variables earlier in the function!)

Debug with printing 🐛

print(">>>> show me something=", something)

Misc keywords that I want to make a note of now:

assert ensure that something is true
break stop this loop
del delete from dictionary
except do this if an exception happens
exec run a string a Python
global declare a global value
lambda create a short anonymous function
pass this block is empty
raise raise an exeption when things go wrong
try try this! and if exception go to except

Remember that…

  • String formats with % are a thing that can be useful to know that exist. This was how to format strings before f-strings came along in Python 3.
  • Off-by-one errors are something I’m going to meet a lot.
  • Hm… there is something about function calls I don’t understand yet. Get back to that soon.