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()• ...
Note thatf.seek(0)returns tothe start ofthe file …
… but withanothernumber, such asf.seek(52)… reading thefile will begin atthat position,not at the start.
Examples usingf.readline()
What is the result?a)   case = 5     case = case + 1b)   case = 5     case += 1c)   case = 5     case –= 1
What is the result?a)   case = 5     case = case + 1b)   case = 5     case += 1c)   case = 5     case –= 1
Using pydoc• Zed suggests you look at the built-in Python  documentation from time to time.• When he says “try pydoc file”...
Python’s built-in documentation:Press the down arrow to scroll.Press Q to quit.
Exercise 21                  Returns• When you want to run a function and use the  result outside the function, you’ll put...
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 e...
Exercise 21   Exercise 21’s study drill is tough,          especially if you are       easily confused by math.what = add(...
Exercise 21what = add(age, subtract(height, multiply(weight, divide(iq, 2))))   There’s no point in anyone explaining it. ...
Exercise 22Exercise 22: Do it or don’t do it.
Exercise 22Exercise 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, th...
Exercise 23Exercise 23: Reading code.  (Other people’s code.)
Exercise 24Practicing what you (should) know• Review of how escape characters work:  n newline  t tab indent   one backsla...
Exercise 24
Exercise 24This is close to Zed’s version. Do not be fooled by his re-use of variable names.
Exercise 24Note how in this version, I changed the variable names. And it works the same way.
Exercise 24This version is the most confusing, but it’s also the most concise.
A little break: TextWrangler settingsSetting preferences in TextWrangler:1. Line numbers (turn them on)2. Soft wrap (lines...
TextWrangler – turn on the line numbers
TextWrangler – “soft wrap” lines                       Note: You can also change the                       font size here....
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  fun...
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  fun...
This is what I get when I run: help(ex25)
That is because I wrote THIS in my ex25.py file!
Exercise 25This is slightly different from what Zed suggests,because I learn a lot from playing with the code.Look at the ...
Exercise 25What does this teach you about the way the functionsort_words() really works?
Exercise 25Do you understand it now?
Ex. 25: What does each one do?(something is a variable name)    something.split( )    sorted(something)    something.pop(0...
Exercise 25This is how I answer the questions on the previous slide.When Zed says “this is a list which you will learn abo...
Ex. 26: Fix someone else’s code• You might not enjoy this exercise, but (like  medicine) it is good for you• Remember: Zed...
Exercise 26(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.
Heads up! Exercise 27 is important. Zed asks you to memorize and tells you how.                               Do what he s...
Some students have paid $29 to download Zed’svideos. You also get a PDF of the complete book,Learn Python the Hard Way. It...
Learn Python the Hard Way         Exercises 20 – 26     (we are getting smarter, little by little)
Learning Python - Week 3
Learning Python - Week 3
Learning Python - Week 3
Learning Python - Week 3
Learning Python - Week 3
Learning Python - Week 3
Upcoming SlideShare
Loading in …5
×

Learning Python - Week 3

3,931 views

Published on

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.

Published in: Education
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,931
On SlideShare
0
From Embeds
0
Number of Embeds
1,315
Actions
Shares
0
Downloads
87
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • 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?
  • LPTHW Exercise 26.
  • CODE EXAMPLE. LPTHW Exercise 26 – top part.
  • CODE EXAMPLE. LPTHW Exercise 26 – bottom part.
  • LPTHW Exercise 27 preview
  • Mindy McAdams - CONTACT – http://mindymcadams.com/
  • Learning Python - Week 3

    1. 1. Learn Python the Hard Way Exercises 20 – 26 http://learnpythonthehardway.org/
    2. 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()
    3. 3. Note thatf.seek(0)returns tothe start ofthe file …
    4. 4. … but withanothernumber, such asf.seek(52)… reading thefile will begin atthat position,not at the start.
    5. 5. Examples usingf.readline()
    6. 6. What is the result?a) case = 5 case = case + 1b) case = 5 case += 1c) case = 5 case –= 1
    7. 7. What is the result?a) case = 5 case = case + 1b) case = 5 case += 1c) case = 5 case –= 1
    8. 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”
    9. 9. Python’s built-in documentation:Press the down arrow to scroll.Press Q to quit.
    10. 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.
    11. 11. See if you can figure this out.
    12. 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 theidea of returns.NOTICE: Are x and y used outside the function?
    13. 13. 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))))
    14. 14. Exercise 21what = 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.
    15. 15. Exercise 22Exercise 22: Do it or don’t do it.
    16. 16. Exercise 22Exercise 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.
    17. 17. Exercise 23Exercise 23: Reading code. (Other people’s code.)
    18. 18. Exercise 24Practicing 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: """
    19. 19. Exercise 24
    20. 20. Exercise 24This is close to Zed’s version. Do not be fooled by his re-use of variable names.
    21. 21. Exercise 24Note how in this version, I changed the variable names. And it works the same way.
    22. 22. Exercise 24This version is the most confusing, but it’s also the most concise.
    23. 23. A little break: TextWrangler settingsSetting preferences in TextWrangler:1. Line numbers (turn them on)2. Soft wrap (lines)3. Appearance of soft-wrapped lines4. Colors
    24. 24. TextWrangler – turn on the line numbers
    25. 25. TextWrangler – “soft wrap” lines Note: You can also change the font size here. Make it larger if you are feeling eye strain.
    26. 26. TextWrangler – appearance of “soft wrap”
    27. 27. TextWrangler – change the colors
    28. 28. 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
    29. 29. 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
    30. 30. This is what I get when I run: help(ex25)
    31. 31. That is because I wrote THIS in my ex25.py file!
    32. 32. Exercise 25This 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?
    33. 33. Exercise 25What does this teach you about the way the functionsort_words() really works?
    34. 34. Exercise 25Do you understand it now?
    35. 35. Ex. 25: What does each one do?(something is a variable name) something.split( ) sorted(something) something.pop(0) something.pop(–1)
    36. 36. Exercise 25This is how I answer the questions on the previous slide.When Zed says “this is a list which you will learn aboutlater,” he means the result at the arrow, above.
    37. 37. 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 bookP.S. When I did this exercise, it took me 17minutes altogether. But I spent a long, long timeon exercises 24 and 25.
    38. 38. Exercise 26(1) This is how the program ran after I had fixed all the errors.
    39. 39. Exercise 26(2) This is how the program ran after I had fixed all the errors.
    40. 40. Heads up! Exercise 27 is important. Zed asks you to memorize and tells you how. Do what he says.
    41. 41. Some students have paid $29 to download Zed’svideos. You also get a PDF of the complete book,Learn Python the Hard Way. It’s a completepackage, all videos and the PDF for one price. [LINK]
    42. 42. Learn Python the Hard Way Exercises 20 – 26 (we are getting smarter, little by little)

    ×