learn 90% of python in 90 minutes

119
90% of Python in 90 Minutes 90% of Python in 90 Minutes @__mharrison__ @__mharrison__ Copyright 2013 Copyright 2013

Upload: matt-harrison

Post on 14-May-2015

33.028 views

Category:

Technology


42 download

DESCRIPTION

The basics of Python are rather straightforward. In a few minutes you can learn most of the syntax. There are some gotchas along the way that might appear tricky. This talk is meant to bring programmers up to speed with Python. They should be able to read and write Python.

TRANSCRIPT

Page 1: Learn 90% of Python in 90 Minutes

90% of Python in 90 Minutes90% of Python in 90 Minutes@__mharrison__@__mharrison__

Copyright 2013Copyright 2013

Page 2: Learn 90% of Python in 90 Minutes

About MeAbout Me

● 12+ years Python12+ years Python● Worked in Data Analysis, HA, Search, Worked in Data Analysis, HA, Search,

Open Source, BI, and StorageOpen Source, BI, and Storage● Author of multiple Python BooksAuthor of multiple Python Books

Page 3: Learn 90% of Python in 90 Minutes

GoalGoal

● Read PythonRead Python● Write PythonWrite Python

Page 4: Learn 90% of Python in 90 Minutes

BookBook

Page 5: Learn 90% of Python in 90 Minutes

BookBook

Treading on Python Volume 1Treading on Python Volume 1 covers this covers this talk in much more detail.talk in much more detail.

Page 6: Learn 90% of Python in 90 Minutes

BeginBegin

Page 7: Learn 90% of Python in 90 Minutes

DisclaimerDisclaimer

● Assume some programming experienceAssume some programming experience● Not covering all api's, just syntaxNot covering all api's, just syntax

Page 8: Learn 90% of Python in 90 Minutes

WarningWarning

● Starting from ground zeroStarting from ground zero● Hands-on at endHands-on at end

Page 9: Learn 90% of Python in 90 Minutes

Three Python'isms to RememberThree Python'isms to Remember

● dirdir● helphelp● colon/indent shufflecolon/indent shuffle

Page 10: Learn 90% of Python in 90 Minutes

Why Python?Why Python?

Python is a powerful, multi-paradigm, Python is a powerful, multi-paradigm, interpreted language popular with interpreted language popular with start-ups and large Co’sstart-ups and large Co’s

Page 11: Learn 90% of Python in 90 Minutes

Python 2 or 3?Python 2 or 3?

For beginners there is no real difference For beginners there is no real difference between Python 2 & 3. The basics are the between Python 2 & 3. The basics are the same (except for same (except for printprint))

Page 12: Learn 90% of Python in 90 Minutes

Hello WorldHello World

Page 13: Learn 90% of Python in 90 Minutes

hello worldhello world

printprint "hello world""hello world"

Page 14: Learn 90% of Python in 90 Minutes

from interpreterfrom interpreter

$ python$ python>>> >>> printprint "hello world""hello world"hello worldhello world

Page 15: Learn 90% of Python in 90 Minutes

REPLREPL

Read, Eval, Print, LoopRead, Eval, Print, Loop

Page 16: Learn 90% of Python in 90 Minutes

REPLREPL

$ python$ python>>> >>> 22 ++ 22 # read, eval# read, eval4 # print4 # print>>> >>> # repeat (loop)# repeat (loop)

Page 17: Learn 90% of Python in 90 Minutes

REPL (2)REPL (2)

Many developers keep a REPL handy Many developers keep a REPL handy during programmingduring programming

Page 18: Learn 90% of Python in 90 Minutes

From scriptFrom script

Make file Make file hello.pyhello.py with with

print "hello world"print "hello world"

Run with:Run with:

python hello.pypython hello.py

Page 19: Learn 90% of Python in 90 Minutes

(unix) script(unix) script

Make file Make file hellohello with with

#!/usr/bin/env python#!/usr/bin/env pythonprint "hello world"print "hello world"

Run with:Run with:

chmod +x hellochmod +x hello./hello./hello

Page 20: Learn 90% of Python in 90 Minutes

Python 3 hello worldPython 3 hello world

printprint is no longer a statement, but a is no longer a statement, but a functionfunction

print("hello world")print("hello world")

Page 21: Learn 90% of Python in 90 Minutes

ObjectsObjects

Page 22: Learn 90% of Python in 90 Minutes

ObjectsObjects

Everything in Everything in PythonPython is an object that has: is an object that has:● an an identityidentity ( (idid))● a a valuevalue (mutable or immutable) (mutable or immutable)

Page 23: Learn 90% of Python in 90 Minutes

idid

>>> >>> a a == 44>>> >>> idid(a)(a)64068966406896

Page 24: Learn 90% of Python in 90 Minutes

ValueValue

●Mutable:Mutable:When you alter the item, the When you alter the item, the idid is still the same. Dictionary, List is still the same. Dictionary, List

● Immutable:Immutable:String, Integer, TupleString, Integer, Tuple

Page 25: Learn 90% of Python in 90 Minutes

MutableMutable

>>> >>> b b == [] []>>> >>> idid(b)(b)140675605442000140675605442000>>> >>> bb..append(append(33))>>> >>> bb[3][3]>>> >>> idid(b)(b)140675605442000 # SAME!140675605442000 # SAME!

Page 26: Learn 90% of Python in 90 Minutes

ImmutableImmutable

>>> >>> a a == 44>>> >>> idid(a)(a)64068966406896>>> >>> a a == a a ++ 11>>> >>> idid(a)(a)6406872 # DIFFERENT!6406872 # DIFFERENT!

Page 27: Learn 90% of Python in 90 Minutes

VariablesVariables

a a == 44 # Integer# Integerb b == 5.65.6 # Float# Floatc c == "hello""hello" # String# Stringa a == "4""4" # rebound to String# rebound to String

Page 28: Learn 90% of Python in 90 Minutes

NamingNaming

● lowercaselowercase● underscore_between_wordsunderscore_between_words● don't start with numbersdon't start with numbers

See PEP 8See PEP 8

Page 29: Learn 90% of Python in 90 Minutes

PEPPEP

Python Enhancement Proposal (similar to Python Enhancement Proposal (similar to JSR in Java)JSR in Java)

Page 30: Learn 90% of Python in 90 Minutes

MathMath

Page 31: Learn 90% of Python in 90 Minutes

MathMath

++, , --, , **, , //, , **** (power), (power), %% (modulo) (modulo)

Page 32: Learn 90% of Python in 90 Minutes

Careful with integer divisionCareful with integer division

>>> >>> 3/43/400>>> >>> 3/4.3/4.0.750.75

(In Python 3 (In Python 3 //// is integer division is integer division operator)operator)

Page 33: Learn 90% of Python in 90 Minutes

What happens when you What happens when you raise 10 to the 100th?raise 10 to the 100th?

Page 34: Learn 90% of Python in 90 Minutes

LongLong

>>> >>> 10**10010**10010000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L00000L

Page 35: Learn 90% of Python in 90 Minutes

LongLong (2) (2)

>>> >>> importimport syssys>>> >>> syssys..maxintmaxint92233720368547758079223372036854775807>>> >>> syssys..maxint maxint ++ 119223372036854775808L9223372036854775808L

Page 36: Learn 90% of Python in 90 Minutes

StringsStrings

Page 37: Learn 90% of Python in 90 Minutes

StringsStrings

name name == 'matt''matt'with_quote with_quote == "I ain't gonna""I ain't gonna"longer longer == """This string has"""This string hasmultiple linesmultiple linesin it"""in it"""

Page 38: Learn 90% of Python in 90 Minutes

How do I print?How do I print?

He said, “I’m sorry”He said, “I’m sorry”

Page 39: Learn 90% of Python in 90 Minutes

String escapingString escaping

Escape with Escape with \\>>> >>> printprint 'He said, "I'He said, "I\'\'m sorry"'m sorry"'He said, "I'm sorry"He said, "I'm sorry">>> >>> printprint '''He said, "I'm sorry"''''''He said, "I'm sorry"'''He said, "I'm sorry"He said, "I'm sorry">>> >>> printprint """He said, "I'm sorry"""He said, "I'm sorry\"\"""""""He said, "I'm sorry"He said, "I'm sorry"

Page 40: Learn 90% of Python in 90 Minutes

String escaping (2)String escaping (2)Escape Sequence Output

\\ Backslash

\' Single quote

\" Double quote

\b ASCII Backspace

\n Newline

\t Tab

\u12af Unicode 16 bit

\U12af89bc Unicode 32 bit

\o84 Octal character

\xFF Hex character

Page 41: Learn 90% of Python in 90 Minutes

String formattingString formatting

c-likec-like>>> >>> ""%s%s %s%s"" %%(('hello''hello', , 'world''world'))'hello world''hello world'

PEP 3101 stylePEP 3101 style>>> >>> "{0} {1}""{0} {1}"..format(format('hello''hello', , 'world''world'))'hello world''hello world'

Page 42: Learn 90% of Python in 90 Minutes

Methods & Methods & dirdir

Page 43: Learn 90% of Python in 90 Minutes

dirdir

Lists attributes and methods:Lists attributes and methods:>>> >>> dirdir(("a string""a string"))['__add__', '__class__', ... 'startswith', 'strip', ['__add__', '__class__', ... 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']'swapcase', 'title', 'translate', 'upper', 'zfill']

Page 44: Learn 90% of Python in 90 Minutes

Whats with all the Whats with all the '__blah__''__blah__'??

Page 45: Learn 90% of Python in 90 Minutes

dunderdunder methods methods

dunderdunder (double under) or "special/magic" (double under) or "special/magic" methods determine what will happen methods determine what will happen when when ++ ( (__add____add__) or ) or // ( (__div____div__) is ) is called.called.

Page 46: Learn 90% of Python in 90 Minutes

helphelp

>>> >>> help(help("a string""a string"..startswith)startswith)

Help on built-in function startswith:Help on built-in function startswith:

startswith(...)startswith(...) S.startswith(prefix[, start[, end]]) -> bool S.startswith(prefix[, start[, end]]) -> bool

Return True if S starts with the specified prefix, False Return True if S starts with the specified prefix, False otherwise.otherwise. With optional start, test S beginning at that position. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. With optional end, stop comparing S at that position. prefix can also be a tuple of strings to try. prefix can also be a tuple of strings to try.

Page 47: Learn 90% of Python in 90 Minutes

String methodsString methods

● s.endswith(sub)s.endswith(sub)

Returns True if endswith Returns True if endswith subsub● s.find(sub)s.find(sub)

Returns index of Returns index of subsub or or -1-1● s.format(*args)s.format(*args)

Places args in stringPlaces args in string

Page 48: Learn 90% of Python in 90 Minutes

String methods (2)String methods (2)

● s.index(sub)s.index(sub)

Returns index of Returns index of subsub or exception or exception● s.join(list)s.join(list)

Returns Returns listlist items separated by string items separated by string● s.strip()s.strip()

Removes whitespace from start/endRemoves whitespace from start/end

Page 49: Learn 90% of Python in 90 Minutes

CommentsComments

Page 50: Learn 90% of Python in 90 Minutes

commentscomments

Comments follow a Comments follow a ##

Page 51: Learn 90% of Python in 90 Minutes

commentscomments

No multi-line commentsNo multi-line comments

Page 52: Learn 90% of Python in 90 Minutes

More TypesMore Types

Page 53: Learn 90% of Python in 90 Minutes

NoneNone

Pythonic way of saying Pythonic way of saying NULLNULL. Evaluates . Evaluates to False.to False.

c c == NoneNone

Page 54: Learn 90% of Python in 90 Minutes

booleansbooleans

a a == TrueTrueb b == FalseFalse

Page 55: Learn 90% of Python in 90 Minutes

sequencessequences● listslists● tuplestuples● setssets

Page 56: Learn 90% of Python in 90 Minutes

listslists

Hold sequences.Hold sequences.

How would we find out the attributes & How would we find out the attributes & methods of a list?methods of a list?

Page 57: Learn 90% of Python in 90 Minutes

listslists>>> >>> dirdir([])([])['__add__', '__class__', '__contains__',... ['__add__', '__class__', '__contains__',... '__iter__',... '__len__',... , 'append', 'count', '__iter__',... '__len__',... , 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']'reverse', 'sort']

Page 58: Learn 90% of Python in 90 Minutes

listslists

>>> >>> a a == [] []>>> >>> aa..append(append(44))>>> >>> aa..append(append('hello''hello'))>>> >>> aa..append(append(11))>>> >>> aa..sort() sort() # in place# in place>>> >>> printprint a a[1, 4, 'hello'][1, 4, 'hello']

Page 59: Learn 90% of Python in 90 Minutes

listslists

How would we find out documentation How would we find out documentation for a method?for a method?

Page 60: Learn 90% of Python in 90 Minutes

listslists

helphelp function: function:>>> >>> help([]help([]..append)append)Help on built-in function append:Help on built-in function append:

append(...)append(...) L.append(object) -- append object to end L.append(object) -- append object to end

Page 61: Learn 90% of Python in 90 Minutes

List methodsList methods

● l.append(x)l.append(x)

Insert Insert xx at end of list at end of list● l.extend(l2)l.extend(l2)

Add Add l2l2 items to list items to list● l.sort()l.sort()

In place sortIn place sort

Page 62: Learn 90% of Python in 90 Minutes

List methods (2)List methods (2)

● l.reverse()l.reverse()

Reverse list in placeReverse list in place● l.remove(item)l.remove(item)

Remove first Remove first itemitem found found● l.pop()l.pop()

Remove/return item at end of listRemove/return item at end of list

Page 63: Learn 90% of Python in 90 Minutes

DictionariesDictionaries

Page 64: Learn 90% of Python in 90 Minutes

dictionariesdictionaries

Also called Also called hashmaphashmap or or associative arrayassociative array elsewhereelsewhere

>>> >>> age age == {} {}>>> >>> age[age['george''george'] ] == 1010>>> >>> age[age['fred''fred'] ] == 1212>>> >>> age[age['henry''henry'] ] == 1010>>> >>> printprint age[ age['george''george']]1010

Page 65: Learn 90% of Python in 90 Minutes

dictionariesdictionaries (2) (2)

Find out if Find out if 'matt''matt' in in ageage

>>> >>> 'matt''matt' inin age ageFalseFalse

Page 66: Learn 90% of Python in 90 Minutes

inin statement statement

Uses Uses __contains____contains__ dunder method to dunder method to determine membership. (Or determine membership. (Or __iter____iter__ as as fallback)fallback)

Page 67: Learn 90% of Python in 90 Minutes

.get.get>>> >>> printprint age[ age['charles''charles']]Traceback (most recent call last):Traceback (most recent call last): File File "<stdin>""<stdin>", line , line 11, in <module>, in <module>KeyErrorKeyError: 'charles': 'charles'>>> >>> printprint age age..get(get('charles''charles', , 'Not found''Not found'))Not foundNot found

Page 68: Learn 90% of Python in 90 Minutes

deleting keysdeleting keys

Removing Removing 'charles''charles' from from ageage

>>>>>> deldel age[ age['charles''charles']]

Page 69: Learn 90% of Python in 90 Minutes

deleting keysdeleting keys

deldel not in not in dirdir. . .pop.pop is an alternative is an alternative

Page 70: Learn 90% of Python in 90 Minutes

FunctionsFunctions

Page 71: Learn 90% of Python in 90 Minutes

functionsfunctions

defdef add_2add_2(num):(num): """ return 2""" return 2 more than num more than num """ """ returnreturn num num ++ 22

five five == add_2( add_2(33))

Page 72: Learn 90% of Python in 90 Minutes

functions (2)functions (2)● defdef

● function namefunction name

● (parameters)(parameters)

● :: + indent + indent

● optional documentationoptional documentation

● bodybody

● returnreturn

Page 73: Learn 90% of Python in 90 Minutes

whitespacewhitespace

Instead of Instead of {{ use a use a :: and indent and indent consistently (4 spaces)consistently (4 spaces)

Page 74: Learn 90% of Python in 90 Minutes

whitespace (2)whitespace (2)

invoke invoke python -ttpython -tt to error out during to error out during inconsistent tab/space usage in a fileinconsistent tab/space usage in a file

Page 75: Learn 90% of Python in 90 Minutes

default (named) parametersdefault (named) parameters

defdef add_nadd_n(num, n(num, n=3=3):): """default to"""default to adding 3""" adding 3""" returnreturn num num ++ n n

five five == add_n( add_n(22))ten ten == add_n( add_n(1515, , -5-5))

Page 76: Learn 90% of Python in 90 Minutes

__doc____doc__

Functions have Functions have docstringsdocstrings. Accessible . Accessible via via .__doc__.__doc__ or or helphelp

Page 77: Learn 90% of Python in 90 Minutes

__doc____doc__>>> >>> defdef echoecho(txt):(txt):... ... "echo back txt""echo back txt"... ... returnreturn txt txt>>> >>> help(echo)help(echo)Help on function echo in module __main__:Help on function echo in module __main__:<BLANKLINE><BLANKLINE>echo(txt)echo(txt) echo back txt echo back txt<BLANKLINE><BLANKLINE>

Page 78: Learn 90% of Python in 90 Minutes

namingnaming

● lowercaselowercase● underscore_between_wordsunderscore_between_words● don't start with numbersdon't start with numbers● verbverb

See PEP 8See PEP 8

Page 79: Learn 90% of Python in 90 Minutes

ConditionalsConditionals

Page 80: Learn 90% of Python in 90 Minutes

conditionalsconditionals

ifif grade grade >> 9090:: printprint "A""A"elifelif grade grade >> 8080:: printprint "B""B"elifelif grade grade >> 7070:: printprint "C""C"elseelse:: printprint "D""D"

Page 81: Learn 90% of Python in 90 Minutes

Remember the Remember the colon/whitespace!colon/whitespace!

Page 82: Learn 90% of Python in 90 Minutes

BooleansBooleans

a a == TrueTrueb b == FalseFalse

Page 83: Learn 90% of Python in 90 Minutes

Comparison OperatorsComparison Operators

Supports (Supports (>>, , >=>=, , <<, , <=<=, , ====, , !=!=))

>>> >>> 55 >> 99FalseFalse>>> >>> 'matt''matt' !=!= 'fred''fred'TrueTrue>>> >>> isinstanceisinstance(('matt''matt', , basestringbasestring))TrueTrue

Page 84: Learn 90% of Python in 90 Minutes

Boolean OperatorsBoolean Operators

andand, , oror, , notnot (for logical), (for logical), &&, , ||, and , and ^̂ (for (for bitwise)bitwise)

>>> >>> x x == 55>>> >>> x x << -4-4 oror x x >> 44TrueTrue

Page 85: Learn 90% of Python in 90 Minutes

Boolean noteBoolean note

Parens are only required for precedenceParens are only required for precedence

ifif (x (x >> 1010):): printprint "Big""Big"

same assame as

ifif x x >> 1010:: printprint "Big""Big"

Page 86: Learn 90% of Python in 90 Minutes

Chained comparisonsChained comparisons

ifif 33 << x x << 55:: printprint "Four!""Four!"

Same asSame as

ifif x x >> 33 andand x x << 55:: printprint "Four!""Four!"

Page 87: Learn 90% of Python in 90 Minutes

IterationIteration

Page 88: Learn 90% of Python in 90 Minutes

iterationiteration

forfor number number inin [ [11,,22,,33,,44,,55,,66]:]: printprint number number

forfor number number inin rangerange((11, , 77):): printprint number number

Page 89: Learn 90% of Python in 90 Minutes

rangerange note note

Python tends to follow Python tends to follow half-open intervalhalf-open interval (([start,end)[start,end)) with ) with rangerange and slices. and slices.● end - start = lengthend - start = length● easy to concat ranges w/o overlapeasy to concat ranges w/o overlap

Page 90: Learn 90% of Python in 90 Minutes

iteration (2)iteration (2)

Java/C-esque style of object in array Java/C-esque style of object in array access (BAD):access (BAD):animals animals == [ ["cat""cat", , "dog""dog", , "bird""bird"]]forfor index index inin rangerange((lenlen(animals)):(animals)): printprint index, animals[index] index, animals[index]

Page 91: Learn 90% of Python in 90 Minutes

iteration (3)iteration (3)

If you need indices, use If you need indices, use enumerateenumerateanimals animals == [ ["cat""cat", , "dog""dog", , "bird""bird"]]forfor index, value index, value inin enumerateenumerate(animals):(animals): printprint index, value index, value

Page 92: Learn 90% of Python in 90 Minutes

iteration (4)iteration (4)

Can Can breakbreak out of nearest loop out of nearest loopforfor item item inin sequence: sequence: # process until first negative# process until first negative ifif item item << 00:: breakbreak # process item# process item

Page 93: Learn 90% of Python in 90 Minutes

iteration (5)iteration (5)

Can Can continuecontinue to skip over items to skip over items

forfor item item inin sequence: sequence: ifif item item << 00:: continuecontinue # process all positive items# process all positive items

Page 94: Learn 90% of Python in 90 Minutes

iteration (6)iteration (6)

Can loop over lists, strings, iterators, Can loop over lists, strings, iterators, dictionaries... sequence like things:dictionaries... sequence like things:my_dict my_dict == { { "name""name": : "matt""matt", , "cash""cash": : 5.455.45}}forfor key key inin my_dict my_dict..keys():keys(): # process key# process key

forfor value value inin my_dict my_dict..values():values(): # process value# process value

forfor key, value key, value inin my_dict my_dict..items():items(): # process items# process items

Page 95: Learn 90% of Python in 90 Minutes

passpass

passpass is a null operation is a null operation

forfor i i inin rangerange((1010):): # do nothing 10 times# do nothing 10 times passpass

Page 96: Learn 90% of Python in 90 Minutes

HintHint

Don't modify Don't modify listlist or or dictionarydictionary contents contents while looping over themwhile looping over them

Page 97: Learn 90% of Python in 90 Minutes

SlicingSlicing

Page 98: Learn 90% of Python in 90 Minutes

SlicingSlicing

Sequences (lists, tuples, strings, etc) can Sequences (lists, tuples, strings, etc) can be be slicedsliced to pull out a single item to pull out a single item

my_pets my_pets == [ ["dog""dog", , "cat""cat", , "bird""bird"]]favorite favorite == my_pets[ my_pets[00]]bird bird == my_pets[ my_pets[-1-1]]

Page 99: Learn 90% of Python in 90 Minutes

Negative IndexingNegative Indexing

Proper way to think of [negative Proper way to think of [negative indexing] is to reinterpret indexing] is to reinterpret a[-X]a[-X] as as a[len(a)-X]a[len(a)-X]

@gvanrossum@gvanrossum

Page 100: Learn 90% of Python in 90 Minutes

Slicing (2)Slicing (2)

Slices can take an end index, to pull out a Slices can take an end index, to pull out a list of itemslist of items

my_pets my_pets == [ ["dog""dog", , "cat""cat", , "bird""bird"] ] # a list# a listcat_and_dog cat_and_dog == my_pets[ my_pets[00::22]]cat_and_dog2 cat_and_dog2 == my_pets[: my_pets[:22]]cat_and_bird cat_and_bird == my_pets[ my_pets[11::33]]cat_and_bird2 cat_and_bird2 == my_pets[ my_pets[11:]:]

Page 101: Learn 90% of Python in 90 Minutes

Slicing (3)Slicing (3)

Slices can take a strideSlices can take a stride

my_pets my_pets == [ ["dog""dog", , "cat""cat", , "bird""bird"] ] # a list# a listdog_and_bird dog_and_bird == [ [00::33::22]]zero_three_etc zero_three_etc == rangerange((00,,1010))[::[::33]]

Page 102: Learn 90% of Python in 90 Minutes

Slicing (4)Slicing (4)

Just to beat it inJust to beat it in

veg veg == "tomatoe""tomatoe"correct correct == veg[: veg[:-1-1]]tmte tmte == veg[:: veg[::22]]eotamot eotamot == veg[:: veg[::-1-1]]

Page 103: Learn 90% of Python in 90 Minutes

File IOFile IO

Page 104: Learn 90% of Python in 90 Minutes

File InputFile Input

Open a file to read from it (old style):Open a file to read from it (old style):

fin fin == openopen(("foo.txt""foo.txt"))forfor line line inin fin: fin: # manipulate line# manipulate line

finfin..close()close()

Page 105: Learn 90% of Python in 90 Minutes

File OutputFile Output

Open a file using Open a file using 'w''w' to to writewrite to a file: to a file:

fout fout == openopen(("bar.txt""bar.txt", , "w""w"))foutfout..write(write("hello world""hello world"))foutfout..close()close()

Page 106: Learn 90% of Python in 90 Minutes

Always remember to Always remember to close your files!close your files!

Page 107: Learn 90% of Python in 90 Minutes

closing with closing with withwith

implicit implicit closeclose (new 2.5+ style) (new 2.5+ style)

withwith openopen(('bar.txt''bar.txt') ) asas fin: fin: forfor line line inin fin: fin: # process line# process line

Page 108: Learn 90% of Python in 90 Minutes

ClassesClasses

Page 109: Learn 90% of Python in 90 Minutes

ClassesClassesclassclass AnimalAnimal((objectobject):): defdef __init____init__((selfself, name):, name): selfself..name name == name name

defdef talktalk((selfself):): printprint "Generic Animal Sound""Generic Animal Sound"

animal animal == Animal( Animal("thing""thing"))animalanimal..talk()talk()

Page 110: Learn 90% of Python in 90 Minutes

Classes (2)Classes (2)

notes:notes:● objectobject (base class) (fixed in 3.X) (base class) (fixed in 3.X)● dunderdunder init (constructor) init (constructor)● all methods take all methods take selfself as first parameter as first parameter

Page 111: Learn 90% of Python in 90 Minutes

Classes(2)Classes(2)

SubclassingSubclassingclassclass CatCat(Animal):(Animal): defdef talktalk((selfself):): printprint ''%s%s says, "Meow!"' says, "Meow!"' %% ( (selfself..name)name)

cat cat == Cat( Cat("Groucho""Groucho"))catcat..talk() talk() # invoke method# invoke method

Page 112: Learn 90% of Python in 90 Minutes

Classes(3)Classes(3)

classclass CheetahCheetah(Cat):(Cat): """classes can have"""classes can have docstrings""" docstrings"""

defdef talktalk((selfself):): printprint "Growl""Growl"

Page 113: Learn 90% of Python in 90 Minutes

namingnaming

● CamelCaseCamelCase● don't start with numbersdon't start with numbers● NounsNouns

Page 114: Learn 90% of Python in 90 Minutes

DebuggingDebugging

Page 115: Learn 90% of Python in 90 Minutes

Poor mansPoor mans

printprint works a lot of the time works a lot of the time

Page 116: Learn 90% of Python in 90 Minutes

RememberRemember

Clean up Clean up printprint statements. If you really statements. If you really need them, use need them, use logginglogging or write to or write to sys.stdoutsys.stdout

Page 117: Learn 90% of Python in 90 Minutes

pdbpdb

importimport pdbpdb; pdb; pdb..set_trace()set_trace()

Page 118: Learn 90% of Python in 90 Minutes

pdbpdb commands commands

● hh - help - help● ss - step into - step into● nn - next - next● cc - continue - continue● ww - where am I (in stack)? - where am I (in stack)?● ll - list code around me - list code around me

Page 119: Learn 90% of Python in 90 Minutes

That's allThat's all

Questions? Tweet meQuestions? Tweet me

For more details see For more details see Treading on Python Volume 1Treading on Python Volume 1

@__mharrison__@__mharrison__http://hairysun.comhttp://hairysun.com