Learning Python - Week 1
-
Upload
mindy-mcadams -
Category
Education
-
view
2.731 -
download
0
description
Transcript of Learning Python - Week 1
Learn Python the Hard Way
Exercises 1 – 12
http://learnpythonthehardway.org/
Exercise 1: Quotation marks
Which of these will throw an error?
a) print "They wouldn't do that."b) print 'They wouldn't do that.'c) print 'They wouldn\'t do that.'d) print "They wouldn't do that.'
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
Which one will print blank lines?
print "Fourscore and seven years ago"##print "Our fathers brought forth"
print "Fourscore and seven years ago"printprintprint "Our fathers brought forth"
1)
2)
Exercise 3: Math
Which one of these would give a different answer than the others?
a) 4 + 6 / 3 + 5b) 4 + (6 / 3) + 5c) (4 + 6) / 3 + 5d) (4 + 6 / 3) + 5e) 4 + (6 / 3 + 5)
PEMDAS
• P ( )• E exponents, e.g. 10**2• M *• D /• A +• S –
But …
PEMDAS is not the whole story
• P ( )• E exponents, e.g. 10**2•MD* / %• AS + –
See http://en.wikipedia.org/wiki/Order_of_operations
Exercise 4: Math
What is the answer Python will give to all of these?
a) 5 > 10b) 10 < 2c) 1444 < 1443d) 1 > 2
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.)
Exercise 5: Integers and floats
What is the answer Python will give to this?
1 / 4
Exercise 5: Integers and floats
What is the answer Python will give to this?
1 / 4.0
Learn this!
>>> 1 / 40>>>
integer
>>> 1 / 4.00.25>>>
float
Exercise 6: Modulus
What is the answer Python will give to all of these?
a) 4 % 2b) 10 % 2c) 144 % 12d) 100 % 25
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!)
Modulo (also modulus)
What will this return? (That is, what answer will Python give?)
115 % 11
Variables
Variables
Name
apple
Value
Variables
Variables
apple
Variables
Name Value
apple
Variables
Name Value
apple
Variables
Name Value
apple 57
Variables
Name Value
apple
Any questions?
“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!
%r %s %d Not all the same.
Notice how %r in this case returns something very different from %s
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.)
The value of “play”
(an essential part of learning to code)
\n newline (line break)\t tab (indent)The backslash \ is “the escape character.”
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 "\n\n%s\n\t%s\n%s\n\t%s\n\n" % (a, b, c, d)
What would happen if the order were changed to: (d, c, b, a)
Running that program …
Escapes
Zed says memorize all of these. I don’t think that’s necessary.But do memorize this:
The backslash \ is the escape character.
Escapes!
raw_input( )
Zed’s exercises 11 and 12 introduce this.
raw_input( )
Learn Python the Hard Way
Exercises 1 – 12
(we’re just getting started)