Python Intro
Python Intro
Agenda
• Introduction
• Running Python
• Python Programming
– Data types
– Control flows
– Classes, functions, modules
• Hands-on Exercises
Course Goals
• To understand the basic structure and syntax
of Python programming language
• To learn how to run Python scripts on our
research computing facility, the Emerald Linux
cluster
• To write your own simple Python scripts.
• To serve as the starting point for more
advanced training on Python coding
What is python?
• Object oriented language
• Interpreted language
• Supports dynamic data type
• Independent from platforms
• Focused on development time
• Simple and easy grammar
• High-level internal object data types
• Automatic memory management
• It’s free (open source)!
Timeline
• Python born, name picked - Dec 1989
– By Guido van Rossum, now at GOOGLE
• First public release (USENET) - Feb 1991
• python.org website - 1996 or 1997
• 2.0 released - 2000
• Python Software Foundation - 2001
• …
• 2.4 released - 2004
• 2.5 released – 2006
• Current version: 2.6.x
Language properties
• Everything is an object
• Modules, classes, functions
• Exception handling
• Dynamic typing, polymorphism
• Static scoping
• Operator overloading
• Indentation for block structure
High-level data types
• Numbers: int, long, float, complex
• Strings: immutable
• Lists and dictionaries: containers
• Other types for e.g. binary data, regular
expressions, introspection
• Extension modules can define new “built-in”
data types
Why learn python?
• Fun-to-use "Scripting language"
• Object-oriented
– Highly educational
• Very easy to learn
• Powerful, scalable, easy to maintain
– high productivity
– Lots of libraries
• Glue language
– Interactive front-end for FORTRAN/C/C++ code
Why learn python? (cont.)
• Reduce development time
• Reduce code length
• Easy to learn and use as developers
• Easy to understand codes
• Easy to do team projects
• Easy to extend to other languages
Where to use python?
• System management (i.e., scripting)
• Graphic User Interface (GUI)
• Internet programming
• Database (DB) programming
• Text data processing
• Distributed processing
• Numerical operations
• Graphics
• And so on…
Python vs. Perl
– Easier to learn
• important for occasional users
– More readable code
• improved code maintenance
– Fewer “magical” side effects
– More “safety” guarantees
– Better Java integration
– Less Unix bias
Python vs. Java
– Code 5-10 times more concise
– Dynamic typing
– Much quicker development
• no compilation phase
• less typing
– Yes, it runs slower
• but development is so much faster!
– Similar (but more so) for C/C++
• Use Python with Java: JPython!
Agenda
• Introduction
• Running Python
• Python Programming
– Data types
– Control flows
– Classes, functions, modules
• Hands-on Exercises
Running Python Interactively
• Start python by typing "python"
– /afs/isis/pkg/isis/bin/python
• ^D (control-D) exits
• % python
• >>> ^D
• %
• Comments start with ‘#’
• >>> 2+2 #Comment on the same line as text
• 4
• >>> 7/3 #Numbers are integers by default
• 2
• >>> x = y = z = 0 #Multiple assigns at once
• >>> z
• 0
Running Python Programs
• In general
• % python ./myprogram.py
• Can also create executable scripts
– Compose the code in an editor like vi/emacs
• % vi ./myprogram.py # Python scripts with the suffix .py.
– Then you can just type the script name to execute
• % python ./myprogram.py
• The first line of the program tells the OS how to execute it:
• #! /afs/isis/pkg/isis/bin/python
– Make the file executable:
• % chmod +x ./myprogram.py
– Then you can just type the script name to execute
• % ./myprogram.py
Running Python Programs
Interactively
Suppose the file script.py contains the following lines:
print 'Hello world'
x = [0,1,2]
Let's run this script in each of the ways described on the last slide:
• python -i script.py
Hello world
>>> x
[0,1,2]
• $ python
>>> execfile('script.py')
>>> x
[0,1,2]
Running Python Programs
Interactively
Suppose the file script.py contains the following lines:
print 'Hello world'
x = [0,1,2]
Let's run this script in each of the ways described on the last slide:
• python
>>> import script # DO NOT add the .py suffix. Script is a module here
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
>>> script.x # to make use of x, we need to let Python know which
#module it came from, i.e. give Python its context
[0,1,2]
Running Python Programs
Interactively
# Pretend that script.py contains multiple stored quantities. To promote x(and only x) to
the top level context, type the following:
• $ python
>>> from script import x
Hello world
>>> x
[0,1,2]
>>>
# To promote all quantities in script.py to the top level context, type
from script import * into the interpreter. Of course, if that's what you
want, you might as well type python -i script.py into the terminal.
>>> from script import *
File naming conventions
• python files usually end with the suffix .py
• but executable files usually don’t have the .py
extension
• modules (later) should always have the .py extension
Comments
• Numbers
– Integer, floating-point, complex!
• Strings
– characters are strings of length 1
• + - * / % (like C)
• += -= etc. (no ++ or --)
• Assignment using =
– but semantics are different!
a = 1
a = "foo" # OK
• Can also use + to concatenate strings
Strings
• "hello"+"world" "helloworld" # concatenation
• "hello"*3 "hellohellohello" # repetition
• "hello"[0] "h" # indexing
• "hello"[-1] "o" # (from end)
• "hello"[1:4] "ell" # slicing
• len("hello") 5 # size
• "hello" < "jello" 1 # comparison
• "e" in "hello" 1 # search
• New line: "escapes: \n "
• Line continuation: triple quotes ’’’
• Quotes: ‘single quotes’, "raw strings"
Simple Data Types
– Triple quotes useful for multi-line strings
>>> s = """ a long
... string with "quotes" or
anything else"""
>>> s
' a long\012string with "quotes"
or anything else'
>>> len(s)
45
Methods in string
f = file("foo", "r")
line = f.readline()
print line,
f.close()
# Can use sys.stdin as input;
# Can use sys.stdout as output.
Files: Input
input = open(‘data’, ‘r’) Open the file for input
class Thingy:
"""This class stores an arbitrary object."""
def __init__(self, value):
constructor
"""Initialize a Thingy."""
self.value = value
def showme(self): method
Notes:
• blocks delimited by indentation!
• colon (:) used at end of lines containing control flow keywords
Control flow (3)
• while loops
a = 10
while a > 0:
print a
a -= 1
Control flow (4)
• for loops
for a in range(10):
print a
a = [3, 1, 4, 1, 5, 9]
for i in range(len(a)):
print a[i]
Control flow (6)
def foo(x):
y = 10 * x + 2
return y
• All variables are local unless
specified as global
• Arguments passed by value
Executing functions
def foo(x):
y = 10 * x + 2
return y
• import module
• module.function()
• Modules are namespaces
– Can be used to organize variable names, i.e.
• atom.position = atom.position - molecule.position
Modules
• Also note that the FFTW package ("fastest Fourier transform in the West")
has a python wrapper. See notes at the end
• Python Standard Libraries/Modules:
– http://docs.python.org/library/
– http://its2.unc.edu/dci/dci_components/shared_apps/packages/python
_packages.html
– http://pypi.python.org/pypi/
Command-line arguments
import sys
print len(sys.argv) # NOT argc
# Print all arguments:
print sys.argv
# Print all arguments but the program
# or module name:
print sys.argv[1:] # "array slice"
Catching Exceptions
#python code a.py
x = 0
try:
print 1/x
except ZeroDivisionError, message:
print "Can’t divide by zero:"
print message
>>>python a.py
Can't divide by zero:
integer division or modulo by zero
Try-Finally: Cleanup
f = open(file)
try:
process_file(f)
finally:
f.close() # always executed
print "OK" # executed on success only
Raising Exceptions
• raise IndexError
• raise IndexError("k out of range")
• raise IndexError, "k out of range”
• try:
something
except: # catch everything
print "Oops"
raise # reraise
Python: Pros & Cons
• Pros
– Free availability (like Perl, Python is open source).
– Stability (Python is in release 2.6 at this point and, as I noted earlier, is older
than Java).
– Very easy to learn and use
– Good support for objects, modules, and other reusability mechanisms.
– Easy integration with and extensibility using C and Java.
• Cons
– Smaller pool of Python developers compared to other languages, such as Java
– Lack of true multiprocessor support
– Absence of a commercial support point, even for an Open Source project
(though this situation is changing)
– Software performance slow, not suitable for high performance applications
References
– Python Homepage
• http://www.python.org
– Python Tutorial
• http://docs.python.org/tutorial/
– Python Documentation
• http://www.python.org/doc
– Python Library References
• http://docs.python.org/release/2.5.2/lib/lib.html
– Python Add-on Packages:
• http://pypi.python.org/pypi