I have started and stopped learning python an embarrassing number of times. Guess what? I’m at it again. For reasons I cannot adequately explain I have never really been able to latch on to it. I am, after all, a networking guy at heart. I have always wanted to write my own code but in some geek form of writer’s block I could never come up with a “why” for programming. As I learned I never made that all important leap into the realm of, “Here’s how I can utilize this to make my life better|cooler|easier.” Ponderous, I know. Well now I find myself overrun with ideas for programs I want to write. I’m brimming with them. Can’t tell you what they are, mind you. I, like most, am waiting for people to know me by my truncated last name (Zuck, ya’ see).
As a form of penance I always start back at the beginning of python. I force myself to read (albeit quickly) the chapter on what python is and why it’s good and then I read the section on how to install python (Windows). As I type this (on my iMac) I am running an Ubuntu VM and a Window 8 VM (God, I hate Windows 8). Python is python is python, of course. I just like to see if anything is different anywhere (which I don’t expect it to be). It’s good to go through the motions, even with OS’ I don’t use as often any more (Windows), and make sure I stay honest about them all.
I own the Internet as well as a few books on python programming so I think I’m pretty well armed with learning material. I also have my subscription to Vivek Ramachadran’s python scripting course. Too add to that I dropped the $30 on Zed Shaw’s Learn Python the Hard Way course. It’s free in HTML format but you can buy the videos and PDF’s to simplify your life and get some worthwhile extras. That and Mr. Shaw says he’ll provide email support. That alone will be worth $30. As I started going through Zed’s course one of the early exercises had me type this:
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
The answer to that equation is 7. After busting out some PEMDAS I found myself blankly staring at the “%” sitting in the middle of the equation. And I could not fathom what it meant. The sad part is I remember not knowing what it was the last time I learned python but I couldn’t remember learning it. How lame is that? I remember not knowing but I don’t remember the result of correcting the not knowing. The older my mind gets the less I like the way it works.
I tried a few math problems to see if I could deduce what it might mean.
print "What is 5 + 4 % 5?:"
print: "Answer: ", 5 + 4 % 5
The answer: 9.
Crap. That didn’t help. Following advice tip #5 from Zed’s introduction (i.e. try to figure it out yourself) I refused to Google it. I kept trying different equations but none caused the “%” to magically reveal its purpose.
print "What is 5 + 16 % 4?"
print "Answer: ", 5 + 16 % 4
The answer to that one is 5. Yeah, I was lost. I’m usually OK at seeing patterns in numbers but as I noted earlier, I’m getting older. So I relented. I Googled. Here is what I learned:
Use #1
In python the “%” has two different uses. First, when used in a math problem like those above, it is used to represent modulus. The modulus of a number is the remainder left when you divide. 4 mod 5 = 4 (you can divide 5 into 4 zero times with a remainder of 4). Similarly, 5 mod 4 = 1 (you can divide 4 into 5 one times with a remainder of 1. I even did the long division to prove it. Check it out.
Use #2
In a string of text the “%” finds a completely different use. Within a string it can be used for formatting. Normally characters enclosed in quotes (” “) are treated literally, not as any type of code. This means that when you enter “5 + 4” python will see it as a text string of 5 + 4 (i.e not math). Python will not see it as a math problem that equals 9. If you enter 5 + 4 into python without the parenthesis, it’s math. Python will crunch the numbers and tell you that it is 9.
To be exceedingly clear, in python:
- “5 + 4” is the text 5 + 4
- 5 + 4 is 9
When you use the % inside a string it tells python that there is something going on inside the string that is not supposed to be taken literally; there is something for python to do. Let’s explore:
Consider this string:
print "I own 10 computers but I seldom use more than 3 at a time."
When run in python it will simply print the exact string of text. Nothing particularly exciting about that.
I can accomplish a similar feat using variables. For example:
comps = 10
usage = 3
print "I own", comps, "computers but I seldom use more than", usage, "at a time."
$ python example.py
I own 10 computers but I seldom use more than 3 at a time.
It works but it’s kinda’ busy. Notice how I had to pull the variables out of the strings in order to get python to turn them into numbers. The following code would NOT produce the desired result:
print "I own comps computers but I seldom use more than usage at a time."
Nor would:
print "I own, comps, computers but I seldom use more than, usage, at a time."
Here are all three and their corresponding output:
comps = 10
usage = 3
print "I own", comps, "computers but I seldom use more than", usage, "at a time."
print "I own comps computers but I seldom use more than usage at a time."
print "I own, comps, computers but I seldom use more than, usage, at a time."
$ python example.py
I own 10 computers but I seldom use more than 3 at a time.
I own comps computers but I seldom use more than usage at a time.
I own, comps, computers but I seldom use more than, usage, at a time.
The last two lines fail.
But check out what the “%” within a text string can do:
comps = 10
usage = 3
print "I own %s computers but seldom use more than 3 at a time." % comps
$ python example.py
I own 10 computers but I seldom use more than 3 at a time.
You don’t actually have to create variables to use this, though. You can do this:
print "I own %s computers but I seldom use more than 3 at a time." % 10
And yes, you can even use substitute multiple variables. For example:
comps = 10
usage = 3
print "I own %s computers but I seldom use more than %s at a time." % (comps, usage)
$ python example.py
I own 10 computers but I seldom use more than 3 at a time.
And finally, this will work, too:
print "I own %s computers but I seldom use more than %s at a time." % (10, 3)
There are actually a bunch of other options for the whole “%” string formatting thing. Substituting text is just the tip of what it can do. Allow me to direct you to a brief and hilarious article that offers further elaboration on the use of string formatting. You can also see the decidedly technical (and not terribly helpful to newbies) info in the python documentation located on the python.org web site.
Cheers,
Colin Weaver