Professional Documents
Culture Documents
Read Python
Read Python
Write Python
Write Python
Book
Book
Book
Book
Treading on Python Volume 1
Treading on Python Volume 1
covers this
covers this
talk in much more detail.
talk in much more detail.
Begin
Begin
Disclaimer
Disclaimer
Hands-on at end
Hands-on at end
Three Python'isms to Remember
Three Python'isms to Remember
dir
dir
help
help
colon/indent shuffle
colon/indent shuffle
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 Cos
start-ups and large Cos
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
print
print
)
)
Hello World
Hello World
hello world
hello world
print
print
"hello world"
"hello world"
from interpreter
from interpreter
$ python
$ python
>>>
>>>
print
print
"hello world"
"hello world"
hello world
hello world
REPL
REPL
Read, Eval, Print, Loop
Read, Eval, Print, Loop
REPL
REPL
$ python
$ python
>>>
>>>
2
2
+
+
2
2
# read, eval
# read, eval
4 # print
4 # print
>>>
>>>
# repeat (loop)
# repeat (loop)
REPL (2)
REPL (2)
Many developers keep a REPL handy
Many developers keep a REPL handy
during programming
during programming
From script
From script
Make file
Make file
hello.py
hello.py
with
with
print "hello world"
print "hello world"
Run with:
Run with:
python hello.py
python hello.py
(unix) script
(unix) script
Make file
Make file
hello
hello
with
with
#!/usr/bin/env python
#!/usr/bin/env python
print "hello world"
print "hello world"
Run with:
Run with:
chmod +x hello
chmod +x hello
./hello
./hello
Python 3 hello world
Python 3 hello world
print
print
is no longer a statement, but a
is no longer a statement, but a
function
function
print("hello world")
print("hello world")
Objects
Objects
Objects
Objects
Everything in
Everything in
Python
Python
is an object that has:
is an object that has:
an
an
identity
identity
(
(
id
id
)
)
a
a
value
value
(mutable or immutable)
(mutable or immutable)
id
id
>>>
>>>
!
!
4
4
>>>
>>>
id
id
()
()
"4#"$%"
"4#"$%"
Value
Value
Mutable:
Mutable:
When you alter the item, the
When you alter the item, the
id
id
is still the same. Dictionary, List
is still the same. Dictionary, List
Immutable:
Immutable:
String, Integer, Tuple
String, Integer, Tuple
Mutable
Mutable
>>>
>>>
b
b
!
!
&'
&'
>>>
>>>
id
id
(b)
(b)
(4#")*"#*442###
(4#")*"#*442###
>>>
>>>
b
b
.
.
ppend(
ppend(
+
+
)
)
>>>
>>>
b
b
&+'
&+'
>>>
>>>
id
id
(b)
(b)
(4#")*"#*442### # ,-./!
(4#")*"#*442### # ,-./!
Immutable
Immutable
>>>
>>>
!
!
4
4
>>>
>>>
id
id
()
()
"4#"$%"
"4#"$%"
>>>
>>>
!
!
+
+
(
(
>>>
>>>
id
id
()
()
"4#"$)2 # 0122/3/45!
"4#"$)2 # 0122/3/45!
Variables
Variables
!
!
4
4
# Integer
# Integer
b
b
!
!
*."
*."
# Float
# Float
c
c
!
!
"hello"
"hello"
# String
# String
!
!
"4"
"4"
# rebound to String
# rebound to String
Naming
Naming
lowercase
lowercase
underscore_between_words
underscore_between_words
s.endswith(sub)
s.endswith(sub)
Returns True if endswith
Returns True if endswith
sub
sub
s.find(sub)
s.find(sub)
Returns index of
Returns index of
sub
sub
or
or
6(
6(
s.format(*args)
s.format(*args)
Places args in string
Places args in string
String methods (2)
String methods (2)
s.index(sub)
s.index(sub)
Returns index of
Returns index of
sub
sub
or exception
or exception
s.join(list)
s.join(list)
Returns
Returns
list
list
items separated by string
items separated by string
s.strip()
s.strip()
Removes whitespace from start/end
Removes whitespace from start/end
Comments
Comments
comments
comments
Comments follow a
Comments follow a
#
#
comments
comments
No multi-line comments
No multi-line comments
More Types
More Types
4one
4one
Pythonic way of saying
Pythonic way of saying
4B99
4B99
. Evaluates
. Evaluates
to False.
to False.
c
c
!
!
4one
4one
booleans
booleans
!
!
5rue
5rue
b
b
!
!
2lse
2lse
sequences
sequences
lists
lists
tuples
tuples
sets
sets
lists
lists
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?
lists
lists
>>> >>> dir dir(&') (&')
&:;;dd;;:@ :;;clss;;:@ :;;contins;;:@... &:;;dd;;:@ :;;clss;;:@ :;;contins;;:@...
:;;iter;;:@... :;;len;;:@... @ :ppend:@ :count:@ :;;iter;;:@... :;;len;;:@... @ :ppend:@ :count:@
:extend:@ :index:@ :insert:@ :pop:@ :remove:@ :extend:@ :index:@ :insert:@ :pop:@ :remove:@
:reverse:@ :sort:' :reverse:@ :sort:'
lists
lists
>>>
>>>
!
!
&'
&'
>>>
>>>
.
.
ppend(
ppend(
4
4
)
)
>>>
>>>
.
.
ppend(
ppend(
:hello:
:hello:
)
)
>>>
>>>
.
.
ppend(
ppend(
(
(
)
)
>>>
>>>
.
.
sort()
sort()
# in place
# in place
>>>
>>>
print
print
&(@ 4@ :hello:'
&(@ 4@ :hello:'
lists
lists
How would we find out documentation
How would we find out documentation
for a method?
for a method?
lists
lists
help
help
function:
function:
>>> >>> help(&' help(&'. .ppend) ppend)
?elp on built6in Aunction ppendF ?elp on built6in Aunction ppendF
ppend(...) ppend(...)
9.ppend(obIect) 66 ppend obIect to end 9.ppend(obIect) 66 ppend obIect to end
List methods
List methods
l.append(x)
l.append(x)
Insert
Insert
x
x
at end of list
at end of list
l.extend(l)
l.extend(l)
Add
Add
l2
l2
items to list
items to list
l.sort()
l.sort()
In place sort
In place sort
List methods (2)
List methods (2)
l.re!erse()
l.re!erse()
Reverse list in place
Reverse list in place
l.remo!e(item)
l.remo!e(item)
Remove first
Remove first
item
item
found
found
l.pop()
l.pop()
Remove/return item at end of list
Remove/return item at end of list
Dictionaries
Dictionaries
dictionaries
dictionaries
Also called
Also called
hashmap
hashmap
or
or
associative array
associative array
elsewhere
elsewhere
>>>
>>>
=e
=e
!
!
CD
CD
>>>
>>>
=e&
=e&
:=eor=e:
:=eor=e:
'
'
!
!
(#
(#
>>>
>>>
=e&
=e&
:Ared:
:Ared:
'
'
!
!
(2
(2
>>>
>>>
=e&
=e&
:henry:
:henry:
'
'
!
!
(#
(#
>>>
>>>
print
print
=e&
=e&
:=eor=e:
:=eor=e:
'
'
(#
(#
dictionaries
dictionaries
(2)
(2)
Find out if
Find out if
:mtt:
:mtt:
in
in
=e
=e
>>>
>>>
:mtt:
:mtt:
in
in
=e
=e
2lse
2lse
in
in
statement
statement
Uses
Uses
;;contins;;
;;contins;;
dunder method to
dunder method to
determine membership. (Or
determine membership. (Or
;;iter;;
;;iter;;
as
as
fallback)
fallback)
.=et
.=et
>>> >>> print print =e& =e&:chrles: :chrles:' '
5rcebcJ (most recent cll lst)F 5rcebcJ (most recent cll lst)F
2ile 2ile "KstdinG" "KstdinG"@ line @ line ( (@ in KmoduleG @ in KmoduleG
Ley/rror Ley/rrorF :chrles: F :chrles:
>>> >>> print print =e =e. .=et( =et(:chrles: :chrles:@ @ :4ot Aound: :4ot Aound:) )
4ot Aound 4ot Aound
deleting keys
deleting keys
Removing
Removing
:chrles:
:chrles:
from
from
=e
=e
GGG
GGG
del
del
=e&
=e&
:chrles:
:chrles:
'
'
deleting keys
deleting keys
del
del
not in
not in
dir
dir
.
.
.pop
.pop
is an alternative
is an alternative
Functions
Functions
functions
functions
def
def
dd;2
dd;2
(num)F
(num)F
""" return 2
""" return 2
more than num
more than num
"""
"""
return
return
num
num
+
+
2
2
Aive
Aive
!
!
dd;2(
dd;2(
+
+
)
)
functions (2)
functions (2)
deA deA
(parameters) (parameters)
F F + indent + indent
body body
return return
whitespace
whitespace
Instead of
Instead of
C
C
use a
use a
F
F
and indent
and indent
consistently (4 spaces)
consistently (4 spaces)
whitespace (2)
whitespace (2)
invoke
invoke
python 6tt
python 6tt
to error out during
to error out during
inconsistent tab/space usage in a file
inconsistent tab/space usage in a file
default (named) parameters
default (named) parameters
def
def
dd;n
dd;n
(num@ n
(num@ n
!+
!+
)F
)F
"""default to
"""default to
adding 3"""
adding 3"""
return
return
num
num
+
+
n
n
Aive
Aive
!
!
dd;n(
dd;n(
2
2
)
)
ten
ten
!
!
dd;n(
dd;n(
(*
(*
@
@
6*
6*
)
)
;;doc;;
;;doc;;
Functions have
Functions have
docstrings
docstrings
. Accessible
. Accessible
via
via
.;;doc;;
.;;doc;;
or
or
help
help
;;doc;;
;;doc;;
>>> >>> def def echo echo(txt)F (txt)F
... ... "echo bcJ txt" "echo bcJ txt"
... ... return return txt txt
>>> >>> help(echo) help(echo)
?elp on Aunction echo in module ;;min;;F ?elp on Aunction echo in module ;;min;;F
KM9-4L914/G KM9-4L914/G
echo(txt) echo(txt)
echo bcJ txt echo bcJ txt
KM9-4L914/G KM9-4L914/G
naming
naming
lowercase
lowercase
underscore_between_words
underscore_between_words
verb
verb
See PEP 8
See PEP 8
Conditionals
Conditionals
conditionals
conditionals
if
if
=rde
=rde
G
G
%#
%#
F
F
print
print
"-"
"-"
elif
elif
=rde
=rde
G
G
$#
$#
F
F
print
print
"M"
"M"
elif
elif
=rde
=rde
G
G
)#
)#
F
F
print
print
"N"
"N"
else
else
F
F
print
print
"0"
"0"
Remember the
Remember the
colon/whitespace!
colon/whitespace!
Booleans
Booleans
!
!
5rue
5rue
b
b
!
!
2lse
2lse
Comparison Operators
Comparison Operators
Supports (
Supports (
G
G
,
,
G!
G!
,
,
K
K
,
,
K!
K!
,
,
!!
!!
,
,
!!
!!
)
)
>>>
>>>
*
*
G
G
%
%
2lse
2lse
>>>
>>>
:mtt:
:mtt:
!!
!!
:Ared:
:Ared:
5rue
5rue
>>>
>>>
isinstnce
isinstnce
(
(
:mtt:
:mtt:
@
@
bsestrin=
bsestrin=
)
)
5rue
5rue
Boolean Operators
Boolean Operators
nd
nd
,
,
or
or
,
,
not
not
(for logical),
(for logical),
O
O
,
,
P
P
, and
, and
Q
Q
(for
(for
bitwise)
bitwise)
>>>
>>>
x
x
!
!
*
*
>>>
>>>
x
x
K
K
64
64
or
or
x
x
G
G
4
4
5rue
5rue
Boolean note
Boolean note
Parens are only required for precedence
Parens are only required for precedence
if
if
(x
(x
G
G
(#
(#
)F
)F
print
print
"Mi="
"Mi="
same as
same as
if
if
x
x
G
G
(#
(#
F
F
print
print
"Mi="
"Mi="
Chained comparisons
Chained comparisons
if
if
+
+
K
K
x
x
K
K
*
*
F
F
print
print
"2our!"
"2our!"
Same as
Same as
if
if
x
x
G
G
+
+
and
and
x
x
K
K
*
*
F
F
print
print
"2our!"
"2our!"
Iteration
Iteration
iteration
iteration
for
for
number
number
in
in
&
&
(
(
@
@
2
2
@
@
+
+
@
@
4
4
@
@
*
*
@
@
"
"
'F
'F
print
print
number
number
for
for
number
number
in
in
rn=e
rn=e
(
(
(
(
@
@
)
)
)F
)F
print
print
number
number
rn=e
rn=e
note
note
Python tends to follow
Python tends to follow
half-open interval
half-open interval
(
(
&strt@end)
&strt@end)
) with
) with
rn=e
rn=e
and slices.
and slices.
obIect
obIect
(base class) (fixed in 3.X)
(base class) (fixed in 3.X)
dunder
dunder
init (constructor)
init (constructor)
CamelCase
CamelCase
Nouns
Nouns
Debugging
Debugging
Poor mans
Poor mans
print
print
works a lot of the time
works a lot of the time
Remember
Remember
Clean up
Clean up
print
print
statements. If you really
statements. If you really
need them, use
need them, use
lo==in=
lo==in=
or write to
or write to
sys.stdout
sys.stdout
pdb
pdb
import
import
pdb
pdb
T pdb
T pdb
.
.
set;trce()
set;trce()
pdb
pdb
commands
commands
h
h
- help
- help
s
s
- step into
- step into
n
n
- next
- next
c
c
- continue
- continue
w
w
- where am I (in stack)?
- where am I (in stack)?
l
l
- list code around me
- list code around me
That's all
That's all
Questions? Tweet me
Questions? Tweet me
For more details see
For more details see
Treading on Python Volume 1
Treading on Python Volume 1
@__mharrison__ @__mharrison__
http://hairysun.com http://hairysun.com