Upcoming SlideShare
×

# Learning Python - Week 1

3,230
-1

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
7 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
3,230
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
65
0
Likes
7
Embeds 0
No embeds

No notes for slide
• SOURCE http://learnpythonthehardway.org/book/
• ANSWER: b and d Quotation marks. NOTE: c is okay because it includes an escape character: \’ (It is DIFFERENT from b for that reason.)
• ANSWER: b C has part of the line commented out. In b, the complete line is commented out.
• Related to making comments. The second example will print two blank lines. The top example prints NO blank lines.
• ANSWER: c Because addition would not be done first, otherwise. Only in c will the addition be done first.
• PEMDAS is a mnemonic, but it doesn’t include modulo! (not explicitly)
• See table at bottom - http://en.wikipedia.org/wiki/Order_of_operationsPython is NOT the only language that follows PEMDAS.
• ANSWER: False (with a capital F)
• ANSWER: 0 (see next slide)
• Floats vs. integers. Floats have decimals and integers do not.
• CODE EXAMPLE. Floats and integers. TRY THIS YOURSELF.
• ANSWER: 0, because there is no remainder for any of them Modulus.
• “In computing, the modulo (sometimes called modulus) operation finds the remainder of division of one number by another.” http://en.wikipedia.org/wiki/Modulo_operation
• ANSWER: 5 (the remainder) But see - http://forums.udacity.com/questions/1001665/python-help-with-modulus Example: - 7 % 5 -&gt; 3 but 7 % 5 -&gt; 2
• (Exercise 4 LPTHW) Variables consist of two things, a name and a value.
• I think of variables as containers. They can hold all kinds of values. You can name them almost anything.
• The name is used in our code to represent the contents – but the contents (the value) can change.
• CODE EXAMPLE. Changing the value of some variables.
• Again: Variables consist of two things, a name and a value.
• Thevalue. can change, while the name stays the same.
• Thevalue. can change, while the name stays the same.
• Thevalue. can change, while the name stays the same.
• CODE EXAMPLE. Any questions? TRY THIS YOURSELF.
• Also called formatters (“short abbreviated variable names”) – Zed introduces these in exercise 5.
• CODE EXAMPLE. Each one has different abilities. Play with these yourself until you understand them.
• CODE EXAMPLE. Best example I have seen of how %r produces a different result from %s or %d or %f.
• http://docs.python.org/2/library/stdtypes.html#string-formattingIntegers are negative or positive numbers without decimals or fractions.
• Playing with code at the command line – and writing lines in your program for which you don’t know how they will come out – this is a big part of how we learn.
• CODE EXAMPLE. Playing with escape characters: \n and \t
• Same thing, just with color added. Note that each of these four variables contains a string. Therefore, the format character is %s for each one.
• Check out how the ”triple double-quotes” and the ”triple single-quotes” work. TRY THEM OUT.
• CODE EXAMPLE. Notice that the \n added an extra line for each line – the way it was typed, each line would have broken at the end anyway.
• LPTHW Exercise 10 – memorize\n and \\ and \’ -- it’s enough.
• CODE EXAMPLE. Demonstrating \n and \\ and \&quot;
• Zed’s exercises 11 and 12 introduce this.
• CODE EXAMPLE. LPTHW Exercises 11 and 12 – use of raw_input () to accept typing from the user while the program is running.
• ### 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)
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.