learning python - week 3
DESCRIPTION
Based on Zed Shaw's "Learn Python the Hard Way," this is a review of Exercises 20 - 26 in that text. For non-computer-science students and learners.TRANSCRIPT
Learn Python the Hard Way
Exercises 20 – 26
http://learnpythonthehardway.org/
Review: Things we do with files
• Open: f = open(somefile)• Read: f.read()• Erase: f.truncate()• Write: f.write()• Close: f.close()• Rewind: f.seek(0)• Move the “playhead”: f.seek()• Read the current line: f.readline()
Note that f.seek(0) returns to the start of the file …
… but with another number, such as f.seek(52) … reading the file will begin at that position, not at the start.
Examples using f.readline()
What is the result?
a) case = 5case = case + 1
b) case = 5case += 1
c) case = 5case –= 1
What is the result?
a) case = 5case = case + 1
b) case = 5case += 1
c) case = 5case –= 1
Using pydoc
• Zed suggests you look at the built-in Python documentation from time to time.
• When he says “try pydoc file” — do this:– Make sure you are not in Python (look for the $)– Type:python –m pydoc file
– This will give you the help document about “file”
Python’s built-in documentation:Press the down arrow to scroll.Press Q to quit.
Returns
• When you want to run a function and use the result outside the function, you’ll put a return statement inside the function.
• When the function reaches the line with the return statement, the function will stop running (and return the specified value).
• When you call a function that includes a return statement, you must have a variable name to “catch” the returned value.
Exercise 21
See if you can figure this out.
Sometimes exercises for beginners are a bit weird.You wouldn’t write a program like this to do real work.But it’s a good example for trying to wrap your brain around the idea of returns.NOTICE: Are x and y used outside the function?
Exercise 21’s study drill is tough, especially if you are
easily confused by math.
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
Exercise 21
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
There’s no point in anyone explaining it. Just try your best to follow
Zed’s instructions. Like he says, it’s a puzzle.
Exercise 21
Exercise 22: Do it or don’t do it.
Exercise 22
Exercise 22: Do it or don’t do it.
Exercise 22
I did it. Just what Zed says. You should too. If you haven’t done it yet, then put it on your To Do list for this week.
Exercise 23: Reading code.(Other people’s code.)
Exercise 23
Practicing what you (should) know
• Review of how escape characters work:\n newline\t tab indent\\ one backslash\' one single quote
• How the “triple double-quotes” work: """
Exercise 24
Exercise 24
Exercise 24
This is close to Zed’s version. Do not be fooled by his re-use of variable names.
Exercise 24
Note how in this version, I changed the variable names. And it works the same way.
Exercise 24
This version is the most confusing, but it’s also the most concise.
A little break: TextWrangler settings
Setting preferences in TextWrangler:
1. Line numbers (turn them on)2. Soft wrap (lines)3. Appearance of soft-wrapped
lines4. Colors
TextWrangler – turn on the line numbers
TextWrangler – “soft wrap” lines
Note: You can also change the font size here. Make it larger if you are feeling eye strain.
TextWrangler – appearance of “soft wrap”
TextWrangler – change the colors
Ex. 25: You made a “module”!
• You wrote a bunch of functions in one .py file• But … the file does not call any of the
functions• Thus, no functions run when you run the file
Ex. 25: You made a “module”!
• You wrote a bunch of functions in one .py file• But … the file does not call any of the
functions• Thus, no functions run when you run the file• Remember from sys import argv ?• Now you are importing some things you wrote
yourself
This is what I get when I run: help(ex25)
That is because I wrote THIS in my ex25.py file!
This is slightly different from what Zed suggests, because I learn a lot from playing with the code.Look at the line that gave me an error.Why did I get that error?How did I fix it?
Exercise 25
What does this teach you about the way the function sort_words() really works?
Exercise 25
Do you understand it now?
Exercise 25
Ex. 25: What does each one do?
(something is a variable name)
something.split(' ')sorted(something)something.pop(0)something.pop(–1)
This is how I answer the questions on the previous slide. When Zed says “this is a list which you will learn about later,” he means the result at the arrow, above.
Exercise 25
Ex. 26: Fix someone else’s code
• You might not enjoy this exercise, but (like medicine) it is good for you
• Remember: Zed is showing you how to learn• This is why I chose Zed’s book
P.S. When I did this exercise, it took me 17 minutes altogether. But I spent a long, long time on exercises 24 and 25.
(1) This is how the program ran after I had fixed all the errors.
Exercise 26
(2) This is how the program ran after I had fixed all the errors.
Exercise 26
Heads up! Exercise 27 is important. Zed asks you to memorize and tells you how.Do what he says.
Some students have paid $29 to download Zed’s videos. You also get a PDF of the complete book, Learn Python the Hard Way. It’s a complete package, all videos and the PDF for one price.
[LINK]
Learn Python the Hard Way
Exercises 20 – 26
(we are getting smarter, little by little)