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().
1. Learn Python the Hard Way
Exercises 1 – 12
http://learnpythonthehardway.org/
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. 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. 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. 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)
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. 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. 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. Exercise 5: Integers and floats
What is the answer Python will give to this?
1/4
11. Exercise 5: Integers and floats
What is the answer Python will give to this?
1 / 4.0
14. 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
15. 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!)
27. “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!
29. Notice how %r in this case returns
something very different from %s
30. 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.)
31. The value of “play”
(an essential part of learning to code)
32. n newline (line break)
t tab (indent)
The backslash is
“the escape character.”
33. 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)
40. Learn Python the Hard Way
Exercises 1 – 12
(we’re just getting started)
Notes de l'éditeur
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)
ANSWER: 0.25 Floating point.
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 -> 3 but 7 % 5 -> 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 \"
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.