python bootcamp - c4dlab, university of nairobi

35
Python Bootcamp - C4Dlab SCI labs, University of Nairobi Nov 24th 2013 Kenny Rachuonyo

Upload: kenny-rachuonyo

Post on 03-Sep-2014

841 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Python bootcamp - C4Dlab, University of Nairobi

Python Bootcamp - C4Dlab

SCI labs, University of NairobiNov 24th 2013

Kenny Rachuonyo

Page 2: Python bootcamp - C4Dlab, University of Nairobi

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++

Page 3: Python bootcamp - C4Dlab, University of Nairobi

Features (cont.)

● Embeddable – games, graphics, ● Extensive Libraries (batteries included) – data

compression, OS, Networking, Internet, Multimedia, Graphics

Page 4: Python bootcamp - C4Dlab, University of Nairobi

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

Page 5: Python bootcamp - C4Dlab, University of Nairobi

Python in the Industry

Desktop● Games – Counterstrike, Civilization IV● Cinema 4D – Graphics● Dropbox● GUI frameworks – PyGTK, PyQT,

Page 6: Python bootcamp - C4Dlab, University of Nairobi

Python in the Industry

Scientific Computing● NASA● Packages: Scipy, Numpy, Matplotlib,

Page 7: Python bootcamp - C4Dlab, University of Nairobi

Python in the Industry

Mobile● Nokia Symbian Series 60 ● Android – Scripting Layer for Android● Blackberry● Kivy – cross-platform: iOS, Android, Linux,

Windows, Mac

Page 8: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

● The interpreter– Installation

– Windows (set path)

● Datatypes: int, str, float, lists, tuples, dictionaries

● Basic I/O

Page 9: Python bootcamp - C4Dlab, University of Nairobi

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

Page 10: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

Operator Operation

+ Addition

- Subtraction

/ Division

* Multiplication

** Power

% Modulus

Page 11: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

● How will this be evaluated?>>>X = 1 + 2 – 3 ** 4 * ( 5+6)

Page 12: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

● Operator Precedence rules

Parenthesis

Power

Multiplication

Addition

Left-to-right

Page 13: Python bootcamp - C4Dlab, University of Nairobi

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)

Page 14: Python bootcamp - C4Dlab, University of Nairobi

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]

Page 15: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

Handy String functions● find

>>> s.find('e')

● Replace>>> n = s.replace('e', 'a' )

● Make upper, lower>>> u = s.upper()

Page 16: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

● Get the second word 'world' by slicing>>> “hello, world”[x:y]

Page 17: Python bootcamp - C4Dlab, University of Nairobi

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])

Page 18: Python bootcamp - C4Dlab, University of Nairobi

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)

Page 19: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

● Append an item to the list within the list>>> l = [1, 'a', [2, 3], 4]

>>> l = [1, 'a', [2, 3, 5], 4]

Page 20: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

● Handy functions

Sum>>> sum([2, 3, 4])

Max>>> max([2, 3, 4])

Min>>> min([2, 3, 4])

Page 21: Python bootcamp - C4Dlab, University of Nairobi

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”]

Page 22: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

Mutability● Mutable – can change

– Lists, dictionary

● Immutable – cannot change– Strings, tuples

● Try set, del..

Page 23: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

Casting – numbers and strings● Strings and numbers

>>> int(“234”)

>>> str(234)

Page 24: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

● Importing modules>>> import math

>>> math.sqrt(4)

>>> from math import sqrt

>>> sqrt(4)

● dir() function >>> dir(math)

Page 25: Python bootcamp - C4Dlab, University of Nairobi

Python Basics

● Basic I/O>>> name = raw_input()

>>> name = raw_input(“Name: “)

Input numbers:>>>age = raw_input(“Age: “)

>>>age = int(raw_input(“Age: “))

Page 26: Python bootcamp - C4Dlab, University of Nairobi

Modules

● Interactive mode vs modules● Indentation

Page 27: Python bootcamp - C4Dlab, University of Nairobi

Boolean Values

● True>>> 1 < 2

● False>>> 1 > 2

● Also evaluate to False:

“”, [], {}, 0

Page 28: Python bootcamp - C4Dlab, University of Nairobi

Loops

● While loop – while condition is truex = 0

while x < 10:

print x

x = x + 1

● For loop – loops over itemswords = ['this' , 'is', 'a', 'list']

for w in words:

print w

● Loop over strings, dictionaries..● Range() function

>>> range(3)

>>> range(0, 10, 2)

Page 29: Python bootcamp - C4Dlab, University of Nairobi

Functions● Defining functions

def say_hello():

print “hello”

● Calling functionssay_hello()

● Parametersdef sub(a, b):

s = a - b

return s

sub(b=3, a=2)

Page 30: Python bootcamp - C4Dlab, University of Nairobi

Functions

● Commenting in Pythondef sub(a, b):

d = a – b #subtracts b from a

return d

● Doc stringsdef sub(a, b):

“””this functions takes in 2 integers and returns their difference”””

d = a – b

return d

Page 31: Python bootcamp - C4Dlab, University of Nairobi

File I/O

● Writing to a filef = open('text.txt', 'wb')

f.write('This is a line.\n')

f.close()

● Reading a filef = open('text.txt', 'rb')

stream = f.read()

f.close()

Page 32: Python bootcamp - C4Dlab, University of Nairobi

Accessing the Web

● Establishing a connection– sockets

● Requests and Responses– GET, retrieve a webpage

– POST, save data

● Download a webpagefopen = urllib.urlopen(“http://www.google.com”)

data = fopen.read()

Page 33: Python bootcamp - C4Dlab, University of Nairobi

Demo

● Web demo● Scientific computing

Page 34: Python bootcamp - C4Dlab, University of Nairobi

Next Steps

● Intermediate topics:– Classes and objects in Python

– Regular Expressions

– Exceptions etc

● Python on Appengine ● Python user group

Page 35: Python bootcamp - C4Dlab, University of Nairobi

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/