1. Python Bootcamp - C4Dlab
SCI labs, University of Nairobi
Nov 24th 2013
Kenny Rachuonyo
2. 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++
4. 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
5. Python in the Industry
Desktop
● Games – Counterstrike, Civilization IV
● Cinema 4D – Graphics
● Dropbox
● GUI frameworks – PyGTK, PyQT,
6. Python in the Industry
Scientific Computing
● NASA
● Packages: Scipy, Numpy, Matplotlib,
7. Python in the Industry
Mobile
● Nokia Symbian Series 60
● Android – Scripting Layer for Android
● Blackberry
● Kivy – cross-platform: iOS, Android, Linux,
Windows, Mac
9. 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
28. 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)
30. 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=a–b
return d
31. 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()
32. Accessing the Web
● Establishing a connection
– sockets
● Requests and Responses
– GET, retrieve a webpage
– POST, save data
● Download a webpage
fopen = urllib.urlopen(“http://www.google.com”)
data = fopen.read()
34. Next Steps
● Intermediate topics:
– Classes and objects in Python
– Regular Expressions
– Exceptions etc
● Python on Appengine
● Python user group
35. 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/