An Introduction To Python Programming
An Introduction To Python Programming
An Introduction To Python Programming
Introduction to Python
Introduction
Introduction
Introduction to Python
Introduction
Introduction to Python
Introduction
Language
Syntax
Types
Conditionals and loops
Errors
Functions
Modules
Classes
NumPy
SciPy
Matplotlib/Pylab
Introduction to Python
Python
What is Python?
I
Introduction to Python
Python
The language
Features
Introduction to Python
Python
The language
Dynamic typing
Built-in tools
Library utilities
Third-party utilities
Introduction to Python
Python
Syntax
Syntax
I
Introduction to Python
Python
Syntax
Assignment
I
As in C: x += 1 is valid
Introduction to Python
Python
Syntax
Identifiers
Introduction to Python
Python
Syntax
Documentation
Introduction to Python
Python
Syntax
Special variables
I
You can add code that runs only when a module is called
directly:
if name == main : test()
Introduction to Python
Python
Types
Introduction to Python
Python
Types
numbers
Introduction to Python
Python
Types
Introduction to Python
Python
Types
Lists
Accessed by offset
Mutable sequence
Introduction to Python
Python
Types
Lists operations
empty list L = []
four items L2 = [0, 1, 2, 3]
nested L3 = [abc, [def, ghi]]
index L2[i], L3[i][j]
slice, length L2[i:j], len(L2)
concatenate, repeat L1 + L2, L2 * 3
iteration, membership for x in L2, 3 in L2
methods L2.append(4), L2.sort(), L2.index(1),
L2.reverse()
shrinking del L2[k], L2[i:j] = []
assignment L2[i] = 1, L2[i:j] = [4,5,6]
create list range(4), xrange(0, 4) # useful to loop
Introduction to Python
Python
Types
Dictionaries
Introduction to Python
Python
Types
Dictionaries operations
empty d1 = {}
two-item d2 = {spam:
nesting d3 = {food:
2, eggs:
{ham:
3}
1, egg:
2}}
Introduction to Python
Python
Types
tuples
They are like lists but immutable. Why Lists and Tuples?
Introduction to Python
Python
Types
Files
input input = open(data, r)
read all S = input.read()
read N bytes S = input.read(N)
read next S = input.readline()
read in lists L = input.readlines()
output output = open(/tmp/spam, w)
write output.write(S)
write strings output.writelines(L)
close output.close()
Introduction to Python
Python
Types
Unsupported Types
no pointer
int vs. short vs. long, there is only one integer type in Python
(its a C long)
Introduction to Python
Python
Types
L1 == L2, L1 is L2
(1, 0)
Introduction to Python
Python
Conditionals and loops
Introduction to Python
Python
Conditionals and loops
while, break
>>> while True:
>>>
line = ReadLine()
>>>
if len(line) == 0:
>>>
break
>>> def showMaxFactor(num):
>>>
cnt = num / 2
>>>
while cnt > 1:
>>>
if (num % cnt == 0):
>>>
print larg. fact. of %d is %d%(num, cnt)
>>>
break
>>>
count = cnt - 1
>>>
else:
>>>
print num, "is prime"
Introduction to Python
Python
Conditionals and loops
for
>>> for letter in hello world:
>>>
print letter
>>> for item in [12, test, 0.1+1.2J]:
>>>
print item
>>> for i in range(2,10,2):
>>>
print i
Equivalent to the C loop:
for (i = 2; i < 10; i+=2){
printf("%d\n",i);
}
Introduction to Python
Python
Conditionals and loops
pass
Introduction to Python
Python
Conditionals and loops
switch/case
There is no such statement in Python. It can be implementd
efficiently with a dictionary of functions:
>>> result = {
>>> a: lambda x: x * 5,
>>> b: lambda x: x + 7,
>>> c: lambda x: x - 2
>>>}
>>> result[b](10)
Note: anonymous function need be defined with the lambda
construct. The following functions f and g do the same thing:
>>> def f(x): return x**2
>>> g = lambda x: x**2
lambda functions can be place anywhere a function is expected
without formal definition.
Introduction to Python
Python
Errors
Introduction to Python
Python
Errors
>>> try:
>>>
f = open(blah)
>>> except IOError:
>>>
print could not open file
Introduction to Python
Python
Errors
assertion
Introduction to Python
Python
Functions
Functions
Introduction to Python
Python
Functions
function example
Introduction to Python
Python
Functions
Introduction to Python
Python
Modules
Introduction to Python
Python
Classes
Classes
>>> class Cone(WaveguideProfile):
>>>
def __init__(self,d0,de,L):
>>>
"Create a cone"
>>>
self.a0 = d0/2
>>>
self.ae = de/2
>>>
self.L = L
>>>
def __del__(self):
>>>
pass
>>>
def radius(self,z):
>>>
return self.ae + (self.a0-self.ae)*z/self.L
>>>
def radiusp(self,z):
>>>
"derivative of the radius at z"
>>>
return (self.a0-self.ae)/self.L
>>> c = Cone(0.1,0.2,1.5); c.radius(0.5)
Introduction to Python
Python
Classes
overloading
Introduction to Python
Python standard library
Introduction to Python
Python standard library
Introduction to Python
Python standard library
Introduction to Python
NumPy
sophisticated functions
Introduction to Python
NumPy
Introduction to Python
NumPy
NumPy
a = array([[1.,2.,3.],[4.,5.,6.]])
a[-1]
a[1,4]
a[1] or a[1,:]
a[0:5] or a[:5] or a[0:5,:]
a[-5:]
a[0:3][:,4:9]
a[::2,:]
a[::-1,:]
a.transpose() or a.T
a.conj().transpose() or a.conj().T
dot(a,b)
a*b
a/b
Introduction to Python
NumPy
NumPy
a**3
where(a>0.5)
a[a<0.5]=0
a[:] = 3
y = x.copy()
y = x[2,:].copy()
y = x.flatten(1)
arange(1.,11.) or r [1.:11.]
arange(10.) or r [:10.]
zeros((3,4))
zeros((3,4,5))
ones((3,4))
eye(3)
Introduction to Python
NumPy
NumPy
diag(a) or a.diagonal()
diag(a,0) or a.diagonal(0)
random.rand(3,4)
linspace(1,3,4)
mgrid[0:9.,0:6.]
tile(a, (m, n))
concatenate((a,b),1) or hstack((a,b)) or c [a,b
concatenate((a,b)) or vstack((a,b)) or r [a,b]
a.max()
a.max(0)
a.max(1)
where(a>b, a, b)
sqrt(dot(v,v)) or linalg.norm(v)
Introduction to Python
NumPy
NumPy
linalg.inv(a)
linalg.pinv(a)
linalg.solve(a,b)
Solve a.T x.T = b.T instead
(U, S, V) = linalg.svd(a)
linalg.cholesky(a)
linalg.eig(a)
(Q,R)=Sci.linalg.qr(a)
(L,U)=linalg.lu(a) or (LU,P)=linalg.lu factor(a)
Sci.linalg.cg
fft(a)
ifft(a)
sort(a) or a.sort()
a[argsort(a[:,0],i)]
Introduction to Python
SciPy
Scipy
I
Introduction to Python
SciPy
Convolution
B-splines
Filtering
Filter design
Linear Systems
LTI Reresentations
Waveforms
Window functions
Wavelets
Introduction to Python
SciPy
I
I
loadmat()
savemat()
Introduction to Python
Matplotlib/Pylab
Matplotlib/Pylab
Introduction to Python
Matplotlib/Pylab