SlideShare une entreprise Scribd logo
1  sur  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)

Contenu connexe

Plus de 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
 

Plus de 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
 

Dernier

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 

Dernier (20)

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
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()
  • 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)

Notes de l'éditeur

  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/