Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Learning Python - Week 3
1. Learn Python the Hard Way
Exercises 20 – 26
http://learnpythonthehardway.org/
2. 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()
6. What is the result?
a) case = 5
case = case + 1
b) case = 5
case += 1
c) case = 5
case –= 1
7. What is the result?
a) case = 5
case = case + 1
b) case = 5
case += 1
c) case = 5
case –= 1
8. 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”
10. Exercise 21
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.
12. 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?
13.
14. Exercise 21
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))))
15. 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.
18. Exercise 22
Exercise 22: Do it or don’t do it.
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.
24. Exercise 24
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: """
29. A little break: TextWrangler settings
Setting preferences in TextWrangler:
1. Line numbers (turn them on)
2. Soft wrap (lines)
3. Appearance of soft-wrapped
lines
4. Colors
34. 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
35. 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
38. Exercise 25
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?
39. Exercise 25
What does this teach you about the way the function
sort_words() really works?
41. Ex. 25: What does each one do?
(something is a variable name)
something.split(' ')
sorted(something)
something.pop(0)
something.pop(–1)
42. Exercise 25
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.
43. 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.
46. Heads up! Exercise 27 is important. Zed asks you to memorize and tells you how.
Do what he says.
47. 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]
48. Learn Python the Hard Way
Exercises 20 – 26
(we are getting smarter, little by little)
Notes de l'éditeur
SOURCE http://learnpythonthehardway.org/book/
If you’re not sure what each one does, REVIEW Zed’s lessons, starting with Exercise 15.
CODE EXAMPLE. Exercise 20.
CODE EXAMPLE. Exercise 20. What is the position? The number of characters from 0 up to … in this case, 52. So we start at 53.
CODE EXAMPLE. Exercise 20.
CODE EXAMPLE. How to open pydoc.
Exercise 21.
CODE EXAMPLE. See if you can figure this out.
CODE EXAMPLE. See if you can figure this out.
Do you see why this one cannot work properly? The variable y does not exist outside the function.
You MUST start with the innermost pair of parentheses – then work your way OUTWARD, pair by pair. Look at each one of the functions as you work out the contents of each pair of parentheses.
LPTHW Exercise 22: “There is no failure, only trying.”
But I agree with Zed, this is how we learn. If you do it, it will help you.
LPTHW Exercise 22: Mine. In part. My text file for this exercise is more than 300 lines of notes. My notes are written for me. They are a method for learning.
LPTHW Exercise 23: Zed asks you to visit four websites that host “code repos” (repos = repositories) and look at Python code that others have written and stored there, in public view.
LPTHW Exercise 23: Zed asks you to go to some specific websites and look at other people’s code. This is Bitbucket.
LPTHW Exercise 23: I would really like you to DO THIS. Spend about an hour. Follow Zed’s instructions. See what you find out! (This is GitHub.)
LPTHW Exercise 23: If you start to feel curious about the sites you visit, you can learn more about the idea behind putting code on one of these sites if you explore this FREE online course from Code School: http://www.codeschool.com/courses/try-git (Up to you – this is optional!)
LPTHW Exercise 24.
LPTHW Exercise 24: Mine. In part. Note the comments I added -- for my own use.
LPTHW Exercise 24: Mine. In part.
LPTHW Exercise 24: Mine.In part.
LPTHW Exercise 24: Mine. You will need to PLAY with this to understand it. It eliminates some of the earlier code, but it still works the same way.
You are spending a lot of time in TextWrangler. You should customize it to suit you and your work.
TextWrangler (1) – Real programmers always use line numbers in their text editor program. Sometimes Zed refers to the line number.
TextWrangler (2) – Set “soft wrap” so that lines of code wrap down instead of going endlessly to the right.
TextWrangler (3) - continued
TextWrangler (4) – continued
LPTHW Exercise 25.
LPTHW Exercise 25.
CODE EXAMPLE. LPTHW Exercise 25. This is what I get when I run: help(ex25) -- in Python, after the import statement. WHY?
LPTHW Exercise 25. This is how Python works. A module has an embedded description IF SOMEBODY WROTE ONE in the file. It’s another fabulous use for the “triple double-quotes”!
CODE EXAMPLE. LPTHW Exercise 25. What does this tell you about how Python works?
LPTHW Exercise 25.
LPTHW Exercise 25.
LPTHW Exercise 25. We use these commands to find and manipulate strings of text in long documents. Python can do lots of useful work.
CODE EXAMPLE. LPTHW Exercise 25. What does this tell you about how Python works?