Download - Python Bootcamp Slides
-
7/30/2019 Python Bootcamp Slides
1/35
Python Bootcamp - C4Dlab
SCI labs, University of NairobiNov 24th 2013
Kenny Rachuonyo
-
7/30/2019 Python Bootcamp Slides
2/35
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++
-
7/30/2019 Python Bootcamp Slides
3/35
Features (cont.)
Embeddable games, graphics,
Extensive Libraries (batteries included) datacompression, OS, Networking, Internet,
Multimedia, Graphics
-
7/30/2019 Python Bootcamp Slides
4/35
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
-
7/30/2019 Python Bootcamp Slides
5/35
Python in the Industry
Desktop
Games Counterstrike, Civilization IV
Cinema 4D Graphics
Dropbox
GUI frameworks PyGTK, PyQT,
-
7/30/2019 Python Bootcamp Slides
6/35
Python in the Industry
Scientific Computing
NASA
Packages: Scipy, Numpy, Matplotlib,
-
7/30/2019 Python Bootcamp Slides
7/35
Python in the Industry
Mobile
Nokia Symbian Series 60
Android Scripting Layer for Android
Blackberry
Kivy cross-platform: iOS, Android, Linux,Windows, Mac
-
7/30/2019 Python Bootcamp Slides
8/35
Python Basics
The interpreter
Installation
Windows (set path)
Datatypes: int, str, float, lists, tuples,dictionaries
Basic I/O
-
7/30/2019 Python Bootcamp Slides
9/35
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
-
7/30/2019 Python Bootcamp Slides
10/35
Python Basics
Operator Operation
+ Addition
- Subtraction
/ Division
* Multiplication
** Power
% Modulus
-
7/30/2019 Python Bootcamp Slides
11/35
Python Basics
How will this be evaluated?
>>>X = 1 + 2 3 ** 4 * ( 5+6)
-
7/30/2019 Python Bootcamp Slides
12/35
Python Basics
Operator Precedence rules
Parenthesis
Power
Multiplication
Addition
Left-to-right
-
7/30/2019 Python Bootcamp Slides
13/35
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)
-
7/30/2019 Python Bootcamp Slides
14/35
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]
-
7/30/2019 Python Bootcamp Slides
15/35
Python Basics
Handy String functions
find
>>> s.find('e')
Replace
>>> n = s.replace('e', 'a' )
Make upper, lower
>>> u = s.upper()
-
7/30/2019 Python Bootcamp Slides
16/35
Python Basics
Get the second word 'world' by slicing
>>> hello, world[x:y]
-
7/30/2019 Python Bootcamp Slides
17/35
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])
-
7/30/2019 Python Bootcamp Slides
18/35
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)
-
7/30/2019 Python Bootcamp Slides
19/35
Python Basics
Append an item to the list within the list
>>> l = [1, 'a', [2, 3], 4]
>>> l = [1, 'a', [2, 3, 5], 4]
-
7/30/2019 Python Bootcamp Slides
20/35
Python Basics
Handy functions
Sum
>>> sum([2, 3, 4])
Max
>>> max([2, 3, 4])
Min
>>> min([2, 3, 4])
-
7/30/2019 Python Bootcamp Slides
21/35
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]
-
7/30/2019 Python Bootcamp Slides
22/35
Python Basics
Mutability
Mutable can change
Lists, dictionary
Immutable cannot change
Strings, tuples
Try set, del..
-
7/30/2019 Python Bootcamp Slides
23/35
Python Basics
Casting numbers and strings
Strings and numbers
>>> int(234)
>>> str(234)
-
7/30/2019 Python Bootcamp Slides
24/35
Python Basics
Importing modules
>>> import math
>>> math.sqrt(4)
>>> from math import sqrt
>>> sqrt(4)
dir() function
>>> dir(math)
-
7/30/2019 Python Bootcamp Slides
25/35
Python Basics
Basic I/O>>> name = raw_input()
>>> name = raw_input(Name: )
Input numbers:>>>age = raw_input(Age: )
>>>age = int(raw_input(Age: ))
-
7/30/2019 Python Bootcamp Slides
26/35
Modules
Interactive mode vs modules
Indentation
-
7/30/2019 Python Bootcamp Slides
27/35
Boolean Values
True
>>> 1 < 2
False
>>> 1 > 2 Also evaluate to False:
, [], {}, 0
-
7/30/2019 Python Bootcamp Slides
28/35
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)
-
7/30/2019 Python Bootcamp Slides
29/35
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)
-
7/30/2019 Python Bootcamp Slides
30/35
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 theirdifference
d = a b
return d
-
7/30/2019 Python Bootcamp Slides
31/35
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()
-
7/30/2019 Python Bootcamp Slides
32/35
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()
-
7/30/2019 Python Bootcamp Slides
33/35
Demo
Web demo
Scientific computing
-
7/30/2019 Python Bootcamp Slides
34/35
Next Steps
Intermediate topics: Classes and objects in Python
Regular Expressions
Exceptions etc
Python on Appengine
Python user group
-
7/30/2019 Python Bootcamp Slides
35/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/
http://docs.python.org/2/tutorial/http://www.swaroopch.com/notes/python/http://www.openbookproject.net/thinkcs/python/english2e/http://www.openbookproject.net/thinkcs/python/english2e/http://www.swaroopch.com/notes/python/http://docs.python.org/2/tutorial/