Successfully reported this slideshow.
Upcoming SlideShare
×

# Learning Python - Week 1

4,524 views

Published on

Based on Zed Shaw's "Learn Python the Hard Way," this is a review of Exercises 1 - 12 in that text. For non-computer-science students and learners. Updated with new slides Jan. 12, 2014. Introduces math, print statement, variables, format strings, raw_input().

Published in: Education, Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• If you need your papers to be written and if you are not that kind of person who likes to do researches and analyze something - you should definitely contact these guys! They are awesome ⇒⇒⇒WRITE-MY-PAPER.net ⇐⇐⇐

Are you sure you want to  Yes  No
• I pasted a website that might be helpful to you: ⇒ www.HelpWriting.net ⇐ Good luck!

Are you sure you want to  Yes  No
• I'd advise you to use this service: ⇒ www.WritePaper.info ⇐ The price of your order will depend on the deadline and type of paper (e.g. bachelor, undergraduate etc). The more time you have before the deadline - the less price of the order you will have. Thus, this service offers high-quality essays at the optimal price.

Are you sure you want to  Yes  No
• Does Penis Size REALLY Matter? The truth comes out... ●●● https://bit.ly/30G1ZO1

Are you sure you want to  Yes  No

Are you sure you want to  Yes  No

### Learning Python - Week 1

1. 1. Learn Python the Hard Way Exercises 1 – 12 http://learnpythonthehardway.org/
2. 2. Exercise 1: Quotation marks Which of these will throw an error? a) b) c) d) print "They wouldn't do that." print 'They wouldn't do that.' print 'They wouldn't do that.' print "They wouldn't do that.'
3. 3. Exercise 2: Comments Which line is commented out? a) print "Fourscore and seven years" b) # print "Fourscore" c) print "Eighty years" # this is fourscore years
4. 4. Which one will print blank lines? 1) print "Fourscore and seven years ago" # # print "Our fathers brought forth" 2) print "Fourscore and seven years ago" print print print "Our fathers brought forth"
5. 5. Exercise 3: Math Which one of these would give a different answer than the others? a) b) c) d) e) 4+6/3+5 4 + (6 / 3) + 5 (4 + 6) / 3 + 5 (4 + 6 / 3) + 5 4 + (6 / 3 + 5)
6. 6. PEMDAS • • • • • • P E M D A S () exponents, e.g. 10**2 * / + – But …
7. 7. PEMDAS is not the whole story •P () • E exponents, e.g. 10**2 • MD * / % • AS + – See http://en.wikipedia.org/wiki/Order_of_operations
8. 8. Exercise 4: Math What is the answer Python will give to all of these? a) b) c) d) 5 > 10 10 < 2 1444 < 1443 1>2
9. 9. Exercise 4: Math True or False: < > <= >= == != More about True and False to come, in Zed’s exercise 27. (Note: In Python, these values always start with an uppercase letter.)
10. 10. Exercise 5: Integers and floats What is the answer Python will give to this? 1/4
11. 11. Exercise 5: Integers and floats What is the answer Python will give to this? 1 / 4.0
12. 12. Learn this! >>> 1 / 4 0 >>> >>> 1 / 4.0 0.25 >>> integer float
13. 13. Exercise 6: Modulus What is the answer Python will give to all of these? a) b) c) d) 4%2 10 % 2 144 % 12 100 % 25
14. 14. Modulo The modulus operand is commonly used to find out if a number is odd or even. % Don’t get confused: In Python, the percent sign (yes, the same character, but used differently) is also used for format strings, as seen first in Exercise 5. (You’ll be seeing even more of that!)
15. 15. Modulo (also modulus) What will this return? (That is, what answer will Python give?) 115 % 11
16. 16. Variables
17. 17. Variables Name apple Value
18. 18. Variables
19. 19. Variables
20. 20. Variables Name Value
21. 21. Variables Name Value
22. 22. Variables Name Value 57
23. 23. Variables Name Value
24. 24. Any questions?
25. 25. “Format strings” • %s %d %r %f • Each one is slightly different • They are a kind of shorthand for working with variables in Python • NOTE! These are NOT variables! • Zed also calls these “format characters” • NOTE! This is NOT modulus!
26. 26. %r %s %d Not all the same.
27. 27. Notice how %r in this case returns something very different from %s
28. 28. String formatting continued • • • • %s string: use this for text %d use this for integers (no decimal places) %f float: shows up to 6 decimal places %r representation: works for numbers and strings, but (usually) adds quotation marks * * Zed says, “The %r is best for debugging.” (But you don’t really know what debugging is.)
29. 29. The value of “play” (an essential part of learning to code)
30. 30. n newline (line break) t tab (indent) The backslash is “the escape character.”
31. 31. Putting things together a = "Mary had a little lamb." b = "Its fleece was white as snow." c = "And everywhere that Mary went" d = "The lamb was sure to go." print "nn%snt%sn%snt%snn" % (a, b, c, d) What would happen if the order were changed to: (d, c, b, a)
32. 32. Running that program …
33. 33. Escapes Zed says memorize all of these. I don’t think that’s necessary. But do memorize this: The backslash is the escape character.
34. 34. Escapes!
35. 35. raw_input( ) Zed’s exercises 11 and 12 introduce this.
36. 36. raw_input( )
37. 37. Learn Python the Hard Way Exercises 1 – 12 (we’re just getting started)