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.

Learning Python - Week 4

3,762 views

Published on

Based on Zed Shaw's "Learn Python the Hard Way," this is a review of Exercises 27 - 34 in that text. For non-computer-science students and learners. Updated with new slides Feb. 2, 2014. Introduces Booleans, if-elif-else, loops, lists.

Published in: Education, Technology
  • These are one of the best companies for review articles. High quality with cheap rates. ⇒⇒⇒WRITE-MY-PAPER.net ⇐⇐⇐ I highly recommend it :)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • My personal experience with research paper writing services was highly positive. I sent a request to ⇒ www.HelpWriting.net ⇐ and found a writer within a few minutes. Because I had to move house and I literally didn’t have any time to sit on a computer for many hours every evening. Thankfully, the writer I chose followed my instructions to the letter. I know we can all write essays ourselves. For those in the same situation I was in, I recommend ⇒ www.HelpWriting.net ⇐.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • If you’re struggling with your assignments like me, check out ⇒ www.WritePaper.info ⇐. My friend sent me a link to to tis site. This awesome company. After I was continuously complaining to my family and friends about the ordeals of student life. They wrote my entire research paper for me, and it turned out brilliantly. I highly recommend this service to anyone in my shoes. ⇒ www.WritePaper.info ⇐.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • How long does it take for VigRX Plus to start working? ♣♣♣ https://bit.ly/30G1ZO1
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Learning Python - Week 4

  1. 1. Learn Python the Hard Way Exercises 27 – 34 http://learnpythonthehardway.org/
  2. 2. New things in ex. 27–34 • Booleans: True, False, logic, operators • if – elif – else: Note the relationship to True and False • loops: 2 kinds, for and while • lists and how to – create a new list – add and delete things from lists – find out what’s in a list
  3. 3. How the Boolean AND works There is a very long bungee jump above a deep river gorge in Africa. You say: “If the bungee jump looks safe and it is cheap, then I will do it.”
  4. 4. How the Boolean AND works “If the bungee jump looks safe and it is cheap, then I will do it.” looks safe = False (you won’t do it) is cheap = False (you won’t do it) You’ll only do it if both conditions are TRUE.
  5. 5. How the Boolean OR works There is a great party Tuesday night, but you are in danger of failing your Wednesday test. You say: “If I finish studying in time or the test is canceled, then I will go to the party.”
  6. 6. How the Boolean OR works “If I finish studying in time or the test is canceled, then I will go to the party.” finish studying = True (can go) test canceled = True (can go) You can go if only one of the conditions is TRUE. Or both. But one is enough.
  7. 7. and True True False False False False False or and True : True and False : and True : True True False False or or or or True False True False : : : : True True True False and False : “If the bungee jump looks safe and it is cheap, then I will do it.” “If I finish studying in time or the test is canceled, then I will go to the party.”
  8. 8. Boolean operators (symbols) 1 != 0 1 == (3 – 2) 5 >= 1 10 <= 100 1 != (3 – 2) 1 == 0 5 <= 1 10 >= 100 True True True True False False False False
  9. 9. Exercise 28
  10. 10. if – elif – else (if statements)
  11. 11. Exercises 29 and 30
  12. 12. Exercises 29 and 30
  13. 13. Exercise 30
  14. 14. Loops Lists
  15. 15. Loops Exercise 32 • Every programming language has loops • A loop typically runs over and over until something makes it stop (different from a usual function, which runs only once) • The “for” loop is a standard kind of loop • for-loops can appear inside a function
  16. 16. Loops Exercise 32 • Standard syntax for starting a for-loop in Python: for fruit in fruits: Note that fruit could be any variable name, like x or a or cat. In this case, there must already be a list named fruits (more about lists in a minute)
  17. 17. Loops Exercise 32 • Also standard syntax (but different) for starting a for-loop in Python: for i in range(0, 9): In this case, we don’t know the name of the list yet. Or maybe this loop does not even use a list.
  18. 18. So, 2 different for-loops Exercise 32 for fruit in fruits: print fruit for i in range(0, 9): print "Hello!" Like functions, loops must be indented. They can have many lines. These are short just to make them simple.
  19. 19. while-loops Exercise 33 • The “while” loop is another standard kind of loop • while-loops can appear inside a function • If you can figure out how to do what you want with a for-loop, then use a for-loop. • If you must use a while-loop, be careful that it will not run forever. If it does, it’s an infinite loop (which is NOT good).
  20. 20. Lists Exercise 32 • In many languages, a “list” is called an “array” (but Zed says we should say “list” for Python) • Lists can be gigantic—there can be thousands of items in one list, or none • The standard format of a list in Python: fruits = ['apples', 'oranges', 'pears', 'apricot s'] Note: If there are numbers in the list, they won’t have quotes around them.
  21. 21. Loops and Lists Exercise 32 These two for-loops do exactly the same thing: fruits = ['apples', 'oranges', 'pears', 'apricots' ] for fruit in fruits: print "A fruit of type: %s" % fruit for y in fruits: print "A fruit of type: %s" % y
  22. 22. Lists Exercise 32 • You actually already saw a list in Exercise 25, when you did this: words = stuff.split(' ') • After that, words contained a list: ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.'] • You can use pop() and len() on any list
  23. 23. Some things we do with lists fruits.pop() fruits.append("bananas") fruits.extend(["plums", "mangoes"]) del fruits[2] print fruits With append() you can add only one item at a time to the list.
  24. 24. Exercises 32, 34
  25. 25. Items in lists Exercise 34: Items in lists can be counted. Items in lists can be referenced by number, but the first number is not 1 — it is 0.
  26. 26. Back to while-loops Exercise 33 • You might think the while-loops act a lot like the for-loops that used range (and you would be right) • However, the while-loops are different • The condition at the start of a while-loop could test for something not numeric, such as “while we are not yet at the end of the file” • Note: for-loops are very common, and while-loops are less so (as Zed says: “Usually a for-loop is better”)
  27. 27. Learning … while-loops Exercise 33 • You really need to play with a lot of the extra credit or “study drills” in Exercise 33 to get this into your brain • I made four different .py files to test all the comparisons that Zed recommends • If you play with this, you can really understand how for-loops and while-loops are different
  28. 28. Zed’s Exercise 33
  29. 29. A word of advice So Exercise 31 is long, but easy. You might feel like now it’s all getting easy … BUT WAIT! No, it’s not. • Exercise 32 introduces the for-loop. • Exercise 33 introduces the while-loop. • Exercise 34 introduces how we navigate through the elements in a list. These last 3 exercises are QUITE challenging!
  30. 30. Indents in Python • The usual way is to indent 4 spaces (not a tab indent — actually type 4 spaces) • If you accidentally type 3, or 5, or something else, and all the others are 4, Python will throw an error • Most programming languages require multiple levels of indents, and these levels (and the format of indenting) are important, so take care
  31. 31. An example of multiple indent levels within a Python program
  32. 32. Learn Python the Hard Way Exercises 27 – 34 (now we know some stuff)

×