SlideShare a Scribd company logo
1 of 48
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 = 5
     case = case + 1

b)   case = 5
     case += 1

c)   case = 5
     case –= 1
What is the result?
a)   case = 5
     case = case + 1

b)   case = 5
     case += 1

c)   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” — 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.
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.
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




   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 22




Exercise 22: Do it or don’t do it.
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.
Exercise 23




Exercise 23: Reading code.
  (Other people’s code.)
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: """
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
   lines
4. 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!
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?
Exercise 25




What does this teach you about the way the function
sort_words() really works?
Exercise 25




Do you understand it now?
Ex. 25: What does each one do?
(something is a variable name)


    something.split(' ')
    sorted(something)
    something.pop(0)
    something.pop(–1)
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.
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.
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 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)

More Related Content

More from Mindy McAdams

New skill sets for journalism
New skill sets for journalismNew skill sets for journalism
New skill sets for journalismMindy McAdams
 
Journalism blogs: An introduction
Journalism blogs: An introduction Journalism blogs: An introduction
Journalism blogs: An introduction Mindy McAdams
 
Why Your Newsroom Should Be Using WordPress - ONA13
Why Your Newsroom Should Be Using WordPress - ONA13Why Your Newsroom Should Be Using WordPress - ONA13
Why Your Newsroom Should Be Using WordPress - ONA13Mindy McAdams
 
Journalism's Future: Journalism, Not Newspapers
Journalism's Future: Journalism, Not NewspapersJournalism's Future: Journalism, Not Newspapers
Journalism's Future: Journalism, Not NewspapersMindy McAdams
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 CanvasMindy McAdams
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOMMindy McAdams
 
HTML and Responsive Design
HTML and Responsive Design HTML and Responsive Design
HTML and Responsive Design Mindy McAdams
 
Design Concepts and Web Design
Design Concepts and Web DesignDesign Concepts and Web Design
Design Concepts and Web DesignMindy McAdams
 
Freedom of Speech - Louis Brandeis
Freedom of Speech - Louis BrandeisFreedom of Speech - Louis Brandeis
Freedom of Speech - Louis BrandeisMindy McAdams
 
Networked Information Economy / Benkler
Networked Information Economy / BenklerNetworked Information Economy / Benkler
Networked Information Economy / BenklerMindy McAdams
 
Convergence Culture / Jenkins
Convergence Culture / JenkinsConvergence Culture / Jenkins
Convergence Culture / JenkinsMindy McAdams
 
How to Share Your Digital Stories
How to Share Your Digital StoriesHow to Share Your Digital Stories
How to Share Your Digital StoriesMindy McAdams
 
Global Journalism Research
Global Journalism ResearchGlobal Journalism Research
Global Journalism ResearchMindy McAdams
 
Social media / professional use of Twitter
Social media / professional use of TwitterSocial media / professional use of Twitter
Social media / professional use of TwitterMindy McAdams
 
Social Media and Journalists: Part 4
Social Media and Journalists: Part 4Social Media and Journalists: Part 4
Social Media and Journalists: Part 4Mindy McAdams
 
Social Media and Journalists: Part 1
Social Media and Journalists: Part 1Social Media and Journalists: Part 1
Social Media and Journalists: Part 1Mindy McAdams
 

More from Mindy McAdams (20)

New skill sets for journalism
New skill sets for journalismNew skill sets for journalism
New skill sets for journalism
 
Journalism blogs: An introduction
Journalism blogs: An introduction Journalism blogs: An introduction
Journalism blogs: An introduction
 
Why Your Newsroom Should Be Using WordPress - ONA13
Why Your Newsroom Should Be Using WordPress - ONA13Why Your Newsroom Should Be Using WordPress - ONA13
Why Your Newsroom Should Be Using WordPress - ONA13
 
Journalism's Future: Journalism, Not Newspapers
Journalism's Future: Journalism, Not NewspapersJournalism's Future: Journalism, Not Newspapers
Journalism's Future: Journalism, Not Newspapers
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 Canvas
 
Beginning jQuery
Beginning jQueryBeginning jQuery
Beginning jQuery
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101
 
HTML and Responsive Design
HTML and Responsive Design HTML and Responsive Design
HTML and Responsive Design
 
Design Concepts and Web Design
Design Concepts and Web DesignDesign Concepts and Web Design
Design Concepts and Web Design
 
Learning Python
Learning PythonLearning Python
Learning Python
 
Freedom of Speech - Louis Brandeis
Freedom of Speech - Louis BrandeisFreedom of Speech - Louis Brandeis
Freedom of Speech - Louis Brandeis
 
Networked Information Economy / Benkler
Networked Information Economy / BenklerNetworked Information Economy / Benkler
Networked Information Economy / Benkler
 
Convergence Culture / Jenkins
Convergence Culture / JenkinsConvergence Culture / Jenkins
Convergence Culture / Jenkins
 
How to Share Your Digital Stories
How to Share Your Digital StoriesHow to Share Your Digital Stories
How to Share Your Digital Stories
 
Global Journalism Research
Global Journalism ResearchGlobal Journalism Research
Global Journalism Research
 
Social media / professional use of Twitter
Social media / professional use of TwitterSocial media / professional use of Twitter
Social media / professional use of Twitter
 
Global Journalism
Global JournalismGlobal Journalism
Global Journalism
 
Social Media and Journalists: Part 4
Social Media and Journalists: Part 4Social Media and Journalists: Part 4
Social Media and Journalists: Part 4
 
Social Media and Journalists: Part 1
Social Media and Journalists: Part 1Social Media and Journalists: Part 1
Social Media and Journalists: Part 1
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri 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()
  • 3. Note that f.seek(0) returns to the start of the file …
  • 4. … but with another number, such as f.seek(52) … reading the file will begin at that position, not at the start.
  • 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”
  • 9. Python’s built-in documentation: Press the down arrow to scroll. Press Q to quit.
  • 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. See if you can figure this out.
  • 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.
  • 16.
  • 17. Exercise 22 Exercise 22: Do it or don’t do it.
  • 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.
  • 19.
  • 20. Exercise 23 Exercise 23: Reading code. (Other people’s code.)
  • 21.
  • 22.
  • 23.
  • 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: """
  • 26. Exercise 24 This is close to Zed’s version. Do not be fooled by his re-use of variable names.
  • 27. Exercise 24 Note how in this version, I changed the variable names. And it works the same way.
  • 28. Exercise 24 This version is the most confusing, but it’s also the most concise.
  • 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
  • 30. TextWrangler – turn on the line numbers
  • 31. TextWrangler – “soft wrap” lines Note: You can also change the font size here. Make it larger if you are feeling eye strain.
  • 32. TextWrangler – appearance of “soft wrap”
  • 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
  • 36. This is what I get when I run: help(ex25)
  • 37. That is because I wrote THIS in my ex25.py file!
  • 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?
  • 40. Exercise 25 Do you understand it now?
  • 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.
  • 44. Exercise 26 (1) This is how the program ran after I had fixed all the errors.
  • 45. Exercise 26 (2) This is how the program ran after I had fixed all the errors.
  • 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)

Editor's Notes

  1. SOURCE http://learnpythonthehardway.org/book/
  2. If you’re not sure what each one does, REVIEW Zed’s lessons, starting with Exercise 15.
  3. CODE EXAMPLE. Exercise 20.
  4. 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.
  5. CODE EXAMPLE. Exercise 20.
  6. CODE EXAMPLE. How to open pydoc.
  7. Exercise 21.
  8. CODE EXAMPLE. See if you can figure this out.
  9. CODE EXAMPLE. See if you can figure this out.
  10. Do you see why this one cannot work properly? The variable y does not exist outside the function.
  11. 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.
  12. LPTHW Exercise 22: “There is no failure, only trying.”
  13. But I agree with Zed, this is how we learn. If you do it, it will help you.
  14. 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.
  15. 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.
  16. LPTHW Exercise 23: Zed asks you to go to some specific websites and look at other people’s code. This is Bitbucket.
  17. 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.)
  18. 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!)
  19. LPTHW Exercise 24.
  20. LPTHW Exercise 24: Mine. In part. Note the comments I added -- for my own use.
  21. LPTHW Exercise 24: Mine. In part.
  22. LPTHW Exercise 24: Mine.In part.
  23. 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.
  24. You are spending a lot of time in TextWrangler. You should customize it to suit you and your work.
  25. TextWrangler (1) – Real programmers always use line numbers in their text editor program. Sometimes Zed refers to the line number.
  26. TextWrangler (2) – Set “soft wrap” so that lines of code wrap down instead of going endlessly to the right.
  27. TextWrangler (3) - continued
  28. TextWrangler (4) – continued
  29. LPTHW Exercise 25.
  30. LPTHW Exercise 25.
  31. CODE EXAMPLE. LPTHW Exercise 25. This is what I get when I run: help(ex25) -- in Python, after the import statement. WHY?
  32. 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”!
  33. CODE EXAMPLE. LPTHW Exercise 25. What does this tell you about how Python works?
  34. LPTHW Exercise 25.
  35. LPTHW Exercise 25.
  36. LPTHW Exercise 25. We use these commands to find and manipulate strings of text in long documents. Python can do lots of useful work.
  37. CODE EXAMPLE. LPTHW Exercise 25. What does this tell you about how Python works?
  38. LPTHW Exercise 26.
  39. CODE EXAMPLE. LPTHW Exercise 26 – top part.
  40. CODE EXAMPLE. LPTHW Exercise 26 – bottom part.
  41. LPTHW Exercise 27 preview
  42. Mindy McAdams - CONTACT – http://mindymcadams.com/