90% of Python in 90 Minutes90% of Python in 90 Minutes
@__mharrison__@__mharrison__
Copyright 2013Copyright 2013
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
GoalGoal
●
Read PythonRead Python
●
Write PythonWrite Python
BookBook
BookBook
Treading on Python Volume 1Treading on Python Volume 1 covers thiscovers this
talk in much more detail.talk in much more detail.
BeginBegin
DisclaimerDisclaimer
●
Assume some programming experienceAssume some programming experience
●
Not covering all api's, just syntaxNot covering all api's, just syntax
WarningWarning
●
Starting from ground zeroStarting from ground zero
●
Hands-on at endHands-on at end
Three Python'isms to RememberThree Python'isms to Remember
●
dirdir
●
helphelp
●
colon/indent shufflecolon/indent shuffle
Why Python?Why Python?
Python is a powerful, multi-paradigm,Python is a powerful, multi-paradigm,
interpreted language popular withinterpreted language popular with
start-ups and large Co’sstart-ups and large Co’s
Python 2 or 3?Python 2 or 3?
For beginners there is no real differenceFor beginners there is no real difference
between Python 2 & 3. The basics are thebetween Python 2 & 3. The basics are the
same (except forsame (except for printprint))
Hello WorldHello World
hello worldhello world
printprint "hello world""hello world"
from interpreterfrom interpreter
$ python$ python
>>>>>> printprint "hello world""hello world"
hello worldhello world
REPLREPL
Read, Eval, Print, LoopRead, Eval, Print, Loop
REPLREPL
$ python$ python
>>>>>> 22 ++ 22 # read, eval# read, eval
4 # print4 # print
>>>>>> # repeat (loop)# repeat (loop)
REPL (2)REPL (2)
Many developers keep a REPL handyMany developers keep a REPL handy
during programmingduring programming
From scriptFrom script
Make fileMake file hello.pyhello.py withwith
print "hello world"print "hello world"
Run with:Run with:
python hello.pypython hello.py
(unix) script(unix) script
Make fileMake file hellohello withwith
#!/usr/bin/env python#!/usr/bin/env python
print "hello world"print "hello world"
Run with:Run with:
chmod +x hellochmod +x hello
./hello./hello
Python 3 hello worldPython 3 hello world
printprint is no longer a statement, but ais no longer a statement, but a
functionfunction
print("hello world")print("hello world")
ObjectsObjects
ObjectsObjects
Everything inEverything in PythonPython is an object that has:is an object that has:
●
anan identityidentity ((idid))
●
aa valuevalue (mutable or immutable)(mutable or immutable)
idid
>>>>>> aa == 44
>>>>>> idid(a)(a)
64068966406896
ValueValue
●
Mutable:Mutable:When you alter the item, theWhen you alter the item, the
idid is still the same. Dictionary, Listis still the same. Dictionary, List
●
Immutable:Immutable:String, Integer, TupleString, Integer, Tuple
MutableMutable
>>>>>> bb == [][]
>>>>>> idid(b)(b)
140675605442000140675605442000
>>>>>> bb..append(append(33))
>>>>>> bb
[3][3]
>>>>>> idid(b)(b)
140675605442000 # SAME!140675605442000 # SAME!
ImmutableImmutable
>>>>>> aa == 44
>>>>>> idid(a)(a)
64068966406896
>>>>>> aa == aa ++ 11
>>>>>> idid(a)(a)
6406872 # DIFFERENT!6406872 # DIFFERENT!
VariablesVariables
aa == 44 # Integer# Integer
bb == 5.65.6 # Float# Float
cc == "hello""hello" # String# String
aa == "4""4" # rebound to String# rebound to String
NamingNaming
●
lowercaselowercase
●
underscore_between_wordsunderscore_between_words
●
don't start with numbersdon't start with numbers
See PEP 8See PEP 8
PEPPEP
Python Enhancement Proposal (similar toPython Enhancement Proposal (similar to
JSR in Java)JSR in Java)
MathMath
MathMath
++,, --,, **,, //,, **** (power),(power), %% (modulo)(modulo)
Careful with integer divisionCareful with integer division
>>>>>> 3/43/4
00
>>>>>> 3/4.3/4.
0.750.75
(In Python 3(In Python 3 //// is integer divisionis integer division
operator)operator)
What happens when youWhat happens when you
raise 10 to the 100th?raise 10 to the 100th?
LongLong
>>>>>> 10**10010**100
1000000000000000000000000000000010000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
00000L00000L
LongLong (2)(2)
>>>>>> importimport syssys
>>>>>> syssys..maxintmaxint
92233720368547758079223372036854775807
>>>>>> syssys..maxintmaxint ++ 11
9223372036854775808L9223372036854775808L
StringsStrings
StringsStrings
namename == 'matt''matt'
with_quotewith_quote == "I ain't gonna""I ain't gonna"
longerlonger == """This string has"""This string has
multiple linesmultiple lines
in it"""in it"""
How do I print?How do I print?
He said, “I’m sorry”He said, “I’m sorry”
String escapingString escaping
Escape withEscape 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"
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
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'
Methods &Methods & dirdir
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']
Whats with all theWhats with all the
'__blah__''__blah__'??
dunderdunder methodsmethods
dunderdunder (double under) or "special/magic"(double under) or "special/magic"
methods determine what will happenmethods determine what will happen
whenwhen ++ ((__add____add__) or) or // ((__div____div__) is) is
called.called.
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]]) -> boolS.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, FalseReturn 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.
String methodsString methods
●
s.endswith(sub)s.endswith(sub)
Returns True if endswithReturns True if endswith subsub
●
s.find(sub)s.find(sub)
Returns index ofReturns index of subsub oror -1-1
●
s.format(*args)s.format(*args)
Places args in stringPlaces args in string
String methods (2)String methods (2)
●
s.index(sub)s.index(sub)
Returns index ofReturns index of subsub or exceptionor exception
●
s.join(list)s.join(list)
ReturnsReturns listlist items separated by stringitems separated by string
●
s.strip()s.strip()
Removes whitespace from start/endRemoves whitespace from start/end
CommentsComments
commentscomments
Comments follow aComments follow a ##
commentscomments
No multi-line commentsNo multi-line comments
More TypesMore Types
NoneNone
Pythonic way of sayingPythonic way of saying NULLNULL. Evaluates. Evaluates
to False.to False.
cc == NoneNone
booleansbooleans
aa == TrueTrue
bb == FalseFalse
sequencessequences
●
listslists
●
tuplestuples
●
setssets
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?
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']
listslists
>>>>>> aa == [][]
>>>>>> aa..append(append(44))
>>>>>> aa..append(append('hello''hello'))
>>>>>> aa..append(append(11))
>>>>>> aa..sort()sort() # in place# in place
>>>>>> printprint aa
[1, 4, 'hello'][1, 4, 'hello']
listslists
How would we find out documentationHow would we find out documentation
for a method?for a method?
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 endL.append(object) -- append object to end
List methodsList methods
●
l.append(x)l.append(x)
InsertInsert xx at end of listat end of list
●
l.extend(l2)l.extend(l2)
AddAdd l2l2 items to listitems to list
●
l.sort()l.sort()
In place sortIn place sort
List methods (2)List methods (2)
●
l.reverse()l.reverse()
Reverse list in placeReverse list in place
●
l.remove(item)l.remove(item)
Remove firstRemove first itemitem foundfound
●
l.pop()l.pop()
Remove/return item at end of listRemove/return item at end of list
DictionariesDictionaries
dictionariesdictionaries
Also calledAlso called hashmaphashmap oror associative arrayassociative array
elsewhereelsewhere
>>>>>> ageage == {}{}
>>>>>> age[age['george''george']] == 1010
>>>>>> age[age['fred''fred']] == 1212
>>>>>> age[age['henry''henry']] == 1010
>>>>>> printprint age[age['george''george']]
1010
dictionariesdictionaries (2)(2)
Find out ifFind out if 'matt''matt' inin ageage
>>>>>> 'matt''matt' inin ageage
FalseFalse
inin statementstatement
UsesUses __contains____contains__ dunder method todunder method to
determine membership. (Ordetermine membership. (Or __iter____iter__ asas
fallback)fallback)
.get.get
>>>>>> printprint age[age['charles''charles']]
Traceback (most recent call last):Traceback (most recent call last):
FileFile "<stdin>""<stdin>", line, line 11, in <module>, in <module>
KeyErrorKeyError: 'charles': 'charles'
>>>>>> printprint ageage..get(get('charles''charles',, 'Not found''Not found'))
Not foundNot found
deleting keysdeleting keys
RemovingRemoving 'charles''charles' fromfrom ageage
>>>>>> deldel age[age['charles''charles']]
deleting keysdeleting keys
deldel not innot in dirdir.. .pop.pop is an alternativeis an alternative
FunctionsFunctions
functionsfunctions
defdef add_2add_2(num):(num):
""" return 2""" return 2
more than nummore than num
""""""
returnreturn numnum ++ 22
fivefive == add_2(add_2(33))
functions (2)functions (2)
●
defdef
●
function namefunction name
●
(parameters)(parameters)
●
:: + indent+ indent
●
optional documentationoptional documentation
●
bodybody
●
returnreturn
whitespacewhitespace
Instead ofInstead of {{ use ause a :: and indentand indent
consistently (4 spaces)consistently (4 spaces)
whitespace (2)whitespace (2)
invokeinvoke python -ttpython -tt to error out duringto error out during
inconsistent tab/space usage in a fileinconsistent tab/space usage in a file
default (named) parametersdefault (named) parameters
defdef add_nadd_n(num, n(num, n=3=3):):
"""default to"""default to
adding 3"""adding 3"""
returnreturn numnum ++ nn
fivefive == add_n(add_n(22))
tenten == add_n(add_n(1515,, -5-5))
__doc____doc__
Functions haveFunctions have docstringsdocstrings. Accessible. Accessible
viavia .__doc__.__doc__ oror helphelp
__doc____doc__
>>>>>> defdef echoecho(txt):(txt):
...... "echo back txt""echo back txt"
...... returnreturn txttxt
>>>>>> 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 txtecho back txt
<BLANKLINE><BLANKLINE>
namingnaming
●
lowercaselowercase
●
underscore_between_wordsunderscore_between_words
●
don't start with numbersdon't start with numbers
●
verbverb
See PEP 8See PEP 8
ConditionalsConditionals
conditionalsconditionals
ifif gradegrade >> 9090::
printprint "A""A"
elifelif gradegrade >> 8080::
printprint "B""B"
elifelif gradegrade >> 7070::
printprint "C""C"
elseelse::
printprint "D""D"
Remember theRemember the
colon/whitespace!colon/whitespace!
BooleansBooleans
aa == TrueTrue
bb == FalseFalse
Comparison OperatorsComparison Operators
Supports (Supports (>>,, >=>=,, <<,, <=<=,, ====,, !=!=))
>>>>>> 55 >> 99
FalseFalse
>>>>>> 'matt''matt' !=!= 'fred''fred'
TrueTrue
>>>>>> isinstanceisinstance(('matt''matt',,
basestringbasestring))
TrueTrue
Boolean OperatorsBoolean Operators
andand,, oror,, notnot (for logical),(for logical), &&,, ||, and, and ^^ (for(for
bitwise)bitwise)
>>>>>> xx == 55
>>>>>> xx << -4-4 oror xx >> 44
TrueTrue
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 xx >> 1010::
printprint "Big""Big"
Chained comparisonsChained comparisons
ifif 33 << xx << 55::
printprint "Four!""Four!"
Same asSame as
ifif xx >> 33 andand xx << 55::
printprint "Four!""Four!"
IterationIteration
iterationiteration
forfor numbernumber inin [[11,,22,,33,,44,,55,,66]:]:
printprint numbernumber
forfor numbernumber inin rangerange((11,, 77):):
printprint numbernumber
rangerange notenote
Python tends to followPython 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
iteration (2)iteration (2)
Java/C-esque style of object in arrayJava/C-esque style of object in array
access (BAD):access (BAD):
animalsanimals == [["cat""cat",, "dog""dog",, "bird""bird"]]
forfor indexindex inin rangerange((lenlen(animals)):(animals)):
printprint index, animals[index]index, animals[index]
iteration (3)iteration (3)
If you need indices, useIf you need indices, use enumerateenumerate
animalsanimals == [["cat""cat",, "dog""dog",, "bird""bird"]]
forfor index, valueindex, value inin enumerateenumerate(animals):(animals):
printprint index, valueindex, value
iteration (4)iteration (4)
CanCan breakbreak out of nearest loopout of nearest loop
forfor itemitem inin sequence:sequence:
# process until first negative# process until first negative
ifif itemitem << 00::
breakbreak
# process item# process item
iteration (5)iteration (5)
CanCan continuecontinue to skip over itemsto skip over items
forfor itemitem inin sequence:sequence:
ifif itemitem << 00::
continuecontinue
# process all positive items# process all positive items
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_dictmy_dict == {{ "name""name":: "matt""matt",, "cash""cash":: 5.455.45}}
forfor keykey inin my_dictmy_dict..keys():keys():
# process key# process key
forfor valuevalue inin my_dictmy_dict..values():values():
# process value# process value
forfor key, valuekey, value inin my_dictmy_dict..items():items():
# process items# process items
passpass
passpass is a null operationis a null operation
forfor ii inin rangerange((1010):):
# do nothing 10 times# do nothing 10 times
passpass
HintHint
Don't modifyDon't modify listlist oror dictionarydictionary contentscontents
while looping over themwhile looping over them
SlicingSlicing
SlicingSlicing
Sequences (lists, tuples, strings, etc) canSequences (lists, tuples, strings, etc) can
bebe slicedsliced to pull out a single itemto pull out a single item
my_petsmy_pets == [["dog""dog",, "cat""cat",, "bird""bird"]]
favoritefavorite == my_pets[my_pets[00]]
birdbird == my_pets[my_pets[-1-1]]
Negative IndexingNegative Indexing
Proper way to think of [negativeProper way to think of [negative
indexing] is to reinterpretindexing] is to reinterpret a[-X]a[-X] asas
a[len(a)-X]a[len(a)-X]
@gvanrossum@gvanrossum
Slicing (2)Slicing (2)
Slices can take an end index, to pull out aSlices can take an end index, to pull out a
list of itemslist of items
my_petsmy_pets == [["dog""dog",, "cat""cat",, "bird""bird"]]
# a list# a list
cat_and_dogcat_and_dog == my_pets[my_pets[00::22]]
cat_and_dog2cat_and_dog2 == my_pets[:my_pets[:22]]
cat_and_birdcat_and_bird == my_pets[my_pets[11::33]]
cat_and_bird2cat_and_bird2 == my_pets[my_pets[11:]:]
Slicing (3)Slicing (3)
Slices can take a strideSlices can take a stride
my_petsmy_pets == [["dog""dog",, "cat""cat",, "bird""bird"]]
# a list# a list
dog_and_birddog_and_bird == [[00::33::22]]
zero_three_etczero_three_etc == rangerange((00,,1010))
[::[::33]]
Slicing (4)Slicing (4)
Just to beat it inJust to beat it in
vegveg == "tomatoe""tomatoe"
correctcorrect == veg[:veg[:-1-1]]
tmtetmte == veg[::veg[::22]]
eotamoteotamot == veg[::veg[::-1-1]]
File IOFile IO
File InputFile Input
Open a file to read from it (old style):Open a file to read from it (old style):
finfin == openopen(("foo.txt""foo.txt"))
forfor lineline inin fin:fin:
# manipulate line# manipulate line
finfin..close()close()
File OutputFile Output
Open a file usingOpen a file using 'w''w' toto writewrite to a file:to a file:
foutfout == openopen(("bar.txt""bar.txt",, "w""w"))
foutfout..write(write("hello world""hello world"))
foutfout..close()close()
Always remember toAlways remember to
close your files!close your files!
closing withclosing with withwith
implicitimplicit closeclose (new 2.5+ style)(new 2.5+ style)
withwith openopen(('bar.txt''bar.txt')) asas fin:fin:
forfor lineline inin fin:fin:
# process line# process line
ClassesClasses
ClassesClasses
classclass AnimalAnimal((objectobject):):
defdef __init____init__((selfself, name):, name):
selfself..namename == namename
defdef talktalk((selfself):):
printprint "Generic Animal Sound""Generic Animal Sound"
animalanimal == Animal(Animal("thing""thing"))
animalanimal..talk()talk()
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 takeall methods take selfself as first parameteras first parameter
Classes(2)Classes(2)
SubclassingSubclassing
classclass CatCat(Animal):(Animal):
defdef talktalk((selfself):):
printprint ''%s%s says, "Meow!"'says, "Meow!"' %% ((selfself..name)name)
catcat == Cat(Cat("Groucho""Groucho"))
catcat..talk()talk() # invoke method# invoke method
Classes(3)Classes(3)
classclass CheetahCheetah(Cat):(Cat):
"""classes can have"""classes can have
docstrings"""docstrings"""
defdef talktalk((selfself):):
printprint "Growl""Growl"
namingnaming
●
CamelCaseCamelCase
●
don't start with numbersdon't start with numbers
●
NounsNouns
DebuggingDebugging
Poor mansPoor mans
printprint works a lot of the timeworks a lot of the time
RememberRemember
Clean upClean up printprint statements. If you reallystatements. If you really
need them, useneed them, use logginglogging or write toor write to
sys.stdoutsys.stdout
pdbpdb
importimport pdbpdb; pdb; pdb..set_trace()set_trace()
pdbpdb commandscommands
●
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
That's allThat's all
Questions? Tweet meQuestions? Tweet me
For more details seeFor more details see
Treading on Python Volume 1Treading on Python Volume 1
@__mharrison__@__mharrison__
http://hairysun.comhttp://hairysun.com

Python Quick Start