learning python - week 3

48
Learn Python the Hard Way Exercises 20 – 26 http://learnpythonthehardway.org /

Upload: mindy-mcadams

Post on 06-May-2015

2.861 views

Category:

Education


2 download

DESCRIPTION

Based on Zed Shaw's "Learn Python the Hard Way," this is a review of Exercises 20 - 26 in that text. For non-computer-science students and learners.

TRANSCRIPT

Page 1: Learning Python - Week 3

Learn Python the Hard Way

Exercises 20 – 26

http://learnpythonthehardway.org/

Page 2: Learning Python - Week 3

Review: Things we do with files

• Open: f = open(somefile)• Read: f.read()• Erase: f.truncate()• Write: f.write()• Close: f.close()• Rewind: f.seek(0)• Move the “playhead”: f.seek()• Read the current line: f.readline()

Page 3: Learning Python - Week 3

Note that f.seek(0) returns to the start of the file …

Page 4: Learning Python - Week 3

… but with another number, such as f.seek(52) … reading the file will begin at that position, not at the start.

Page 5: Learning Python - Week 3

Examples using f.readline()

Page 6: Learning Python - Week 3

What is the result?

a) case = 5case = case + 1

b) case = 5case += 1

c) case = 5case –= 1

Page 7: Learning Python - Week 3

What is the result?

a) case = 5case = case + 1

b) case = 5case += 1

c) case = 5case –= 1

Page 8: Learning Python - Week 3

Using pydoc

• Zed suggests you look at the built-in Python documentation from time to time.

• When he says “try pydoc file” — do this:– Make sure you are not in Python (look for the $)– Type:python –m pydoc file

– This will give you the help document about “file”

Page 9: Learning Python - Week 3

Python’s built-in documentation:Press the down arrow to scroll.Press Q to quit.

Page 10: Learning Python - Week 3

Returns

• When you want to run a function and use the result outside the function, you’ll put a return statement inside the function.

• When the function reaches the line with the return statement, the function will stop running (and return the specified value).

• When you call a function that includes a return statement, you must have a variable name to “catch” the returned value.

Exercise 21

Page 11: Learning Python - Week 3

See if you can figure this out.

Page 12: Learning Python - Week 3

Sometimes exercises for beginners are a bit weird.You wouldn’t write a program like this to do real work.But it’s a good example for trying to wrap your brain around the idea of returns.NOTICE: Are x and y used outside the function?

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

Exercise 21’s study drill is tough, especially if you are

easily confused by math.

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

Exercise 21

Page 15: Learning Python - Week 3

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

There’s no point in anyone explaining it. Just try your best to follow

Zed’s instructions. Like he says, it’s a puzzle.

Exercise 21

Page 16: Learning Python - Week 3
Page 17: Learning Python - Week 3

Exercise 22: Do it or don’t do it.

Exercise 22

Page 18: Learning Python - Week 3

Exercise 22: Do it or don’t do it.

Exercise 22

I did it. Just what Zed says. You should too. If you haven’t done it yet, then put it on your To Do list for this week.

Page 19: Learning Python - Week 3
Page 20: Learning Python - Week 3

Exercise 23: Reading code.(Other people’s code.)

Exercise 23

Page 21: Learning Python - Week 3
Page 22: Learning Python - Week 3
Page 24: Learning Python - Week 3

Practicing what you (should) know

• Review of how escape characters work:\n newline\t tab indent\\ one backslash\' one single quote

• How the “triple double-quotes” work: """

Exercise 24

Page 25: Learning Python - Week 3

Exercise 24

Page 26: Learning Python - Week 3

Exercise 24

This is close to Zed’s version. Do not be fooled by his re-use of variable names.

Page 27: Learning Python - Week 3

Exercise 24

Note how in this version, I changed the variable names. And it works the same way.

Page 28: Learning Python - Week 3

Exercise 24

This version is the most confusing, but it’s also the most concise.

Page 29: Learning Python - Week 3

A little break: TextWrangler settings

Setting preferences in TextWrangler:

1. Line numbers (turn them on)2. Soft wrap (lines)3. Appearance of soft-wrapped

lines4. Colors

Page 30: Learning Python - Week 3

TextWrangler – turn on the line numbers

Page 31: Learning Python - Week 3

TextWrangler – “soft wrap” lines

Note: You can also change the font size here. Make it larger if you are feeling eye strain.

Page 32: Learning Python - Week 3

TextWrangler – appearance of “soft wrap”

Page 33: Learning Python - Week 3

TextWrangler – change the colors

Page 34: Learning Python - Week 3

Ex. 25: You made a “module”!

• You wrote a bunch of functions in one .py file• But … the file does not call any of the

functions• Thus, no functions run when you run the file

Page 35: Learning Python - Week 3

Ex. 25: You made a “module”!

• You wrote a bunch of functions in one .py file• But … the file does not call any of the

functions• Thus, no functions run when you run the file• Remember from sys import argv ?• Now you are importing some things you wrote

yourself

Page 36: Learning Python - Week 3

This is what I get when I run: help(ex25)

Page 37: Learning Python - Week 3

That is because I wrote THIS in my ex25.py file!

Page 38: Learning Python - Week 3

This is slightly different from what Zed suggests, because I learn a lot from playing with the code.Look at the line that gave me an error.Why did I get that error?How did I fix it?

Exercise 25

Page 39: Learning Python - Week 3

What does this teach you about the way the function sort_words() really works?

Exercise 25

Page 40: Learning Python - Week 3

Do you understand it now?

Exercise 25

Page 41: Learning Python - Week 3

Ex. 25: What does each one do?

(something is a variable name)

something.split(' ')sorted(something)something.pop(0)something.pop(–1)

Page 42: Learning Python - Week 3

This is how I answer the questions on the previous slide. When Zed says “this is a list which you will learn about later,” he means the result at the arrow, above.

Exercise 25

Page 43: Learning Python - Week 3

Ex. 26: Fix someone else’s code

• You might not enjoy this exercise, but (like medicine) it is good for you

• Remember: Zed is showing you how to learn• This is why I chose Zed’s book

P.S. When I did this exercise, it took me 17 minutes altogether. But I spent a long, long time on exercises 24 and 25.

Page 44: Learning Python - Week 3

(1) This is how the program ran after I had fixed all the errors.

Exercise 26

Page 45: Learning Python - Week 3

(2) This is how the program ran after I had fixed all the errors.

Exercise 26

Page 46: Learning Python - Week 3

Heads up! Exercise 27 is important. Zed asks you to memorize and tells you how.Do what he says.

Page 47: Learning Python - Week 3

Some students have paid $29 to download Zed’s videos. You also get a PDF of the complete book, Learn Python the Hard Way. It’s a complete package, all videos and the PDF for one price.

[LINK]

Page 48: Learning Python - Week 3

Learn Python the Hard Way

Exercises 20 – 26

(we are getting smarter, little by little)