Session One: Introductions

In which you are introduced to this class, your instructors, your environment
and your new best friend, Python.


Now let’s back up and meet each other.

Your instructor

Paul Pham
@paulpham on Slack
(paul at codefellows dot com)

Seattleite for 8 years. UW alum. Taught at The Evergreen State College in Olympia. I like startups and using technology to improve people’s lives, especially through education.

Your TAs

Grace Hatamyar
Crystal Stellwagen

How This Fits into CodeFellows


  • Foundations I: Web Dev with HTML, CSS, Javascript
  • Unix & Git for Everyone

This Class

  • Foundations II: Python

Where to Go From Here

  • Python 401 next year

(the new Development Accelerator)

Outline of this Class

  • Session 1: Dev Environment, Python Syntax
  • Session 2: Functions, Modules, Booleans
  • Session 3: Sequences, Iteration and String Formatting
  • Session 4: Dictionaries, Sets, Exceptions, and Files
  • Session 5: Arguments, Comprehensions, Lambdas and Functional Programming
  • Session 6: Intro to Object Oriented Programming
  • Session 7: Testing, More OO
  • Session 8: Optional Topics (Generators, Iterators, Decorators, and Context Managers)

Based on a curriculum designed by

Cris Ewing and Chris Barker
(cris at crisewing dot com)

Puzzle Given

Every session, you’ll be given a puzzle in the form of a Python program to write. By the end of class, you’ll know everything you need to solve the puzzle.

Today’s puzzle:

Write a Python program that prints “Hello, World!” if you call it with no arguments, otherwise prints the correct translation of “Hello, World!” in whatever language is given as the first argument to the program.


Class Meetings

  • Twice a week for 4 weeks
  • 8 total class sessions
  • Mondays and Wednesdays, 7-9pm
  • The “Easy”

Office Hours

  • 6pm before class, in the Easy
  • Instructor + all TAs will be here
  • Also by appointment with any TA


  • 2-4 homework tasks per class session.
  • Overall, about 20 homework tasks.
  • Worth 5-10 points each.


  • 0 points not turned in
  • 1 points crashes, major syntax errors.
  • 2 points crashes, minor syntax errors.
  • 3 points runs, major logical errors
  • 4 points runs, with minor logical or style errors
  • 5 points, compiles and runs perfectly with good style.

Grading Policy

In order to pass the class:

  • Attend at least 6 out of 8 classes.
  • Score 85% of the points in the class total.
  • You can resubmit to get more points.
  • Late homework will be accepted up to 1 week after the class has ended (July 15)

Intense, Fast-paced

  • Homework is assigned every class, due by the next class.
  • You can’t afford to miss more than one or two classes.
  • It’s easy to fall behind on the homework.
  • Like learning a foreign language by moving to another country for four weeks.

Who are you?

Time for Python classmate speed-dating.

Course Materials Online

Where to Find Your Stuff


There are two repositories in GitHub you will want to bookmark:

Student Homework Repository:

Fork this repository to your own github account and do homework there.

Course Materials Repository:

Contains lecture material sources, supplemental materials and homework assignments

A rendered HTML copy of all these class materials may be found online at


We will be using Canvas to track your homework submission. Grades will be entered here as well:


Class email list:

Code Fellows provides an email list for us. We will use this list for announcements. Please make sure that you are receiving the messages sent to this list:

Class Slack Channel:

The student repository README contains a link to the class chatroom. You can sign into the Codefellow Slack team <> website or you can download the desktop client for your OS.

Once you’re signed in, join the #sea-c45-python channel.

This is the official communication medium for the class, and where announcements will be made.

Introduction to Python

Python Programming

How I Learned Python, and Why I’m Glad I Did

All my friends were talking about it.

I had a new project to do, and complete freedom to choose the technology.

Python is now a standard tool for numerical and scientific computation. (e.g. Machine Learning)

Current and future dream job: Industrial Light & Magic is hiring Python coders, presumably to work on the new Star Wars movies.

What is Python?

  • Dynamic
  • Object oriented
  • Byte-compiled
  • Interpreted

But what does that mean?

Python Features


  • Unlike C, C++, C#, Java ... More like Ruby, Lisp, Perl, Javascript ...
  • Dynamic – no type declarations
    • Programs are shorter
    • Programs are more flexible
    • Less code means fewer bugs
  • Interpreted – no separate compile, build steps - programming process is simpler

What’s a Dynamic language

Dynamic typing.

  • Type checking and dispatch happen at run-time
In [1]: x = a + b
  • What is a?
  • What is b?
  • What does it mean to add them?
  • a and b can change at any time before this process

Strong typing.

In [1]: a = 5

In [2]: type(a)
Out[2]: int

In [3]: b = '5'

In [4]: type(b)
Out[4]: str
  • everything has a type.
  • the type of a thing determines what it can do.

Duck Typing

“If it looks like a duck, and quacks like a duck – it’s probably a duck”

If an object behaves as expected at run-time, it’s the right type.

Python Versions

Python 2.x

  • “Classic” Python
  • Evolved from original

Python 3.x (“py3k”)

  • Updated version
  • Removed the “warts”
  • Allowed to break code

This class uses Python 3.x (3.4 is the latest as of this writing) but we will point out the minor differences with Python 2.7, which you will see in the wild.

Other Reasons Why Python is Awesome

Keep your eye on the prize!

  • Built-in data types like lists, dictionaries, tuples that access simply by typing the right grouping symbols! [] {} ()
  • Its father, Guido van Rossum, still hearts it, actively guides its development,

and tweets about how awesome it is. * It is named after Monty Python, but it also enables a lot of snake puns. * Short, succinct metaphors and a can-do attitude * Easy to experiment and play with. Open the interpreter, start messing around.

Your computer is your own laboratory or viewport into exploring a virtual world.

Introduction to Your Environment

There are three basic elements to your environment when working with Python:

  • Your Command Line
  • Your Interpreter
  • Your Editor

Your Command Line (cli)

Having some facility on the command line is important

We won’t cover this in class, so if you are not comfortable, please bone up at home.

I suggest running through the cli tutorial at “learn code the hard way”:

You can also read the materials from the Code Fellows Unix & Git workshop:


There are a few things you can do to help make your command line a better place to work.

Part of your homework this week will be to do these things.

More on this later.

Your Interpreter

Python comes with a built-in interpreter.

You see it when you type python at the command line:

$ python3
Python 3.4.3 (default, Jun  1 2015, 09:58:35)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

That last thing you see, >>> is the “Python prompt”.

This is where you type code.

Try it out:

>>> print(u"hello world!")
hello world!
>>> 4 + 5
>>> 2 ** 8 - 1
>>> print(u"one string" + u" plus another")
one string plus another

When you are in an interpreter, there are a number of tools available to you.

There is a help system:

>>> help(str)
Help on class str in module __builtin__:

class str(basestring)
 |  str(object='') -> string
 |  Return a nice string representation of the object.
 |  If the argument is a string, the return value is the same object.

You can type q to exit the help viewer.

You can also use the dir builtin to find out about the attributes of a given object:

>>> bob = u"this is a string"
>>> dir(bob)
['__add__', '__class__', '__contains__', '__delattr__',
 '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
 '__getitem__', '__getnewargs__', '__getslice__', '__gt__',
 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
>>> help(bob.rpartition)

This allows you quite a bit of latitude in exploring what Python is.

In addition to the built-in interpreter, there are several more advanced interpreters available to you.

We’ll be using one in this course called iPython

More on this soon.

Your Editor

Typing code in an interpreter is great for exploring.

But for anything “real”, you’ll want to save the work you are doing in a more permanent fashion.

This is where an Editor fits in.

Any good text editor will do.

MS Word is not a text editor.

Nor is TextEdit on a Mac.

Notepad is a text editor – but a crappy one.

You need a real “programmers text editor”

A text editor saves only what it shows you, with no special formatting characters hidden behind the scenes.

At a minimum, your editor should have:

  • Syntax Colorization
  • Automatic Indentation

In addition, great features to add include:

  • Tab completion
  • Code linting
  • Jump-to-definition
  • Interactive follow-along for debugging

Have an editor that does all this? Feel free to use it.

If not, I suggest Sublime Text (2 or 3):

Setting Up Your Environment

Shared setup means reduced complications.

Our Class Environment

We are going to work from a common environment in this class.

We will take the time here in class to get this going.

This helps to ensure that you will be able to work.

Step 1: Python 3.4

You have this already, right?

$ python
Python 3.4.3 (default, Jun  1 2015, 09:58:35)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D

If not:

Step 2: Pip

Python comes with quite a bit (“batteries included”).

Sometimes you need a bit more.

Pip allows you to install Python packages to expand your system.

pip comes preinstalled with Python 3.4.

You can check to see if you have it installed by typing:

$ pip --version
pip 7.0.3 from /usr/local/lib/python3.4/site-packages (python 3.4)

(or go to:

Once you’ve installed pip, you use it to install Python packages by name:

$ pip install foobar

To find packages (and their proper names), you can search the python package index (PyPI):

Step 3: Optional – Virtualenv

Python packages come in many versions.

Often you need one version for one project, and a different one for another.

Virtualenv allows you to create isolated environments.

You can then install potentially conflicting software safely.

For this class, this is no big deal, but as you start to work on “real” projects, it can be a key tool.

If you want to install it, here are some notes:

Intro to VirtualEnv

Step 4: Fork Class Repository

GitHub is an industry-standard system for collaboration on software projects – particularly open source ones.

We will use it this class to manage submitting and reviewing your work, etc.

Wait! Don’t have a gitHub account? Set one up now.

Next, you’ll make a copy of the class repository using git.

The canonical copy is in the CodeFellows organization on GitHub:

Open that URL, and click on the Fork button at the top right corner.

This will make a copy of this repository in your github account.

From here, you’ll want to make a clone of your copy on your local machine.

At your command line, run the following commands:

$ cd your_working_directory_for_the_class
$ git clone<yourname>/sea-c45-python.git

(you can copy and paste that link from the gitHub page)

If you have an SSH key set up for gitHub, you’ll want to do this instead:<yourname>/sea-c45-python.git

Remember, <yourname> should be replaced by your github account name.

Brief Aside

Remember our puzzle? Let’s go into our recently cloned class repo and see some starter code.

cd examples/session01

Now back to show!

Step 5: Install Requirements

As this is an intro class, we are going to use almost entirely features of standand library. But there are a couple things you may want:


$pip install ipython

If you are using SublimeText, you may want:

$ pip install PdbSublimeTextSupport

Introduction to iPython

iPython Overview

You have now installed iPython.

iPython is an advanced Python interpreter that offers enhancements.

You can read more about it in the official documentation.

Specifically, you’ll want to pay attention to the information about

Using iPython for Interactive Work.

The very basics of iPython

iPython can do a lot for you, but for starters, here are the key pieces you’ll want to know:

Start it up


$ ipython
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
Type "copyright", "credits" or "license" for more information.

IPython 2.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

This is the stuff I use every day:

  • command line recall:
    • hit the “up arrow” key
    • if you have typed a bit, it will find the last command that starts the same way.
  • basic shell commands:
    • ls, cd, pwd
  • any shell command:
    • ! the_shell_command
  • pasting from the clipboard:
    • %paste (this keeps whitespace cleaner for you)
  • getting help:
    • something?
  • tab completion:
    • something.<tab>
  • running a python file:
    • run

That’s it – you can get a lot done with those.

How to run a python file

A file with python code in it is a ‘module’ or ‘script’

(more on the distiction later on...)

It should be named with the .py extension:

To run it, you have a couple options:

  1. call python on the command line, and pass in your module name
$ python
  1. run iPython, and run it from within iPython with the run command
In [1]: run

Basic Python Syntax

Expressions, Statements,
Values, Types, and Symbols

Code structure

Each line is a piece of code.


In [3]: # everything after a '#' is a comment


In [4]: # evaluating an expression results in a value

In [5]: 3 + 4
Out[5]: 7


In [6]: # statements do not return a value, may contain an expression

In [7]: print(u"this")

In [8]: line_count = 42

In [9]:


In Python 2.x, printing is a statement. In Python 3, it was changed to a function.

You can get the Python 3 behavior in Python 2.6+ using the __future__ module.

from __future__ import print_function

For purposes of writing cross-compatible code, this is a good idea. Please use this idiom in your code.

It’s kind of obvious, but handy when playing with code:

In [1]: from __future__ import print_function
In [2]: print(u"something")

You can print multiple things:

In [3]: print(u"the value is", 5)
the value is 5

Python automatically adds a newline, which you can change with end argument:

In [12]: for i in range(5):
   ....:     print(u"the value is", end=' ')
   ....:     print(i)
the value is 0
the value is 1
the value is 2
the value is 3
the value is 4

Any python object can be printed (though it might not be pretty...)

In [1]: class Bar(object):
   ...:     pass

In [2]: print(Bar)
<class '__main__.Bar'>

Blocks of code are delimited by a colon and indentation:

def a_function():
for i in range(100):


Python uses whitespace to delineate structure.

This means that in Python, whitespace is significant.

(but ONLY for newlines and indentation)

The standard is to indent with 4 spaces.



These two blocks look the same:

for i in range(100):
for i in range(100):

But they are not:

for i in range(100):
for i in range(100):



make sure your editor is set to use spaces only –

ideally even when you hit the <tab> key


  • Values are pieces of unnamed data: 42, u'Hello, world',
  • In Python, all values are objects
    • Try dir(42) - lots going on behind the curtain!
  • Every value belongs to a type
    • Try type(42) - the type of a value determines what it can do

Literals for the Basic Value types:

  • floating point: 3.4
  • integers: 456
  • u"a bit of text"
  • u'a bit of text'
  • (either single or double quotes work – why?)
Boolean values:
  • True
  • False

(There are intricacies to all of these that we’ll get into later)

Values in Action

An expression is made up of values and operators

  • An expression is evaluated to produce a new value: 2 + 2
    • The Python interpreter can be used as a calculator to evaluate expressions
  • Integer vs. float arithmetic
    • 1 / 2 versus 1. / 2
    • (Python 3 smooths this out)
    • Always use / when you want float results, // when you want floored (integer) results
  • Type conversions
    • This is the source of many errors, especially in handling text
    • Python 3 will not implicitly convert bytes to unicode
  • Type errors - checked at run time only


Symbols are how we give names to values (objects).

  • Symbols must begin with an underscore or letter
  • Symbols can contain any number of underscores, letters and numbers
    • this_is_a_symbol
    • this_is_2
    • _AsIsThis
    • 1butThisIsNot
    • nor-is-this
  • Symbols don’t have a type; values do
    • This is why python is ‘Dynamic’

Symbols and Type

Evaluating the type of a symbol will return the type of the value to which it is bound.

In [19]: type(42)
Out[19]: int

In [20]: type(3.14)
Out[20]: float

In [21]: a = 42

In [22]: b = 3.14

In [23]: type(a)
Out[23]: int

In [25]: a = b

In [26]: type(a)
Out[26]: float


A symbol is bound to a value with the assignment operator: =

  • This attaches a name to a value
  • A value can have many names (or none!)
  • Assignment is a statement, it returns no value

Evaluating the name will return the value to which it is bound

In [26]: name = u"value"

In [27]: name
Out[27]: u'value'

In [28]: an_integer = 42

In [29]: an_integer
Out[29]: 42

In [30]: a_float = 3.14

In [31]: a_float
Out[31]: 3.14

In-Place Assignment

You can also do “in-place” assignment with +=.

In [32]: a = 1

In [33]: a
Out[33]: 1

In [34]: a = a + 1

In [35]: a
Out[35]: 2

In [36]: a += 1

In [37]: a
Out[37]: 3

also: -=, *=, /=, **=, %=

(not quite – really in-place assignment for mutables....)

Multiple Assignment

You can assign multiple variables from multiple expressions in one statement

In [48]: x = 2

In [49]: y = 5

In [50]: i, j = 2 * x, 3 ** y

In [51]: i
Out[51]: 4

In [52]: j
Out[52]: 243

Python evaluates all the expressions on the right before doing any assignments

Nifty Python Trick

Using this feature, we can swap values between two symbols in one statement:

In [51]: i
Out[51]: 4

In [52]: j
Out[52]: 243

In [53]: i, j = j, i

In [54]: i
Out[54]: 243

In [55]: j
Out[55]: 4

Multiple assignment and symbol swapping can be very useful in certain contexts


You can test for the equality of certain values with the == operator

In [77]: val1 = 20 + 30

In [78]: val2 = 5 * 10

In [79]: val1 == val2
Out[79]: True

In [80]: val3 = u'50'

In [81]: val1 == val3
Out[84]: False

Operator Precedence

Operator Precedence determines what evaluates first:

4 + 3 * 5 != (4 + 3) * 5

To force statements to be evaluated out of order, use parentheses.

Python Operator Precedence

Parentheses and Literals:

(), [], {}

"", b'', u''

Function Calls:
Slicing and Subscription:


b[0], c['key']

Attribute Reference:
Bitwise NOT, Unary Signing:


+x, -x

Multiplication, Division, Modulus:
*, /, %
Addition, Subtraction:
+, -
Bitwise operations:

<<, >>,

&, ^, |

<, <=, >, >=, !=, ==
Membership and Identity:
in, not in, is, is not
Boolean operations:
or, and, not
Anonymous Functions:

String Literals

You define a string value by writing a literal:

In [1]: u'a string'
Out[1]: u'a string'

In [2]: u"also a string"
Out[2]: u'also a string'

In [3]: u"a string with an apostrophe: isn't it cool?"
Out[3]: u"a string with an apostrophe: isn't it cool?"

In [4]: u'a string with an embedded "quote"'
Out[4]: u'a string with an embedded "quote"'

(what’s the ‘u‘ about?)


Python defines a number of keywords

These are language constructs.

You cannot use these words as symbols.

and       del       from      not       while
as        elif      global    or        with
assert    else      if        pass      yield
break     except    import    print
class     exec      in        raise
continue  finally   is        return
def       for       lambda    try

If you try to use any of the keywords as symbols, you will cause a SyntaxError:

In [13]: del = u"this will raise an error"
  File "<ipython-input-13-c816927c2fb8>", line 1
    del = u"this will raise an error"
SyntaxError: invalid syntax
In [14]: def a_function(else=u'something'):
   ....:     print(else)
  File "<ipython-input-14-1dbbea504a9e>", line 1
    def a_function(else=u'something'):
SyntaxError: invalid syntax


Python also has a number of pre-bound symbols, called builtins

Try this:

In [6]: dir(__builtins__)

You are free to rebind these symbols:

In [15]: type(u'a new and exciting string')
Out[15]: unicode

In [16]: type = u'a slightly different string'

In [17]: type(u'type is no longer what it was')
TypeError                                 Traceback (most recent call last)
<ipython-input-17-907616e55e2a> in <module>()
----> 1 type(u'type is no longer what it was')

TypeError: 'unicode' object is not callable

In general, this is a BAD IDEA.


Notice that the first batch of __builtins__ are all Exceptions

Exceptions are how Python tells you that something has gone wrong.

There are several exceptions that you are likely to see a lot of:

  • NameError: indicates that you have tried to use a symbol that is not bound to a value.
  • TypeError: indicates that you have tried to use the wrong kind of object for an operation.
  • SyntaxError: indicates that you have mis-typed something.
  • AttributeError: indicates that you have tried to access an attribute or method that an object does not have (this often means you have a different type of object than you expect)

The if Statement

In order to do anything interesting at all (including this week’s homework), you need to be able to make a decision.

In [12]: def test(a):
   ....:     if a == 5:
   ....:         print(u"that's the value I'm looking for!")
   ....:     elif a == 7:
   ....:         print(u"that's an OK number")
   ....:     else:
   ....:         print(u"that number won't do!")

In [13]: test(5)
that's the value I'm looking for!

In [14]: test(7)
that's an OK number

In [15]: test(14)
that number won't do!

There is more to it than that, but this will get you started.


What is a function?

A function is a self-contained chunk of code

You use them when you need the same code to run multiple times, or in multiple parts of the program.


Or just to keep the code clean

Functions can take and return information

Minimal Function does nothing

def <name>():

Pass Statement (Note the indentation!)

def minimal():

Functions: def

def is a statement:

  • it is executed
  • it creates a local variable

function defs must be executed before the functions can be called:

In [23]: unbound()
NameError                                 Traceback (most recent call last)
<ipython-input-23-3132459951e4> in <module>()
----> 1 unbound()

NameError: name 'unbound' is not defined
In [18]: def simple():
   ....:     print(u"I am a simple function")

In [19]: simple()
I am a simple function

Calling Functions

You call a function using the function call operator (parens):

In [2]: type(simple)
Out[2]: function
In [3]: simple
Out[3]: <function __main__.simple>
In [4]: simple()
I am a simple function

Functions: Call Stack

functions call functions – this makes an execution stack – that’s all a trace back is

In [5]: def exceptional():
   ...:     print(u"I am exceptional!")
   ...:     print(1/0)
In [6]: def passive():
   ...:     pass
In [7]: def doer():
   ...:     passive()
   ...:     exceptional()

You’ve defined three functions, one of which will call the other two.

Functions: Tracebacks

In [8]: doer()
I am exceptional!
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-8-685a01a77340> in <module>()
----> 1 doer()

<ipython-input-7-aaadfbdd293e> in doer()
      1 def doer():
      2     passive()
----> 3     exceptional()

<ipython-input-5-d8100c70edef> in exceptional()
      1 def exceptional():
      2     print(u"I am exceptional!")
----> 3     print(1/0)

ZeroDivisionError: integer division or modulo by zero

Functions: return

Every function ends by returning a value

This is actually the simplest possible function:

def fun():
    return None

if you don’t explicilty put return there, Python will:

In [9]: def fun():
   ...:     pass
In [10]: fun()
In [11]: result = fun()
In [12]: print(result)

note that the interpreter eats None

Only one return statement will ever be executed.


Anything after a executed return statement will never get run.

This is useful when debugging!

In [14]: def no_error():
   ....:     return u'done'
   ....:     # no more will happen
   ....:     print(1/0)
In [15]: no_error()
Out[15]: u'done'

However, functions can return multiple results:

In [16]: def fun():
   ....:     return (1, 2, 3)
In [17]: fun()
Out[17]: (1, 2, 3)

Remember multiple assignment?

In [18]: x,y,z = fun()
In [19]: x
Out[19]: 1
In [20]: y
Out[20]: 2
In [21]: z
Out[21]: 3

Functions: parameters

In a def statement, the values written inside the parens are parameters

In [22]: def fun(x, y, z):
   ....:     q = x + y + z
   ....:     print(x, y, z, q)

x, y, z are local symbols – so is q

Functions: arguments

When you call a function, you pass values to the function parameters as arguments

In [23]: fun(3, 4, 5)
3 4 5 12

The values you pass in are bound to the symbols inside the function and used.

Enough For Now

That’s it for our basic intro to Python

Before next session, you’ll use what you’ve learned here today to do some exercises in Python programming

Unicode Notes

You might need this for the puzzle if you use foreign languages.

To put unicode in your source file, put:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

at the top of your file ... and be sure to save it as utf-8! (file->save with encoding in Sublime)

You also might want to put:

from __future__ import unicode_literals

Additional notes on using Unicode in Python see:

Puzzle Solved

Now it’s time to solve our puzzle. Remember it?

Write a Python program that prints “Hello, World!” if you call it with no arguments, otherwise prints the correct translation of “Hello, World!” in whatever language is given as the first argument to the program.

Partner up and let’s get to work!


Three Tasks due by Wednesday, check them out on Canvas.

Homework Task 1: Python Pre-work

Homework Task 2: Style Checking

Homework Task 3: Gitting To Know You

Homework Task 4: Break These Functions