an introduction to python blake brogdon. what is python? python is an interpreted, interactive,...
TRANSCRIPT
![Page 1: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/1.jpg)
An Introduction to Python
Blake Brogdon
![Page 2: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/2.jpg)
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
![Page 3: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/3.jpg)
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)
![Page 4: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/4.jpg)
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
![Page 5: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/5.jpg)
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
![Page 6: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/6.jpg)
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
![Page 7: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/7.jpg)
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
![Page 8: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/8.jpg)
Functions No access specifier(public/private) No return type declaration No typing of arguments No brackets
![Page 9: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/9.jpg)
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
![Page 10: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/10.jpg)
Interesting Features
Optional arguments List comprehensions Generators “and”/“or” tricks Lambda functions
![Page 11: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/11.jpg)
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.
![Page 12: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/12.jpg)
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
![Page 13: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/13.jpg)
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
![Page 14: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/14.jpg)
“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.
![Page 15: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/15.jpg)
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
![Page 16: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/16.jpg)
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/
![Page 17: An Introduction to Python Blake Brogdon. What is Python? Python is an interpreted, interactive, object-oriented programming language. (from python.org)](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649ee15503460f94bf1e56/html5/thumbnails/17.jpg)
Case Study
Documentation printer for all of the functions within a module