Python Fundamentals: Getting Started
Python Fundamentals: Getting Started
Getting Started
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
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 Flight:
"""A flight with a particular aircraft."""
if not number[:2].isupper():
raise ValueError("Invalid airline code '{}'".format(number))
self._number = number
self._aircraft = aircraft
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
2.No clutter
Sunday, 28 July, 13
Significant Whitespace
Rules
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
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
< less-than
> greater-than
Sunday, 28 July, 13
Conditional Statements
if expr:
print("expr is True")
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")
Sunday, 28 July, 13
breaking out
while True:
if expr:
break
print("Go here on break")
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