learning python - week 1

40
Learn Python the Hard Way Exercises 1 – 12 http://learnpythonthehardway.org /

Upload: mindy-mcadams

Post on 06-May-2015

2.731 views

Category:

Education


0 download

DESCRIPTION

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().

TRANSCRIPT

Page 1: Learning Python - Week 1

Learn Python the Hard Way

Exercises 1 – 12

http://learnpythonthehardway.org/

Page 2: Learning Python - Week 1

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.'

Page 3: Learning Python - Week 1

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

Page 4: Learning Python - Week 1

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)

Page 5: Learning Python - Week 1

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)

Page 6: Learning Python - Week 1

PEMDAS

• P ( )• E exponents, e.g. 10**2• M *• D /• A +• S –

But …

Page 7: Learning Python - Week 1

PEMDAS is not the whole story

• P ( )• E exponents, e.g. 10**2•MD* / %• AS + –

See http://en.wikipedia.org/wiki/Order_of_operations

Page 8: Learning Python - Week 1

Exercise 4: Math

What is the answer Python will give to all of these?

a) 5 > 10b) 10 < 2c) 1444 < 1443d) 1 > 2

Page 9: Learning Python - Week 1

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.)

Page 10: Learning Python - Week 1

Exercise 5: Integers and floats

What is the answer Python will give to this?

1 / 4

Page 11: Learning Python - Week 1

Exercise 5: Integers and floats

What is the answer Python will give to this?

1 / 4.0

Page 12: Learning Python - Week 1

Learn this!

>>> 1 / 40>>>

integer

>>> 1 / 4.00.25>>>

float

Page 13: Learning Python - Week 1
Page 14: Learning Python - Week 1

Exercise 6: Modulus

What is the answer Python will give to all of these?

a) 4 % 2b) 10 % 2c) 144 % 12d) 100 % 25

Page 15: Learning Python - Week 1

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!)

Page 16: Learning Python - Week 1

Modulo (also modulus)

What will this return? (That is, what answer will Python give?)

115 % 11

Page 17: Learning Python - Week 1

Variables

Page 18: Learning Python - Week 1

Variables

Name

apple

Value

Page 19: Learning Python - Week 1

Variables

Page 20: Learning Python - Week 1

Variables

apple

Page 21: Learning Python - Week 1
Page 22: Learning Python - Week 1

Variables

Name Value

apple

Page 23: Learning Python - Week 1

Variables

Name Value

apple

Page 24: Learning Python - Week 1

Variables

Name Value

apple 57

Page 25: Learning Python - Week 1

Variables

Name Value

apple

Page 26: Learning Python - Week 1

Any questions?

Page 27: Learning Python - Week 1

“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!

Page 28: Learning Python - Week 1

%r %s %d Not all the same.

Page 29: Learning Python - Week 1

Notice how %r in this case returns something very different from %s

Page 30: Learning Python - Week 1

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.)

Page 31: Learning Python - Week 1

The value of “play”

(an essential part of learning to code)

Page 32: Learning Python - Week 1

\n newline (line break)\t tab (indent)The backslash \ is “the escape character.”

Page 33: Learning Python - Week 1

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)

Page 34: Learning Python - Week 1
Page 35: Learning Python - Week 1

Running that program …

Page 36: Learning Python - Week 1

Escapes

Zed says memorize all of these. I don’t think that’s necessary.But do memorize this:

The backslash \ is the escape character.

Page 37: Learning Python - Week 1

Escapes!

Page 38: Learning Python - Week 1

raw_input( )

Zed’s exercises 11 and 12 introduce this.

Page 39: Learning Python - Week 1

raw_input( )

Page 40: Learning Python - Week 1

Learn Python the Hard Way

Exercises 1 – 12

(we’re just getting started)