Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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

4,800 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
  • ⇒⇒⇒WRITE-MY-PAPER.net ⇐⇐⇐ has really great writers to help you get the grades you need, they are fast and do great research. Support will always contact you if there is any confusion with the requirements of your paper so they can make sure you are getting exactly what you need.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • You can try to use this service ⇒ www.HelpWriting.net ⇐ I have used it several times in college and was absolutely satisfied with the result.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • If we are speaking about saving time and money this site ⇒ www.WritePaper.info ⇐ is going to be the best option!! I personally used lots of times and remain highly satisfied.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Have You Seen Mike Walden's new holistic acne System yet? It's called Acne No More I've read the whole thing (all 223 pages) and there's some great information in there about how to naturally and permanently eliminate your acne without drugs, creams or any kind of gimmicks. I highly recommend it - it's very honest and straightforward without all the hype and b.s. you see all over the net these days. Here's the website where you can get more information ▲▲▲ https://tinyurl.com/ybbtmvh8
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Are You Heartbroken? Don't be upset, let Justin help you get your Ex back. ■■■ http://t.cn/R50e2MX
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

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)

×