Intro to Python

Presented by Chris (@0xCMP)

FALL 2014 - Programming Languages


Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C. The language provides constructs intended to enable clear programs on both a small and large scale.[20]

Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.

-- Wikipedia on Python

Overview of Python

Scripting language created by Guido van Rossum

Published in 1991 (23 years ago)

  • Applications in:
    • Web programming
      • Flask,, Django
    • Simulation and Scientific applications
      • SymPy, NumPy, Matplotlib
    • Systems programming
      • systemd
    • Desktop GUI programming
      • Tk, QT, GTK
    • Game programming
      • pygame
    • Sever scripting
    • Plugin language
    • Security tools

Who uses Python? For what?

Most places these days we know about use it for web programming.

  • Dropbox
    • Desktop app is written in python
    • Backend was python, now migrating to Go
    • Employs Guido currently (creator of python)
  • Google
    • Used python since beginining
    • One of their 3 "Offical Languages" (C++, Java, Python)
      • Languages allowed for production
    • Employed Guido for a time (2005-2012)
  • NASA
    • Some use it for data analysis in place of Matlab
  • Quora
    • Website written in Python using Django Web Framework
    • Also uses Cython for speed
  • CCP (Eve Online)

Many more at Python's website.

What is it good at?

Python is great at getting an application created quickly and cleanly

  • Single threaded or I/O intensive things
  • Works well for many applications thanks to libraries with C extensions
  • Not as fast as C/C++, but it is very easy to call C/C++ code
  • Also strong as a scripting language for a program
    • Sid Myer's Civilization mods are written in Python because of this.
    • Use of python to script robots at a semiconductor manufacturing facility in Fishkill, NY originally controlled with C++

What is it not good at?

  • Kernel development
  • Drivers
  • Crtical real time code
  • Embedded and mobile devices
  • Highly important code (airbags, audo jack)

How to setup Python

Everyone needs to go to


  • Download the latest 2.7 32 bit python.
  • Will install to C:\Python27
    • easier to go to with cd \py<tab>
    • keeps seperate from other python versions (like Python 3.4)

Mac OS X

  • You already have Python 2.7 installed.
  • Check your version with python -V in the terminal
  • Download newest version from
    • or through homebrew ( with brew install python


  • You likely have it installed already.
  • Install via apt-get
    1. Go to a terminal
    2. sudo apt-get update
    3. sudo apt-get install python

Note about installing Python 3

Do not install python 3 for this class.

I will talk about python 3 later and explain it's differences then. It is actually incompatible with python 2 in important ways.

IF, you wish to also install it for mac and linux look for python3 in brew or apt-get install

How to use it

  1. open your terminal
    • OS X: spotlight search "Terminal" or install "iTerm2"
    • Linux: refer to distro docs
    • Windows: use powershell, search in start menu by searching "powershell" and pin it to somewhere
  2. type python

The interpreter and quick intro

Lets try the following in the REPL (read, evaluate, print loop)

In [1]:
print "hello world"

hello world

In [2]:


In [3]:


Notice for later: Not true division

In [4]:


In [5]:
print "hello " * 4

hello hello hello hello 

Real Python... (for 2nd class)

Lists, Dictionaries, and Slicing

In [6]:
item = [1,2,3,4,5,6]
print item
print type(item)

[1, 2, 3, 4, 5, 6]
<type 'list'>


In [7]:
print item[3:4]
print item[:4]
print item[3:]

[1, 2, 3, 4]
[4, 5, 6]

In [8]:
myDict = {"key":"value", "name":"chris"}

In [9]:
print myDict

{'name': 'chris', 'key': 'value'}

In [10]:
print "My name is %s" % myDict['name']

My name is chris

if Statements

In [11]:
item = "5"
if item == 5:
    print "It's 5"
elif item == "5":
    print "It's %s" % item
    print "I don't know what it is."

It's 5

In [12]:
item = "5"
if item is not None:
    print "item isn't None"
    print "item is None"
print "---\nSetting item to None\n---"

item = None

if item is None:
    print "item is None"

item isn't None
Setting item to None
item is None

for loops

In [13]:
my_list = [1, 2, 3]
my_strings = ["things", "stuff", "abc"]

In [14]:
for item in my_list:
    print "Item %d" % item

Item 1
Item 2
Item 3

In [15]:
for (index, string) in enumerate(my_strings):
    print "Index of %d and value of %s" % (index, string)

Index of 0 and value of things
Index of 1 and value of stuff
Index of 2 and value of abc

Types and getting more help

In [16]:
print "Type of my_list is %s and the first element is %s" % (type(my_list), type(my_list[0]))

Type of my_list is <type 'list'> and the first element is <type 'int'>

In [17]:
print dir(my_list)

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']


In [18]:
class A_old():

class A_new(object):

print "Old type %s and New type %s" % (type(A_old), type(A_new))

Old type <type 'classobj'> and New type <type 'type'>

In [19]:
my_old_a = A_old()
my_new_a = A_new()
print "Old object %s and New object %s" % (type(my_old_a), type(my_new_a))

Old object <type 'instance'> and New object <class '__main__.A_new'>

Equality and Variables

In Python variables are more like "tags" than "boxes"

Normal ![]( ![]( Python ![]( ![](

In [20]:
item = [1, 2, 3]

In [21]:
item is item


In [22]:
item is item[:]


In [23]:
item == item[:]


Some Simple Programs

Question and answer

In [6]:
import re

user = {}
user['name'] = raw_input("What is your name? ")
user['quest'] = raw_input("What is your quest? ")
user['will-get-shrubbery'] = raw_input("We want.... ONE SHRUBBERY. ")
user['favorite-color'] = raw_input('What is your favorite colour? ')

print '-'*60

accepted = re.compile(r"^((sure|SURE)|(y|Y).*)")
accepted_status = "will acquire"

if['will-get-shrubbery']) is None:
    accepted_status = "will not aquire"

print "%s is on a quest %s and %s a shrubbery. His favorite color is %s" % (user['name'].title(),
        user['quest'].lower(), accepted_status , user['favorite-color'].upper())

What is your name?King Arthur
What is your quest?To find the Holy Grail
We want.... ONE SHRUBBERY.sure
What is your favorite colour?blue
King Arthur is on a quest to find the holy grail and will acquire a shrubbery. His favorite color is BLUE

Linked List

In [ ]: