180 likes | 389 Views
An Introduction to Python. Blake Brogdon. What is Python?. Python is an interpreted, interactive, object-oriented programming language. (from python.org) Portable Dynamic (type/language features) GPL-compatible Java-like or Pythonic code. History. Written by Guido van Rossum
E N D
An Introduction to Python Blake Brogdon
What is Python? • Python is an interpreted, interactive, object-oriented programming language. (from python.org) • Portable • Dynamic (type/language features) • GPL-compatible • Java-like or Pythonic code
History • Written by Guido van Rossum • CWI (Netherlands) • 1991 • Released to USENET • 1995 • Corporation for National Research Initiatives (Reston, VA) • 2000 • BeOpen PythonLabs formed • Moved to Digital Creations (Zope Labs) • 2001 • PSF formed • Non-profit organization created specifically to own Python-related Intellectual Property (python.org)
The Interactive Environment • Unlike Java, Python can be run in an interactive shell • Statements can be evaluated on the fly • Methods do not need to be a part of a class • A module is a file that contains Python code • Function, Class or even just expressions • To use a module’s code call “import modname” • ex. import os • To access that module’s members, prepend the module’s name to the member • ex. print os.path
Basic Syntax • Indentation replaces brackets • No semicolons • Files can be called anything • Unlike Java, Python uses colons • http://www.hetland.org/python/instant-python.php • Built-ins: • True, False, None (null) • Everything is an object • Even functions and classnames
Lists • Lists • Like Java’s arrays • Ex • [1,2,3] • [“Todd”, [“Blake”, “Pat”]] (lists can be nested) • [“Blake”, 3] (can mix types in lists) • Slicing • Like indexing…but better • Leads to very un-Java-like constructs
Dictionaries • Like Java/AP’s Hashtable class • Examples • {“a”:1, “b”:2, “c”:3} • {1:2, 3:4, 5:6} • Any combination of different types/lists
Functions • No access specifier(public/private) • No return type declaration • No typing of arguments • No brackets
Classes • No access specifier (private/public) • Constructor = __init__ • All class methods take an implicit argument: self • Ex: __init__(self) • self == this • Instantiation does not require a “new” • Inheritance • class Dog(Animal): • Call to super via explicit superclass init call • Animal.__init__(self) • Instance variable • Set with self.myVar = 1 • Access with self.myVar
Interesting Features • Optional arguments • List comprehensions • Generators • “and”/“or” tricks • Lambda functions
List Comprehensions • Replace multi-line loops with a single statement • Output: [0, 2, 4, 6, 8] • range(x) returns all the numbers from 0 to x vs.
Generators • Functions that can return a value and maintain their state between calls • Yield vs. Return • Output: 0 1 2 3 4 5 6 7 8 9 10
Optional Arguments • Keyword arguments • A value is not necessary • Use value supplied at function definition • Like Java’s overloaded methods • Flexibility • Output: 0 1 2 3 4 5 6 7 8 9 10
“And”/“Or” Tricks • “Empty” values evaluate to false • ex: 0, False, None, [], () • (Most) Everything else is True • A and B • If A is True return it. • If A is False return b • A or B • If a is true, then return it. • If it isn't, then return b.
Lambda Functions • An anonymous function • So you don’t have to declare a separate, small function • Must be an expression (no ‘print’) • Can have any number of args • Output: [1, 4, 9, 16, 25] • Note: • map(f, list) applies function f to each element in list • x**2 = x*x
How to Learn More • help(m) prints help for m • M can be a function, module, class or variable • dir(m) returns a list of all the public members of a module • obj.__doc__ is the documentation of a module, function or class • Links • http://rgruet.free.fr/PQR24/PQR2.4.html • http://hetland.org/python/instant-python • http://python.org/doc/2.4/tut/tut.html • http://python.org/download/
Case Study • Documentation printer for all of the functions within a module