Python Fundamentals
Getting Started
Robert Smallshire Austin Bingham
@robsmallshire @austin_bingham
rob@sixty-north.com austin@sixty-north.com
Presenter
Sunday, 28 July, 13
Sunday, 28 July, 13
>>> if h > 50:
... print("Greater than 50")
... elif h < 20:
... print("Less than 20")
... else:
... print("Between 20 and 50")
...
Between 20 and 50
Sunday, 28 July, 13
Python Release Timeline
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
Python 2
Python 3
Sunday, 28 July, 13
Python Release Timeline
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
Python 2
straightforward
2.4 2.5 2.6 2.7 upgrade path
Python 3
3.0 3.1 3.2 3.3 3.4
Sunday, 28 July, 13
Portable
Platform Specific
Installation
Sunday, 28 July, 13
Sunday, 28 July, 13
Sunday, 28 July, 13
Sunday, 28 July, 13
>>> Read
Evaluate
Print
Loop
Sunday, 28 July, 13
>>> R E P L
Sunday, 28 July, 13
Significant Indentation in Python
"""Class model for aircraft flights."""
class Flight:
"""A flight with a particular aircraft."""
def __init__(self, number, aircraft):
if not number[:2].isalpha():
raise ValueError("No airline code in '{}'".format(number))
if not number[:2].isupper():
raise ValueError("Invalid airline code '{}'".format(number))
if not (number[2:].isdigit() and int(number[2:]) <= 9999):
raise ValueError("Invalid route number '{}'".format(number))
self._number = number
self._aircraft = aircraft
rows, seats = self._aircraft.seating_plan()
self._seating = [None] + [ {letter:None for letter in seats} for _ in rows ]
def _passenger_seats(self):
"""An iterable series of passenger seating allocations."""
row_numbers, seat_letters = self._aircraft.seating_plan()
for row in row_numbers:
for letter in seat_letters:
passenger = self._seating[row][letter]
if passenger is not None:
yield (passenger, "{}{}".format(row, letter))
Sunday, 28 July, 13
Significant Indentation in Python
Sunday, 28 July, 13
Significant Indentation in Python
Four spaces per level of indentation
:
4
:
4 :
4
:
4
:
4
:
4
:
4 :
4 :
4
Sunday, 28 July, 13
Significant Whitespace
1.Requires readable code
2.No clutter
3.Human and computer can’t get
out of sync
Sunday, 28 July, 13
Significant Whitespace
Rules
1.Prefer four spaces
2.Never mix spaces and tabs
3.Be consistent on consecutive lines
4.Only deviate to improve readability
Sunday, 28 July, 13
Programming as Guido intended it
Sunday, 28 July, 13
Moment of Zen
Readability Counts
Clarity Matters
So readability makes
For valuable code
Sunday, 28 July, 13
Python Standard Library
import module_name
d
de
lu
nc
sI
rie
tte
Sunday, 28 July, 13 Ba
Sunday, 28 July, 13
n = 13
32-bit int
Sunday, 28 July, 13
Scalar types and values
Sunday, 28 July, 13
Scalar types and values
int
arbitrary precision integer
42
float
64-bit floating point numbers
4.2
NoneType
None the null object
bool bool
True False boolean logical values
Sunday, 28 July, 13
int
unlimited precision signed integer
Sunday, 28 July, 13
float
IEEE-754 double precision (64-bit)
53 bits of binary precision
15 to 16 bits of decimal precision
Sunday, 28 July, 13
None The sole value of NoneType.
Often used to represent the absence of a value.
Not displayed by the REPL.
Sunday, 28 July, 13
bool
Boolean logical value.
Either True or False.
Sunday, 28 July, 13
Relational Operators
== value equality / equivalence
!= value inequality / inequivalence
< less-than
> greater-than
<= less-than or equal to
>= greater-than or equal to
Sunday, 28 July, 13
Conditional Statements
if expr:
print("expr is True")
exp r is co nve rte d to b oo l as if by
the bool() constructor
Sunday, 28 July, 13
if h > 50:
print("Greater than 50")
else:
if h < 20:
print("Less than 20")
else:
print("Between 20 and 50")
Sunday, 28 July, 13
if h > 50:
print("Greater than 50")
else:
if h < 20:
print("Less than 20")
else:
print("Between 20 and 50")
Sunday, 28 July, 13
if h > 50:
print("Greater than 50")
else:
if h < 20:
print("Less than 20")
else:
print("Between 20 and 50")
th e i f k e y w o r d to
Python provides e l
fo r n e s te d i f .. . e l s e
eliminate the need
structure s in m a n y c a s es .
Sunday, 28 July, 13
if h > 50:
print("Greater than 50")
elif h < 20:
print("Less than 20")
else:
print("Between 20 and 50")
th e i f k e y w o r d to
Python provides e l
fo r n e s te d i f .. . e l s e
eliminate the need
structure s in m a n y c a s es .
Sunday, 28 July, 13
Flat is better
than nested
if h > 50:
print("Greater than 50")
elif h < 20:
print("Less than 20")
else:
print("Between 20 and 50")
th e i f k e y w o r d to
Python provides e l
fo r n e s te d i f .. . e l s e
eliminate the need
structure s in m a n y c a s es .
Sunday, 28 July, 13
while loops
while expr:
print("loop while expr is True")
exp r is co nve rte d to b oo l as if by
the bool() constructor
Sunday, 28 July, 13
breaking out
while True:
if expr:
break
print("Go here on break")
The break keyword
terminates the
innermost loop, tran
sferring execution
to the first statement
after the loop
Sunday, 28 July, 13
Getting Started – Summary
Obtaining and installing Python 3
Windows
Ubuntu Linux
Mac OS X
Read-Eval-Print-Loop or REPL
Simple arithmetic with + - * / % and //
Assigning objects to named variables with the = operator
print()
Exiting the REPL
Ctrl-Z on Windows
Ctrl-D on Linux and Mac.
Significant indentation - usually four spaces
Python Enhancement Proposals
PEP 8 – Python Style Guide
PEP 20 – The Zen of Python
Sunday, 28 July, 13
Getting Started – Summary
Importing Python Standard Library modules:
import module
from module import function
from module import function as alias
Finding and browsing help()
Scalar built-in types
int float None bool
conversions between types
Relational operators == != < > <= >= for equivalence and ordering
Conditional statements with if ... elif ... else
while loops
Interrupting execution with Ctrl-C to create a KeyboardInterrupt
exception
Breaking out of loops with break
Augmented assignment operators for modifying objects in-place
Requesting text from the user with input()
Sunday, 28 July, 13