Thanks to my company I’ve had the time & support to continue learning new skills while I’m at work. For the last few months of 2021 I started seriously learning Python, something I’ve wanted to do for years now. Here’s the notes I compiled for the beginning of my Python journey!
Why: I like hands-on and I like theoretical – programming’s seemed like the perfect thing since I first understood what programming is (which, sadly, was in college).
My Background: I did tiny amounts of programming in college and did undergrad research at Pittsburgh Supercomputing Center, where I interacted with a Linux system for the first time. My undergraduate degree is in mathematical biology, so we did have some courses that required a light, mostly un-instructed bit of programming.
Why make a post about it? Because it’s (a) more fun than just taking notes, (b) helps keep my thoughts together, and (c) might inspire some other programming newbies to get out there and learn (and help them learn from my mistakes).
Python Tutor: visualize working code
The Very Basics: Definitions
REPL = interactive shell
expression = consists of values and operators that reduce down to a single value (i.e. 2+2)
data type = category for values (i.e. integers, floating-point numbers, strings)
integer = value that is a whole number [int]
floating-point = number with a decimal point [float]
string = [str]
string concatenation = putting multiple strings together using the + operator
assignment statements = statements used to store a value in a variable
initialization = the first time a certain variable is assigned a value
camelcase = compression of multiple words into one without underscores in variable names (i.e. looksLikeThis instead of looks_Like_This)
iterable object = an object capable of returning its elements one by one
exits = terminates = stops running
calling and passing:
Boolean values = True and False
Comparison operators/Relational operators = compare two values and evaluate down to a single Boolean value, either True or False (these include equal to, less than, etc.)
Boolean operators = and, or, and not
condition = an expression, in the context of a flow control statement; always evaluate down to a Boolean value
My First Few (very simple) Programs
I made this one because I was interested in the interplay between the str() and int() functions. I think it’s interesting that this code prints plain old 88 whether I use these functions, or just the variable name. This seemed odd to me at first – why would I, or would I, ever use str(int()) then?
It turns out values a user responds to input() are strings, not integers. That’s why str(thisYear – 2020) did not work; the value of variable thisYear needed to be changed to an integer value bfeore math could be done.
You can see I had a little trouble with the syntax. It turned out to just be an errant indentation; that “else” should have gone back to zero indent, and once I fixed that as shown the code worked.
Crown shyness is a phenomenon where certain trees branch closer to one another than others in a forest canopy. I thought this would be a good candidate for a beginner-level practice code; I started thinking through how this would look, and I’ll work more on it during the first half of 2022.
The First Two Weeks – September 1st to September 10th
It was admittedly hard to get into a learning schedule. It hasn’t even been that long since I took a class; it’s just that COVID has sapped my attention span. So even though I intended to put in 2 hours every week to learning Python, over the last ten days I only put in about 2.5 hours total. And that’s okay. Working on Python at all has made me excited to keep working on it.
I started out with Automate the Boring Stuff, and that’s what I’ll be continuing with these next few weeks. So far I’m up to the section on int(), str(), and float() functions. That means I finished chapter 1! I also used some of the hours I have set aside to just play around with Python – learning by doing, if you will. I’ve found that works for me.
September 13th – September 23rd: Flow Control Statements
This has been a bit of a slow two weeks on the continued education front. There’s been a lot going on outside of work for me (family visit upcoming!) so my focus has honestly not been the best. However, I did learn to understand if, else, elif, and while statements! These were things I used to vaguely understand but didn’t really know how to use. No more! Over the next two weeks I’ll be finishing up the flow control chapter and moving on to functions.
October 6th: break Behavior
Something that truly confused me at first was the break statement. To try and learn, I made a code and ran it with and without break.
And, without break:
It never ends! ‘I am less than 10’ FOOOOREVER!
And so I understand better now: over a range of 5 (i = 1, i = 2, … , i = 5) i will always be less than 10. And so the program continues to print ‘I am less than 10.’ But why does it only print once when I add the break statement? Time to go back to those Boolean values.
As I understand it: as soon as the for statement proves true, the break statement quits the loop. When i = 1, the first value in range(5), i < 10 and therefore the statement is True. The loop quits, thanks to break.
Let’s try and make it print ‘I am less than 10’ five times, which was my original intention.
Well, that was simple enough. 👻
break vs. continue
Another concept I didn’t grasp at first was the continue statement. Here again I tried to get a string to print a finite number of times, and got an infinite cache of “Still less, yup.”
As it turns out, by the way, the continue doesn’t really do much in the program above. If I comment it out, I get the same result.
Understanding the Unary Boolean Operator, not
No, that heading is not a “not” joke. I hit some confusion on “truthy” and “falsey” values; when trying to learn more about that topic, I hit a snag on the not operator. This is the code I was looking at:
I understood the layman’s idea of the code: ask a user to type in their name, and if they leave it blank ask again until they enter something. Then move on to the question about how many guests the user will have.
What confused me was that second line, “while not name:”. As I now understand it,
- the first line defines the variable name as an empty string
- the second line, the one that confused me, initiates a while loop such that if a user enters a blank string for “name” (see line 4) the loop continues
- As long as the condition is True, which it will be as long as an empty string is entered, the while loop continues
- Once the condition is False a.k.a. the user enters a non-empty string, the program prints the guests question
The Collatz Sequence
At the end of chapter 3 of Automate the Boring Stuff, there’s a practice project centered around the Collatz sequence. The idea is to create a program where the user inputs whatever integer they want and, using the Collatz sequence, eventually the program returns 1 and exits. Here’s where I got stuck initially:
As the code above is, you get an output of the same number over and over and over again on repeat forever (cue Squidward foooorever! .gif). I need that number value to update. What’s happening right now is this:
So to fix this, I went home for the weekend, coded nothing, then came back and checked it out again on the next Tuesday. Seriously, though, I made some changes after the weekend and ended up with this:
Why did it keep calling the function, even after 1 appeared? This is progress, but still not perfection! Every time it hits 1, the pattern restarts. I want the collatz function to end once I get the value 1.
Well, after almost throwing my computer out a window I figured it out. It turned out to be a pretty simple fix: REMOVING THE SECOND WHILE STATEMENT!
Dissecting What Happened
What happened when I removed that second while statement, the one outside the defined function? I am, of course, no expert, but it seems that the issue was in the realm of local vs. global variables.
My suspicion is that the returned value of number (from the last line of the function, ‘return number’) doesn’t go to the global scope since there’s no global statement. I wanted to test this, so I made a little baby program:
It’s as I suspected – that returned value isn’t global!
Oh and hey, what’s funnier than 24?
By the way, I made some tea and updated that silly cat.py code, for fun. Here’s what I ended up with: