Download presentation
Presentation is loading. Please wait.
Published byNeal Evans Modified over 9 years ago
1
An Introduction to Python Blake Brogdon
2
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
3
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)
4
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
5
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 http://www.hetland.org/python/instant-python.php Built-ins: –True, False, None (null) Everything is an object –Even functions and classnames
6
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
7
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
8
Functions No access specifier(public/private) No return type declaration No typing of arguments No brackets
9
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
10
Interesting Features Optional arguments List comprehensions Generators “and”/“or” tricks Lambda functions
11
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.
12
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
13
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
14
“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.
15
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
16
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.htmlhttp://rgruet.free.fr/PQR24/PQR2.4.html –http://hetland.org/python/instant-pythonhttp://hetland.org/python/instant-python –http://python.org/doc/2.4/tut/tut.htmlhttp://python.org/doc/2.4/tut/tut.html –http://python.org/download/http://python.org/download/
17
Case Study Documentation printer for all of the functions within a module
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.