“application logic” • Web application server: • Handling a web request • Authenticating a user • Processing the data submitted by the user • Generating an appropriate response • Delivering the response to the user’s web browser • Handling any errors • This is the standard “Request/Response cycle” of a web application • Every web application follows this cycle! 4
be written in many languages, each with its own set of features • JavaScript (sometimes abbreviated JS) is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles.1 • Languages can be compiled or interpreted • JavaScript, Python, SQL are interpreted (dynamic) • C, Java are compiled • Languages have Syntax, the form of the language • Languages are strongly or weakly typed • Languages are formalized, and have different performance and usage characteristics, and thus each is better/worse at different jobs 1. http://en.wikipedia.org/wiki/Javascript 5
interpreted language that emphasizes code readability • Object oriented (not required, though) • Dynamically typed • Imperative (with a little bit of functional programming style) • Created by Guido van Rossum in 1989 • SQL • Function specific interpreted language that is built for data management in Relational Database Management Systems • Not object oriented • Statically typed • Functional programming style • Created by Don Chamberlin and Ray Boyce in 1974 6
= [] for d in data: print 'item: {0}'.format(d) if d > 0: result.append(d * 2) print 'Done' return result >>> r = process_data([0, 1, 2, 3]) Starting to process item: 0 item: 1 item: 2 item: 3 Done >>> print r [2, 4, 6]
• Similar to JavaScript, but: • No braces • No semicolons • Everything is an object (including methods; more later) • Spaces (indentation) and newlines matter! • Easy output to screen using print command (but be careful, this won’t show up in a browser; more on this later) • Comments are prefixed by a hash (#) or triple quotes (""") • Single and double quotes can be used interchangeably • Interpreted line by line • Programs almost always split functionality into different files, which themselves define scope • http://cottagelabs.com/news/python-language-syntax-cheat-sheet
data types • String (str, unicode) • Numeric (int, float) • Date • Collection (tuple, list, set, dict) • Types are assigned based on the data representation • '123' vs. 123 vs. 1.23 • Data is usually assigned to a named variable • name = 'John Doe' • Variables are scoped, but don’t need a declaration (so no “var”) • Python is dynamically typed, which means that a variable can be reassigned different data types • name = 'John Doe' • name = 123
using type() method • String • Defined by a quote character • Represent textual values • Numeric • Defined by numbers • int - integer • float - floating point • autoconvert up (int -> float) • Bool • Defined by True/False • Date • Provided by a module (datetime) • datetime.datetime(2012, 1, 30, 20, 15, 59) 11
number of primitive types (mix and match) • tuple: () • Immutable ordered listing of data • Duplicates allowed • list: [] • Mutable ordered listing of data • Duplicates allowed • set: set() • Mutable unordered listing of data • Duplicates not allowed • dict: {key: value} • Mutable key and value storage • Duplicates allowed for values 12
flowing mostly based upon tests and iterations • if/elif/else • Tests if an expression evaluates to True and if so, executes code • while • Executes code while <expr> is True 14 if <expr>: # do something elif <expr>: # do something else else: # otherwise do this while <expr>: # do something # set <expr> to False
collection, assigning current item to named variable in iteration context, executing code • try/except/finally • Executes code until Exception is raised, optionally catching specified exception and processing it 15 for i in [1, 2, 3,]: print i try: 'a' + 1 except TypeError, e: # handle exception TypeError
sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, ..., in which each item is formed by adding the previous two.1 • Uses recursion! 16 def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) fib(10) => 55 1. http://en.literateprograms.org/Fibonacci_numbers_(Python)
scoped, named collection of program logic • Defined by keyword def • Can be defined anywhere • Are callable from anywhere, as long as they are imported into the current context or are locally defined • A module is a collection of collection of variables and methods defined in a file • Each module has its own scope, and defined variables and methods are defined within the module’s scope • Python modules end in .py • A module can be imported into another module, bringing the imported modules variables and methods into the importing module’s scope • Modules can be nested hierarchically • Must define an __init__.py module in each folder • Nested modules are often times called libraries 17
Exercise 0 of “Learn Python the Hard Way” • This is your best resource about how to write python code! • http://learnpythonthehardway.org/book/ex0.html • Homework: • Read more from “Learn Python the Hard Way” • Email me ([email protected]) so I have your email address and can send you an account on the dev server (for use next time) 19