python crash course intro, scripts bachelors v1.0 dd 09-12-2014 hour 5
TRANSCRIPT
Python Crash CoursePython Crash CourseIntro, scriptsIntro, scripts
Bachelors
V1.0
dd 09-12-2014
Hour 5
Why Python?Why Python?
• Modern scripting languages:– Python, Perl, Ruby, IDL, Matlab, …– High-level– Interactive interpreter
• Ease of use• Speed of development
• Encourages scripting, rather than one-off analysis
• Permanent record• Repeatability
Why not python?Why not python?
• If you want fastest possible performance
• Highly parallel code
• Need low-level control
Why Python is greatWhy Python is great
• Designed to be easy to learn and use – clear syntax• Well documented• Powerful, flexible, fully-featured programming language• ‘Batteries included’• Comprehensive scientific tools• Fast• Interpreter, introspection• Runs everywhere• Completely free• You already have it
Why learn Python?Why learn Python?
• Less stress• Get more science done• Widely used and growing popularity• Throughout academia and industry
– NASA, AstraZeneca, Google, Industrial Light & Magic, Philips,…– Web services, engineering, science, air traffic control,
quantitative finance, games, education, data management, …• Python programmers in demand• Easy introduction to general programming concepts
Why not?• Existing code for your project in another language, but
still…
Python in optical astronomyPython in optical astronomy
• STScI PyRAF (IRAF) + additional Python only routines
• ESO PyMIDAS (MIDAS)
• STScI PyFITS (access to FITS files)
• Astro-WISE (widefield imaging system)
• Pyephem - solar system ephemeris
• LSST will use Python/C+
• …
Python in radio astronomyPython in radio astronomy
• CasaPy (Casa) - AIPS++, default system for EVLA and ALMA data analysis.
• ParselTongue - call AIPS tasks from Python• PYGILDAS (GILDAS) - IRAM data analysis software ported to
Python• BoA (Bolometer Analysis Package) for LABOCA on APEX and other
bolometers
• APECS (APEX control software)
• KAT-7 CMS is in Python• Presto - pulsar search and analysis suite; most recent routines in
Pytho
Python in physicsPython in physics
• CERN – PyROOT (research engine for high energy physics)
– PyMad (simulate particle accelerators)
• Computational physics• ALPS (Algorithms and Libraries for Physics Simulations)
• …
Introduction to language - startIntroduction to language - start
Linux•At command line: python myscript.py•With script: chmod, #!/usr/bin/env python•At python prompt: execfile(’somefile.py’)•At ipython prompt: %run somefile.py
Introduction to language - startIntroduction to language - start
WindowsFiles that have the extension .py are known as Python scripts. In Windows and Mac OS, these files will appear to be "clickable", i.e. will appear to be files that you can open by clicking them with the mouse.
It is not recommended that you open these files by clicking on them. Why? Because quite often the result can be unpredictable. Instead, start IDLE and open Python scripts inside an IDLE session.
Introduction to language - startupIntroduction to language - startup
pczaal2: pythonPython 2.7.5 (default, Nov 3 2014, 14:26:24)[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> 2+2 4 >>> # This is a comment ... 2+2 4 >>> 2+2.0 # and a comment on the same line as code 4.0 >>> (50-5*6)/4 5 >>> width = 20 # assignment, no type declaration >>> height = 5*9 >>> width * height 900 >>> x = y = z = 0 # zero x, y and z >>> y 0 >>> n Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined
Introduction to language - scriptsIntroduction to language - scripts
• Can write in a text editor and copy and paste into interpreter
• Can save and execute from command line:$ python test.py
• Can save and use interactively in future sessions (import)
2+2
# This is a comment
2+2
2+2.0 # and a comment on the same line as code
(50-5*6)/4
width = 20 # assignment, no type declaration
height = 5*9
width * height
x = y = z = 0 # zero x, y and z
y
Python scriptPython script
– Start an editor and type some python commands. Save your file as: myfirst.py– On the Unix command line type: python myfirst.py– We want to be able to start the program by only typing its name. To make it
executable use chmod u+x myfirst.py or chmod +x myfirst.py if you want to allow everybody on the system to execute your program
– Run it with: ./myfirst.py The dot slash part is necessary to force execution if your current directory is not included in the settings for your path!
– If you type command echo $path on the command line, then a list is displayed with directories with the paths to these directories included. If you type the name of an executable file on the command line then all the directories in the list are visited to look for the wanted executable. If the name is found, it will be executed. If not, then you get a warning.
– Now we get an error message. Remember that we created a script and the first line of a script should contain the so called shebang line which defines the path to the application that has to execute the script
– Add in your script a shebang line as the first line: #!/usr/bin/env python– Run it with: ./myfirst.py It should give you the right answer.
Introduction to language - numbersIntroduction to language - numbers
>>> 10 + 3
13
>>> 10 - 3
7
>>> 10 * 3
30
>>> 10 / 3
3
>>> 10 // 3
3
>>> 10 % 3
1
>>> 10**3
1000
>>> 10 + 3 * 5 # *,/ then +,-
25
>>> (10 + 3) * 5
65
>>> -1**2 # -(1**2)
-1
>>> 10.0 + 3.0
13.0
>>> 10.0 - 3.0
7.0
>>> 10.0 * 3
30.0
>>> 10.0 / 3
3.3333333333333335
>>> 10.0 // 3
3.0
>>> 10.0 % 3.0
1.0
>>> 10.0**3
1000.0
>>> 4.2 + 3.14
7.3399999999999999
>>> 4.2 * 3.14
13.188000000000001
Introduction to language - numbersIntroduction to language - numbers
Integer division is weird!
•Integer division truncates
•Floating point division produces floating point numbers
>>> 10 / 25>>> 9 / 24>>> 99 / 1000>>> 10.0 / 2.05.0>>> 99.0 / 100.00.99
Mixing Integer and Floating
•When you perform an operation where one operand is an integer and the other operand is a floating point the result is a floating point
•The integer is converted to a floating point before the operation
>>> 99 / 1000>>> 99 / 100.00.99>>> 99.0 / 1000.99>>> 1 + 2 * 3 / 4.0 - 5-2.5
Arithmetic OperatorsArithmetic Operators
Operator Description Example
+ Addition - Adds values on either side of the operator
a + b will give 30
- Subtraction - Subtracts right hand operand from left hand operand
a - b will give -10
* Multiplication - Multiplies values on either side of the operator
a * b will give 200
/ Division - Divides left hand operand by right hand operand
b / a will give 2
% Modulus - Divides left hand operand by right hand operand and returns remainder
b % a will give 0
** Exponent - Performs exponential (power) calculation on operators
a**b will give 10 to the power 20
// Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.
9//2 is equal to 4 and 9.0//2.0 is equal to 4.0
Assume variable a holds 10 and variable b holds 20 then:
Arithmetic Operators PrecedenceArithmetic Operators Precedence
• Highest precedence rule to lowest precedence rule• Parenthesis are always respected• Exponentiation (raise to a power)• Multiplication, Division, and Remainder• Addition and Subtraction• Left to right
ParenthesisPowerMultiplicationAdditionLeft to Right
1.1 + 2 ** 3 / 4 * 52.1 + 8 / 4 * 53.1 + 2 * 54.1 + 105.11
Numerical typesNumerical types
Integers:>>> 2
>>> 0
>>> -4711
>>> 07, 022 # Octal tuple
>>> 0x9, 0xa, 0XF # Hexadecimal tuple
>>> 17 + 4 # Expression
>>> 0xa - 2
>>> 23 ** (2+3) # Power
>>> 7 / 2, 7 / -2 # Int division
>>> from __future__ import division
>>> 7/2
Floats:>>> 2.3
>>> -4.
>>> 0.1, .1
>>> 2.99E10, 6.62607e-27, -1e10
>>> 1.7 + .4
>>> 17. + 4
>>> 7./2., 7./2, 7/2.
Long integers:>>> 2**1000
>>> 2L, 3l
>>>
1111111111111111111111111111111111111
11111
>>> float(2), float(2**1000)
>>> int(2.3), int(-2.3)
>>> int(2**1000), long(2), str(2)
Complex numbers:>>> 2.+3j, 2-3J # complex literals
>>> j # will not work
>>> 1J # but this will
>>> complex(1,2)
>>> # Watch operator precedence:
>>> 1+1j*2, (1+1j)*2
>>> (2.+3j).real, (2+3j).imag
>>> type(2-3j)
Introduction to language - variablesIntroduction to language - variables
>>> x = 2 # Assign variable>>> x # Display>>> x + 3 # Use variable>>> y = x + 3 # New variable>>> x = x + 1 # Assign new value>>> x += 1 # Shorthand; but no x++>>> x = 12.3 + 98.7j # Change type>>> x **= 2j
Some tricks:>>> x, y = 2, 3>>> x, y = y, x # No temporary variables needed>>> x = y = z = 1
>>> xy, Xy = 2, 3 # Case sensitive>>> 9x = 2 # Not allowed, must begin w. letter>>> x9 = 2 # ok>>> _x = 2 # ok, but special>>> if = 2 # must not be keyword
Reserved keywords:and del from not whileas elif global or withassert else if pass yieldbreak except import printclass exec in raisecontinue finally is returndef for lambda tryNoneas with
Introduction to language - typeIntroduction to language - type
>>> eee = 'hello ' + 'there‘
>>> eee = eee + 1Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: cannot concatenate 'str' and 'int' objects
>>> type(eee)<type 'str'>
>>> type('hello')<type 'str'>
>>> type(1)<type 'int'>
• Python knows what “type” everything is • Some operations are prohibited• You cannot “add 1” to a string• We can ask Python what type something is
by using the type() function.
Assignment OperatorsAssignment Operators
Operator Description Example= Simple assignment operator, Assigns values
from right side operands to left side operandc = a + b will assigne value of a + b into c
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
c += a is equivalent to c = c + a
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand
c -= a is equivalent to c = c - a
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand
c *= a is equivalent to c = c * a
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand
c /= a is equivalent to c = c / a
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand
c %= a is equivalent to c = c % a
**= Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand
c **= a is equivalent to c = c ** a
//= Floor Dividion and assigns a value, Performs floor division on operators and assign value to the left operand
c //= a is equivalent to c = c // a
Assume variable a holds 10 and variable b holds 20 then:
0.6 0.6
Assignment OperatorsAssignment Operators
x
Right side is an expression. Once expression is evaluated,
the result is placed in (assigned to) X..
0.93
A variable is a memory location used to store a value (0.6)
x = 3.9 * x * ( 1 - x )0.6
0.4
0.6
0.6 0.930.6 0.93
Assignment OperatorsAssignment Operators
x
Right side is an expression. Once expression is evaluated,
the result is placed in (assigned to) the variable on the left side
(i.e. x).
0.93
A variable is a memory location used to store a value. The value stored in a
variable can be updated by replacing the old value (0.6) with a new value (0.93).
x = 3.9 * x * ( 1 - x )
Python environmentPython environment
• PYTHONSTARTUP– Personal startup file defined in startup file:
setenv PYTHONSTARTUP /home/personal/mystartup.py– all code in startup file will be executed upon start
• PYTHONPATH– tells the Python interpreter where to locate the module files you import into a
program
setenv PYTHONPATH
“/usr/lib64/python2.7/site-packages:/home/personal/python/site-packages”
$ls /usr/lib64/python2.7/site-packages
abrt_exception_handler.py
abrt_exception_handler.pyc
abrt_exception_handler.pyo
abrt.pth
acutilmodule.so*
audit.py
audit.pyc
audit.pyo
_audit.so*
auparse.so*
Avogadro.so*
basemap-1.0.6-py2.7.egg-info
_blueman.so*
Brlapi-0.5.6-py2.7.egg-info
brlapi.so*
cairo/
...
ipythonipython
• What is it– interactive shell for the Python programming language that offers
enhanced introspection, additional shell syntax, tab completion and rich history.
• Why– default interactive Python shell can sometimes feel to basic
– gives you all that you get in the basic interpreter but with a lot extra (line numbers, advanced editing, more functions, help functions etc)
Python 2.7.5 (default, Nov 3 2014, 14:26:24)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
From .ipython starup env
In [1]:
Python environmentPython environment
• PYTHONSTARTUP– Personal startup file defined in startup file:
setenv PYTHONSTARTUP /home/personal/mystartup.py– all code in startup file will be executed upon start
• Profile– ~/.ipython directory structure– all scripts in .ipython/profile_default/startup are executed upon start– new profile can be created using:
$ ipython profile create profile_name
– and used:$ ipython --profile=profile_name
.ipython/:
db/
history
profile_default/
.ipython/db:
shadowhist/
shadowhist_idx
.ipython/db/shadowhist:
05
21
2e
61
7a
90
95
b4
f3
.ipython/profile_default:
db/
history.sqlite
log/
pid/
security/
startup/
.ipython/profile_default/db:
rootmodules
.ipython/profile_default/log:
.ipython/profile_default/pid:
.ipython/profile_default/security:
.ipython/profile_default/startup:
README
ipythonipython
• TAB completion– especially for attributes, is a convenient way to explore the structure of
any object you’re dealing with
– besides Python objects and keywords, tab completion also works on file and directory names
In [1]: from sys import std
stderr stdin stdout
In [1]: from urllib2 import url
url2pathname urlopen urlparse
In [4]: x.__
x.__abs__ x.__hash__ x.__reduce__
x.__add__ x.__init__ x.__reduce_ex__
x.__class__ x.__int__ x.__repr__
x.__coerce__ x.__le__ x.__rfloordiv__
x.__delattr__ x.__long__ x.__rmod__
x.__div__ x.__lt__ x.__rmul__
x.__divmod__ x.__mod__ x.__rpow__
x.__doc__ x.__mul__ x.__rsub__
x.__eq__ x.__ne__ x.__rtruediv__
x.__float__ x.__neg__ x.__setattr__
x.__floordiv__ x.__new__ x.__setformat__
x.__format__ x.__nonzero__ x.__sizeof__
x.__ge__ x.__pos__ x.__str__
x.__getattribute__ x.__pow__ x.__sub__
x.__getformat__ x.__radd__ x.__subclasshook__
x.__getnewargs__ x.__rdiv__ x.__truediv__
x.__gt__ x.__rdivmod__ x.__trunc__
ipythonipython
• Magic– built in commands
– %quickrefIn [57]: lsmagic
Available line magics:
%alias %alias_magic %autocall %autoindent %automagic %bookmark %cd %colors %config
%cpaste %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history
%install_default_config %install_ext %install_profiles %killbgscripts %load %load_ext
%loadpy %logoff %logon %logstart %logstate %logstop %lsmagic %macro %magic
%notebook %page %paste %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd
%pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab
%quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %run
%save %sc %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls
%whos %xdel %xmode
Available cell magics:
%%! %%bash %%capture %%file %%perl %%prun %%ruby %%script %%sh %%sx %%system
%%timeit
Automagic is ON, % prefix IS NOT needed for line magics.
IPython -- An enhanced Interactive Python - Quick Reference Card
================================================================
obj?, obj?? : Get help, or more help for object (also works as
?obj, ??obj).
?foo.*abc* : List names in 'foo' containing 'abc' in them.
%magic : Information about IPython's 'magic' % functions.
Magic functions are prefixed by % or %%, and typically take their arguments
without parentheses, quotes or even commas for convenience. Line magics take a
single % and cell magics are prefixed with two %%.
Example magic function calls:
%alias d ls -F : 'd' is now an alias for 'ls -F'
alias d ls -F : Works if 'alias' not a python name
alist = %alias : Get list of aliases to 'alist'
cd /usr/share : Obvious. cd -<tab> to choose from visited dirs.
%cd?? : See help AND source for magic %cd
%timeit x=10 : time the 'x=10' statement with high precision.
%%timeit x=2**100
x**100 : time 'x*100' with a setup of 'x=2**100'; setup code is not
counted. This is an example of a cell magic.
ipythonipython
• Input caching system– input is saved and can be retrieved as variables– _i, previous, _ii, next previous, _iii …etc.
• Macros– macros are great for executing the same code over and over
– associate a name with a section of Python code so the code can be run later by referring to the name
In [1]: a=2
In [2]: b=3
In [3]: c=a+b
In [4]: print c
5
In [5]: %macro xxx 1-2 4
Macro `xxx` created. To execute, type its
name (without quotes).
=== Macro contents: ===
a=2
b=3
print c
In [6]: xxx
5
In [1]: a=2
In [2]: b=3
In [3]: c=a+b
In [4]: _ii
Out[4]: u'b=3'
In [5]: _ih[1]
Out[5]: u'a=2‘
In [6]: In[3]
Out[6]: u'c=a+b‘
In [7]: print c
Out[6]: 5
In [8]: exec _ih[7]
Out[8]: 5
ipythonipython
• Useful help commands– %reset resets the interactive environment – %hist allows you to see any part of your input history – %hist -g somestring – Search (‘grep’) through your history by typing
In [55]: hist -g math
19: import math
55: hist -g math
– %paste use text that you have in the clipboard, for example if you have copied code with Ctrl+C. The command cleans up certain characters and tries to find out how the code should be formatted.
– %edit The %edit command (and its alias %ed) will invoke the editor set in your environment as EDITOR.
– %who This function list objects, functions, etc. that have been added in the current namespace, as well as modules that have been imported.
In [50]: who
Interactive namespace is empty.
ipythonipython
• Shell access– Any input line beginning with a ! character is passed verbatim (minus the !) to the
underlying operating system.
– You can capture the output into a Python list, e.g.: files = !ls.
In [2]: !ping www.google.com
PING www.google.com (173.194.67.104): 56 data bytes
64 bytes from 173.194.67.104: icmp_seq=0 ttl=49 time=6.096 ms
64 bytes from 173.194.67.104: icmp_seq=1 ttl=49 time=5.963 ms
^C
ipythonipython
• Aliases– All of your $PATH has been loaded as IPython aliases, so you should be able to
type any normal system command and have it executed.
In [9]: %alias
Total number of aliases: 12
Out[9]:
[('cat', 'cat'),
('cp', 'cp -i'),
('ldir', 'ls -F -o --color %l | grep /$'),
('lf', 'ls -F -o --color %l | grep ^-'),
('lk', 'ls -F -o --color %l | grep ^l'),
('ll', 'ls -F -o --color'),
('ls', 'ls -F --color'),
('lx', 'ls -F -o --color %l | grep ^-..x'),
('mkdir', 'mkdir'),
('mv', 'mv -i'),
('rm', 'rm -i'),
('rmdir', 'rmdir')]
ipythonipython
• the four most helpful commands– ? Introduction and overview of IPython’s features.– %quickrefQuick reference.– help Python’s own help system.– object? Details about ‘object’, use ‘object??’ for extra details.
Assignment OperatorsAssignment Operators
End