learning python - week 2

39
Learn Python the Hard Way Exercises 13 – 19 http://learnpythonthehardway.org /

Upload: mindy-mcadams

Post on 06-May-2015

3.279 views

Category:

Education


3 download

DESCRIPTION

Based on Zed Shaw's "Learn Python the Hard Way," this is a review of Exercises 13 - 19 in that text. For non-computer-science students and learners. This PPT will not make sense without Zed's lessons. The PPT is intended to supplement and help explain these seven lessons. The PPT was updated on Jan. 17, 2014.

TRANSCRIPT

Page 1: Learning Python - Week 2

Learn Python the Hard Way

Exercises 13 – 19

http://learnpythonthehardway.org/

Page 2: Learning Python - Week 2

from sys import argv

• You’ll see this kind of thing a lot in Python scripts:

from ----- import -----• The first thing (sys) is a module that already

exists in Python. It contains many things.• The second thing (argv) is a variable that is

defined in sys. By importing it, you can do stuff that would not work otherwise.

Page 3: Learning Python - Week 2

Python is flexible …

• Once you have argv in your program (because you started with from sys import argv), you can use it like this:

script, x, y, z = argv

• It’s entirely up to you how many things you put in that line before = argv

Page 4: Learning Python - Week 2

… but Python is fussy too

• If your program has four things in the line before = argv

script, x, y, z = argv

• … then you absolutely must invoke your program with three arguments, like this:

~$ python ex13.py Tom Dick Harry

Page 5: Learning Python - Week 2
Page 6: Learning Python - Week 2

from sys import argv

• Zed’s “Study Drills” are really key for learning how this works

• Think of why a program might need this instead of raw_input()

• Note: The “arguments” for argv are loaded into your program before the program is running

Page 7: Learning Python - Week 2

“Adventure”: An early computer game

Page 8: Learning Python - Week 2

> type something here

With Zed’s Exercise 14, again, his “Study Drills” are really important.

If you’re skipping them, you’re probably not getting it.

You’re going to start feeling very lost …

Page 9: Learning Python - Week 2

Opening files

f = open("myfileonmycomputer.txt")

f is a variable name (it could be x, or file, or txt)myfileonmycomputer.txt

is the name of a normal file on your computer

open() is a built-in function in Python.Note: This only stores the file as a value (it does NOT show you the contents of the file).

Exercise 15

Page 10: Learning Python - Week 2

Opening files (2)

f = open(filename)In Ex. 15, Zed has that (above).

Where does filename come from?Go back to the second line of his code:script, filename = argvfilename came from the argument you typed when you invoked your program:~$ python ex15.py ex15_sample.txt

Exercise 15

Page 11: Learning Python - Week 2

Reading files

print f.read()What does this do?

Make sure you understand this.

read() is a method in Python. It reads what you tell it to read, and then returns a string that contains what it read.

Exercise 15

Page 12: Learning Python - Week 2

You can play with this stuff on the command line

Page 13: Learning Python - Week 2

Why this is cool

You can send Python out to the Web and ask it to read files out there.Then you can search the contents of those files and do stuff with the contents.Python is powerful.Code is powerful!

Page 14: Learning Python - Week 2

Messing with the contents of files

f = open(filename, 'w')What does the ‘w’ do? Did you look it up?

f.truncate()What does this do? (Make sure you know!)

f.write(something)What does this do?

Exercise 16

Page 15: Learning Python - Week 2

Truncate a filePlay on the command line!

Page 16: Learning Python - Week 2

Write to a filePlay on the command line!

Page 17: Learning Python - Week 2

Top: The original file, test.txt. Bottom: The same file, after truncating and writing.

Page 18: Learning Python - Week 2

What will Python allow?

What happens if you enter:

f.write(a, b, c)

Note: You should try this kind of thing. Write a comment in your code about what you find out after you try it.

Page 19: Learning Python - Week 2

Modes for open()

f = open(filename, 'w')

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). 'r+' read 'w+' write and erase all contents'a+' append

If the mode is omitted, it defaults to 'r'.

http://docs.python.org/2/library/functions.html#open

Page 20: Learning Python - Week 2

Checking if a file exists

from sys import argvfrom os.path import exists

Review Exercise 13 • The first thing (os.path) is a module that is part

of Python. It contains many things.• The second thing (exists) is a function that is

defined in os.path. You must import it if you intend to use it.

Exercise 17

Page 21: Learning Python - Week 2

Using exists

print "Does the output file exist? %r" % exists(to_file)

If that confuses you, try just this:print exists(to_file)Then run the program again, and give it a filename that does not exist on your computer.

Exercise 17

Page 22: Learning Python - Week 2

You can even play with thison the command line

Page 23: Learning Python - Week 2

Copying file contents

• I think Zed’s filenames are confusing in Ex. 17.• If you change the variable names for the two

files to: oldfile and newfile, or to original and destination, maybe it will help you.

script, oldfile, newfile = argv

Exercise 17

Page 24: Learning Python - Week 2

Copying, Step 1

f = open(oldfile)indata = f.read()

is the same as

indata = open(oldfile).read()(Python allows us to chain instructions together in one line)

Exercise 17

Page 25: Learning Python - Week 2

Copying, Step 2

t = open(newfile, 'w')t.write(indata)

Confusing, yes? The real file represented by newfile will now be represented by the variable name t.The value of indata is put into t.Whatever was in oldfile is now in newfile.

Exercise 17

Page 26: Learning Python - Week 2

This code accomplishes the same thing as Zed’s Ex. 17.

Page 27: Learning Python - Week 2

This is a good one to play withon the command line

len()Exercise 17

Page 28: Learning Python - Week 2

Compare the real file size with the result from len()

Page 29: Learning Python - Week 2

Yes, Exercise 17 is hard.Even Zed says so! *

* In his “Common Student Questions” section

Page 30: Learning Python - Week 2

Functions!

• Functions are as essential to programming as variables

• Most programming languages use functions• Basically, a function has a name and a list of

instructions• When you call the function, those instructions

will run (they will be executed)

Exercise 18

Page 31: Learning Python - Week 2
Page 32: Learning Python - Week 2

More about functions!

• Important: A function does not do anything, ever, until it is called

• If you define a function, but you never call it, it will never run

• Most programs contain several (or many) different functions

Exercise 18

Page 33: Learning Python - Week 2

Accurate typing

• Here’s where your journalism editing skill gives you an edge!

• Follow the style carefully …def functionname(argument,

argument):(don’t forget the colon!)

• Indents: In Python, the indents are super- important! The convention is to use 4 spaces —and NOT a tab.

Exercise 18

Page 34: Learning Python - Week 2

Variables inside and outside

• Python doesn’t throw an error if you use the exact same variable name inside a function AND outside a function

• This can be misleading • Those would actually be two different variables• If you change the value of a variable inside a

function, the value of the other variable (outside) will not change!

Exercise 19

Page 35: Learning Python - Week 2
Page 36: Learning Python - Week 2

Variables inside and outside

Therefore, it is very smart to be careful about the names you give to your variables.

Don’t mix and match.

Use different variable names inside the functions.

Exercise 19

Page 37: Learning Python - Week 2

Start with the code you see below.Then, below it, write a function that takes mpg, price_of_gas, and distance as arguments.

The function will calculate the cost of gas for the trip.

Page 38: Learning Python - Week 2

You’ll learn much more about functions

in the upcoming exercises!

Page 39: Learning Python - Week 2

Learn Python the Hard Way

Exercises 13 – 19

(now we know something)