This is the first part of a python introduction and overview over interesting modules, by the PhD students of Frank Jülichers group at the Max Planck Institute for the Physics of Complex Systems, Dresden, Germany. This part was created by Rabea Seyboldt, April 2016.
# my favorite program
print 'Hello world'
Hello world
Main message: python is simple (syntax, usage...)
full programming language
script-based (no compiling, but slower)
high-level, many simple solutions
dynamic development (many changes, new addons)
here: concentrate on basic syntax, specialties, not features
via
Minimal properties only...
Googleing problems almost always helps
# Beware: Division of integers in python 2
i = 3
print 1/i
print 1./i
1.//3.
0 0.333333333333
0.0
# The fix:
from __future__ import division
print 1/i
0.333333333333
Or: Use python 3 (update of all module: ongoing), Syntax-Differenz: print('Hello world')
def func1():
# need to implement this
# no brackets, indentation used for structuring
# 'pass' needed: error otherwise
pass
def func2(x):
print x
# multiple use
func2('a') # string
func2(True) # boolean
func2(2.) # float
func2([1,2]) # list
func2({'key': 2}) # dictionary
a True 2.0 [1, 2] {'key': 2}
def larger_or_smaller(a,b):
if a == b:
print 'a equals b'
elif a < b:
print 'a smaller b'
else: print 'a larger b'
larger_or_smaller(1,2)
# works for int and float mixed
larger_or_smaller(1,-1.)
larger_or_smaller(1,1.)
a smaller b a larger b a equals b
# range([start=0], stop, [step=1]) is an iterator
# includes start, exclude stop
for i in range(5):
if i == 2: continue
print i
#if i >= 3: break
0 1 3 4
# import a module
import os
# execute a bash command (make directory)
os.system('mkdir new_folder')
# Using variables:
# define integer variable
n = 1
# define string (include integer)
# other options: %s (string), %g (float notation)
name = 'new_folder_%i' %n
# make directory (and add strings)
os.system('mkdir ' + name)
256
Very useful for passing parameters
# define a dictionary
params = {'parameter_a': 1, 'doSomething': True, 'mylist':[0,1]}
# or
params2 = dict(kw1=1, kw2=2)
# change dictionary
params.update({'mylist': [0,2]})
params.update(params2)
params['kw1'] = -1
print params
{'kw1': -1, 'parameter_a': 1, 'kw2': 2, 'mylist': [0, 2], 'doSomething': True}
Use dictionarys for passing many (optional) parameters.
Similar options to read from command line (python name.py param1 param2)
# passing unspecified parameters
def func(*args, **kwargs):
res = 0
# iterate over args:
for i in args:
res *= i
# iterate over kwargs
for key in kwargs:
print key, ': ', kwargs[key]
return res
print func(1, 2, **params)
kw1 : -1 parameter_a : 1 kw2 : 2 mylist : [0, 2] doSomething : True 0
# passing optional parameters
def func(a=1, b=2, multiply=False):
if multiply:
return a*b
else: return a+b
print func()
print func(multiply=True)
3 2
# many parameters: use a dictionary
params = dict(a=43, multiply=False, b=-1)
print func(**params)
42
''' This is a docstring, and it can be made into html doc of your package '''
# this is a function
def main():
# indentation is what brackets are for other languages
print 'Hello, world!'
if __name__ == '__main__':
# this is executed if the file is used via 'python name.py'
main()
Hello, world!
try/except: check for wrong input etc
def func(x):
res = 0
try:
res = x-1
except TypeError:
print 'error!'
return res
func('e')
error!
0
Assert: find errors in the code (for testing)
import types
def func(x):
assert type(x) is types.IntType, "x is not an integer: %r" % x
res = x-1
return res
func('e')
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-17-0b9b90c35b02> in <module>() 5 res = x-1 6 return res ----> 7 func('e') <ipython-input-17-0b9b90c35b02> in func(x) 2 3 def func(x): ----> 4 assert type(x) is types.IntType, "x is not an integer: %r" % x 5 res = x-1 6 return res AssertionError: x is not an integer: 'e'
import numpy as np
# array with values from 0 to 10 (inclusive) with 50 points
x = np.linspace(0,10, num=50, endpoint=True)
# log-spacing
y = np.logspace(-1,1)
# 'init' array
z = np.zeros_like(x)
# 2d-arrays (for 3d plots etc)
X, Y = np.meshgrid(x,y)
# find the shape (all dimensions) or length (1st dimension, not only for arrays)
print X.shape, len(X)
(50, 50) 50
# print first element, last element
print x[0], x[-1]
# print slice: x[min_index : max_index_exclusive : interval]
print x[1:9:2]
0.0 10.0 [ 0.20408163 0.6122449 1.02040816 1.42857143]
import numpy as np
x = np.linspace(0,10)
np.savetxt('test_array.dat', x)
y = np.loadtxt('test_array.dat')
Most important packages for everyday use:
# import symbolic module
import sympy as sy
# define a variable, and the representation
x, f = sy.symbols('x_1 f')
# define an expression (sympy simple style)
expr = x**2 + sy.sin(x)
# symbolic derivative
dexpr = sy.diff(expr,x)
# print as latex
print sy.latex(dexpr)
f = sy.lambdify(x, dexpr, "numpy")
print dexpr.subs(x,1)
2 x_{1} + \cos{\left (x_{1} \right )} cos(1) + 2
Now let us calculate and plot values
import numpy as np
import sympy as sp
y = np.linspace(0,10)
z = f(y)
%matplotlib qt
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.plot(y,z)
plt.ylabel('Is this $x^2$?')
plt.savefig('test.pdf')
For more awesome stuff, more information, see: