Python Bootcamp - C4Dlab
SCI labs, University of Nairobi Nov 24th 2013 Kenny Rachuonyo
Introduction to Python
Features of Python
Simplicity - pseudocode Free and Open Source - community High-level no low-level mngt Interpreted run from source Object-Oriented simple to use Extensible C/C++
Features (cont.)
Embeddable games, graphics, Extensive Libraries (batteries included) data compression, OS, Networking, Internet, Multimedia, Graphics
Python in the Industry
Web
Google Youtube, backend tasks.. Reddit news aggregation site Disqus commenting service Numerous web frameworks django, Zope, webapp2, web.py, pyramid, flask
Python in the Industry
Desktop
Games Counterstrike, Civilization IV Cinema 4D Graphics Dropbox GUI frameworks PyGTK, PyQT,
Python in the Industry
Scientific Computing
NASA Packages: Scipy, Numpy, Matplotlib,
Python in the Industry
Mobile
Nokia Symbian Series 60 Android Scripting Layer for Android Blackberry Kivy cross-platform: iOS, Android, Linux, Windows, Mac
Python Basics
The interpreter
Installation Windows (set path)
Datatypes: int, str, float, lists, tuples, dictionaries Basic I/O
Python Basics
Variables
Dynamically-typed vs statically-typed
>>> x = 1 >>>y = hello
Strongly-typed
>>> x + y
Type function
>>> type(x)
Integer vs float
>>> z = 1.0
Python Basics
Operator + / * ** % Operation Addition Subtraction Division Multiplication Power Modulus
Python Basics
How will this be evaluated?
>>>X = 1 + 2 3 ** 4 * ( 5+6)
Python Basics
Operator Precedence rules Parenthesis Power Multiplication Addition Left-to-right
Python Basics
Integer division
>>> 4/2 >>> 5/2
Mixing integer and floats
>>> 5/2.0
Casting between integer and floats
>>> float(5) >>>int(5.0)
Python Basics
Strings sequence of characters
>>> s = hello world
Looking inside
>>> s[0]
Concatenation
>>> s = hello + world
Finding length
>>> len(s)
Slicing
>>> s = s[0:5]
Python Basics
Handy String functions
find
>>> s.find('e')
Replace
>>> n = s.replace('e', 'a' )
Make upper, lower
>>> u = s.upper()
Python Basics
Get the second word 'world' by slicing
>>> hello, world[x:y]
Python Basics
Lists collection of values
Declaring
>>> l = list() >>> l = []
Can hold different types
>>> l = [1, 'a', [2, 3], 4] >>> l[2]
Appending
>>> l.append('an item') >>>del(l[2])
Python Basics
Lists collection of values
Getting length
>>> len(l)
Slicing
>>> l[1:4]
Converting between strings and lists
>>> strlist = this is a string.split('s') >>> z.join(strlist)
Python Basics
Append an item to the list within the list
>>> l = [1, 'a', [2, 3], 4] >>> l = [1, 'a', [2, 3, 5], 4]
Python Basics
Handy functions Sum
>>> sum([2, 3, 4])
Max
>>> max([2, 3, 4])
Min
>>> min([2, 3, 4])
Python Basics
Dictionaries key, value pairs Associative array, hash table
Declaring
>>> d = dict() >>> d = {}
Setting a value
>>> d[event] = bootcamp >>> d = {event : bootcamp }
Getting a value
>>> d[event]
Python Basics
Mutability
Mutable can change
Lists, dictionary Strings, tuples
Immutable cannot change
Try set, del..
Python Basics
Casting numbers and strings
Strings and numbers
>>> int(234) >>> str(234)
Python Basics
Importing modules
>>> import math >>> math.sqrt(4) >>> from math import sqrt >>> sqrt(4)
dir() function
>>> dir(math)
Python Basics
Basic I/O
>>> name = raw_input() >>> name = raw_input(Name: )
Input numbers:
>>>age = raw_input(Age: ) >>>age = int(raw_input(Age: ))
Modules
Interactive mode vs modules Indentation
Boolean Values
True
>>> 1 < 2
False
>>> 1 > 2
Also evaluate to False:
, [], {}, 0
Loops
While loop while condition is true
x=0 while x < 10: print x x=x+1
For loop loops over items
words = ['this' , 'is', 'a', 'list'] for w in words: print w
Loop over strings, dictionaries.. Range() function
>>> range(3) >>> range(0, 10, 2)
Functions
Defining functions
def say_hello(): print hello
Calling functions
say_hello()
Parameters
def sub(a, b): s=a-b return s sub(b=3, a=2)
Functions
Commenting in Python
def sub(a, b): d = a b #subtracts b from a return d
Doc strings
def sub(a, b): this functions takes in 2 integers and returns their difference d=ab return d
File I/O
Writing to a file
f = open('text.txt', 'wb') f.write('This is a line.\n') f.close()
Reading a file
f = open('text.txt', 'rb') stream = f.read() f.close()
Accessing the Web
Establishing a connection
sockets GET, retrieve a webpage POST, save data
fopen = urllib.urlopen(http://www.google.com) data = fopen.read()
Requests and Responses
Download a webpage
Demo
Web demo Scientific computing
Next Steps
Intermediate topics:
Classes and objects in Python Regular Expressions Exceptions etc
Python on Appengine Python user group
Resources
Official Python Docs tutorial
http://docs.python.org/2/tutorial/
A byte of Python
http://www.swaroopch.com/notes/python/
Think like a Computer Scientist
http://www.openbookproject.net/thinkcs/python/english2e/