PyClass

Today

How much experience do you have with Python?

Why use Python?

Speed Comparison

Python vs C

Python vs Ruby: The Zen of Python

 
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    

Python vs Ruby: The Zen(?) of Ruby

 
      There's more than one way to do it.
      The principle of least surprise.
    

Getting Python: Mac + Linux

[17:21:17] batman@batman ~ $ python
Python 2.6.5 (r265:79063, Nov 29 2010, 10:18:27) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Getting Python: Windows

http://www.python.org/download/releases/2.7.2/

Learn You a Python

print "Hello World!"
  

How To Run Programs

Interactive Prompt:

 
[13:56:25] batman@batman ~ $ python
Python 2.6.5 (r265:79063, Nov 29 2010, 10:18:27) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 2
>>> a + a
4
>>> 
    

Running a Script:

[13:56:58] batman@batman ~ $ python test.py
    

Basic Variables

a = 1
b = "hello"
  

Dynamically typed

a = 1
b = "hello"
  

vs C++:

int a = 1;
string b = "hello";
  

Strongly typed

a = 1
b = "hello"
c = a + b # error!
  

vs Javascript:

a = 1;
b = "hello";
c = a + b; // 1hello
  

Comments

c = a + b # error!
"""
I am a multiline comment/string!
Hello!
"""
  

Variables

a = 1 # int
b = "monty" # string
c = 2.0 # float
d = False # Boolean
e = None # null
  
>>> a = 1
>>> type(a)

>>> 
  

Numbers

>>> a = 1
>>> b = 2
>>> a + b
3
>>> a / b # integer division!
0
>>>

Strings

>>> a = "monty"
>>> b = "python"
>>> a + " " + b
'monty python'
>>> a[0]
'm'
>>> a[1]
'o'
>>> 
  

Strings as lists of characters

>>> a = "monty"
>>> a[0] # starts at zero
'm'
>>> a[0:3]
'mon'
>>> a [2:]
'nty'
>>> a[:]
'monty'
>>> 
>>> a.find("n")
2
>>> sorted(a)
['m', 'n', 'o', 't', 'y']
>>> 
  

Functions

# functions we have already used:
print "hello"
print sorted("howdy")
  

Making Our Own Functions

def say_hello(name):
  print "hello, " + name + "!" # indentation is important!

say_hello("guido")
  

Another Example

def add(a, b):
  return a + b

print add(2, 4)
  

Making Decisions: The if Statement

def rate(language):
  if language == "python":
    return "awesome!"
  else:
    return "meh"
  

if/elif

def rate(language):
  if language == "python":
    return "awesome!"
  elif language == "php":
    return "yuck!"
  else:
    return "meh"
  

or

def rate(language):
  if language == "python":
    return "awesome!"
  elif language == "php" or language == "perl":
    return "yuck!"
  else:
    return "meh"
  

and/or

>>> a = True
>>> b = False
>>> a or b
True
>>> b or a
True
>>> b and a
False
>>> 
  

Problem?

http://codingbat.com/python/Warmup-1

Sneak Preview: Lists

a = [1,2,"foo"]
b = [[1,2],[3]]
print b[0] # prints [1,2]
print a.reverse()
a[0] = "44"
print a
x = [1,2,6,4,3,2,6,4]
x.sort()
print x
  

More things you can do with lists: http://docs.python.org/tutorial/datastructures.html