Python Bootcamp Slides
Python Bootcamp Slides
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
Google Youtube, backend tasks.. Reddit news aggregation site Disqus commenting service Numerous web frameworks django, Zope, webapp2, web.py, pyramid, flask
Games Counterstrike, Civilization IV Cinema 4D Graphics Dropbox GUI frameworks PyGTK, PyQT,
Nokia Symbian Series 60 Android Scripting Layer for Android Blackberry Kivy cross-platform: iOS, Android, Linux, Windows, Mac
Python Basics
The interpreter
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
Python Basics
Python Basics
Integer division
>>> 4/2 >>> 5/2
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' )
Python Basics
Python Basics
Lists collection of values
Declaring
>>> l = list() >>> l = []
Appending
>>> l.append('an item') >>>del(l[2])
Python Basics
Lists collection of values
Getting length
>>> len(l)
Slicing
>>> l[1:4]
Python Basics
Python Basics
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
Python Basics
Casting numbers and strings
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
Boolean Values
True
>>> 1 < 2
False
>>> 1 > 2
Loops
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()
Establishing a connection
Download a webpage
Demo
Next Steps
Intermediate topics:
Resources
A byte of Python
http://www.swaroopch.com/notes/python/