Docsity

Python Programming Fundamentals: A Comprehensive Guide, Study notes of Programming Languages

Toggle sidebar
bg1
Introduction to Python
Heavily based on presentations by
Matt Huenerfauth (Penn State)
Guido van Rossum (Google)
Richard P. Muller (Caltech)
...
Monday, October 19, 2009
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57

Partial preview of the text

Download Python Programming Fundamentals: A Comprehensive Guide and more Study notes Programming Languages in PDF only on Docsity!

Introduction to Python

Heavily based on presentations by

Matt Huenerfauth (Penn State)

Guido van Rossum (Google)

Richard P. Muller (Caltech)

• Open source general-purpose language.

• Object Oriented, Procedural, Functional

• Easy to interface with C/ObjC/Java/Fortran

• Easy-ish to interface with C++ (via SWIG)

• Great interactive environment

• Downloads: http://www.python.org

• Documentation: http://www.python.org/doc/

• Free book: http://www.diveintopython.org

Python

Technical Issues

Installing & Running Python

Binaries

• Python comes pre-installed with Mac OS X and

Linux.

• Windows binaries from http://python.org/

• You might not have to do anything!

Running Programs on UNIX

% python filename.py

You could make the *.py file executable and add the

following #!/usr/bin/env python to the top to make it

runnable.

Batteries Included

• Large collection of proven modules included in the

standard distribution.

http://docs.python.org/modindex.html

matplotlib

• High quality plotting library.

• Downloads: http://matplotlib.sourceforge.net/

#!/usr/bin/env python import numpy as np import matplotlib.mlab as mlab import matplotlib.pyplot as plt mu, sigma = 100, 15 x = mu + sigma*np.random.randn(10000)

the histogram of the data

n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75)

add a 'best fit' line

y = mlab.normpdf( bins, mu, sigma) l = plt.plot(bins, y, 'r--', linewidth=1) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()

PyFITS

• FITS I/O made simple:

• Downloads: http://www.stsci.edu/resources/

software_hardware/pyfits

>>> import pyfits >>> hdulist = pyfits.open(’input.fits’) >>> hdulist.info() Filename: test1.fits No. Name Type Cards Dimensions Format 0 PRIMARY PrimaryHDU 220 () Int 1 SCI ImageHDU 61 (800, 800) Float 2 SCI ImageHDU 61 (800, 800) Float 3 SCI ImageHDU 61 (800, 800) Float 4 SCI ImageHDU 61 (800, 800) Float >>> hdulist[0].header[’targname’] ’NGC121’ >>> scidata = hdulist[1].data >>> scidata.shape (800, 800) >>> scidata.dtype.name ’float32’ >>> scidata[30:40,10:20] = scidata[1,4] = 999

Wrappers for Astronomical Packages

• CasaPy (Casa)

• PYGILDAS (GILDAS)

• ParselTongue (AIPS)

• PyRAF (IRAF)

• PyMIDAS (MIDAS)

• PyIMSL (IMSL)

Custom Distributions

• Python(x,y): http://www.pythonxy.com/

• Python(x,y) is a free scientific and engineering development

software for numerical computations, data analysis and data

visualization

• Sage: http://www.sagemath.org/

• Sage is a free open-source mathematics software system

licensed under the GPL. It combines the power of many existing

open-source packages into a common Python-based interface.

The Basics

A Code Sample

x = 34 - 23 # A comment.

y = “Hello” # Another one.

z = 3.

if z == 3.45 or y == “Hello”:

x = x + 1

y = y + “ World” # String concat.

print x

print y

Basic Datatypes

• Integers (default for numbers)

z = 5 / 2 # Answer is 2, integer division.

• Floats

x = 3.

• Strings

• Can use “” or ‘’ to specify.

“abc” ‘abc’ (Same thing.)

• Unmatched can occur within the string.

“matt’s”

• Use triple double-quotes for multi-line strings or strings than contain both ‘

and “ inside of them:

“““a‘b“c”””

Whitespace

Whitespace is meaningful in Python: especially

indentation and placement of newlines.

• Use a newline to end a line of code.

• Use \ when must go to next line prematurely.

• No braces { } to mark blocks of code in Python…

Use consistent indentation instead.

• The first line with less indentation is outside of the block.

• The first line with more indentation starts a nested block

• Often a colon appears at the start of a new block.

(E.g. for function and class definitions.)