Android Bootcamp Tanzania:Costech mobile bootcamp monetazation
Python bootcamp - C4Dlab, University of Nairobi
-
Upload
kenny-rachuonyo -
Category
Documents
-
view
836 -
download
0
description
Transcript of Python bootcamp - C4Dlab, University of Nairobi
![Page 1: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/1.jpg)
Python Bootcamp - C4Dlab
SCI labs, University of NairobiNov 24th 2013
Kenny Rachuonyo
![Page 2: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/3.jpg)
Features (cont.)
● Embeddable – games, graphics, ● Extensive Libraries (batteries included) – data
compression, OS, Networking, Internet, Multimedia, Graphics
![Page 4: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/6.jpg)
Python in the Industry
Scientific Computing● NASA● Packages: Scipy, Numpy, Matplotlib,
![Page 7: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/10.jpg)
Python Basics
Operator Operation
+ Addition
- Subtraction
/ Division
* Multiplication
** Power
% Modulus
![Page 11: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/11.jpg)
Python Basics
● How will this be evaluated?>>>X = 1 + 2 – 3 ** 4 * ( 5+6)
![Page 12: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/12.jpg)
Python Basics
● Operator Precedence rules
Parenthesis
Power
Multiplication
Addition
Left-to-right
![Page 13: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/16.jpg)
Python Basics
● Get the second word 'world' by slicing>>> “hello, world”[x:y]
![Page 17: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/22.jpg)
Python Basics
Mutability● Mutable – can change
– Lists, dictionary
● Immutable – cannot change– Strings, tuples
● Try set, del..
![Page 23: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/23.jpg)
Python Basics
Casting – numbers and strings● Strings and numbers
>>> int(“234”)
>>> str(234)
![Page 24: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/26.jpg)
Modules
● Interactive mode vs modules● Indentation
![Page 27: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/27.jpg)
Boolean Values
● True>>> 1 < 2
● False>>> 1 > 2
● Also evaluate to False:
“”, [], {}, 0
![Page 28: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/32.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/33.jpg)
Demo
● Web demo● Scientific computing
![Page 34: Python bootcamp - C4Dlab, University of Nairobi](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/34.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052310/5406e98f8d7f7288088b4890/html5/thumbnails/35.jpg)
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/