Академический Документы
Профессиональный Документы
Культура Документы
Intro, scripts
Bachelors
V1.0
dd 09-12-2014
Hour 5
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
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 trafc control, quantitative
nance, games, education, data management,
Why not?
Existing code for your project in another language, but
still
CasaPy (Casa) - AIPS++, default system for EVLA and ALMA data
analysis.
Python in physics
CERN
PyROOT (research engine for high energy physics)
PyMad (simulate particle accelerators)
Computational physics
ALPS (Algorithms and Libraries for Physics Simulations)
Python 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.
Arithmetic Operators
Assume variable a holds 10 and variable b holds 20 then:
Operator
Description
b / a will give 2
**
//
Example
1.1 + 2 ** 3 / 4 * 5
2.1 + 8 / 4 * 5
3.1 + 2 * 5
4.1 + 10
5.11
Numerical types
Integers:
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)
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.
Complex numbers:
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
x
x
x
y
x
x
x
x
= 2 # Assign variable
# Display
+ 3 # Use variable
= x + 3 # New variable
= x + 1 # Assign new value
+= 1 # Shorthand; but no x++
= 12.3 + 98.7j # Change type
**= 2j
Some tricks:
>>> x, y = 2, 3
>>> x, y = y, x # No temporary
variables needed
>>> x = y = z = 1
>>>
>>>
>>>
>>>
>>>
xy, Xy
9x = 2
x9 = 2
_x = 2
if = 2
=
#
#
#
#
2, 3 # Case sensitive
Not allowed, must begin w. letter
ok
ok, but special
must not be keyword
Reserved keywords:
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue nally is return
def for lambda try
None
as with
Assignment Operators
Assume variable a holds 10 and variable b holds 20 then:
Operator
=
+=
-=
*=
/=
%=
**=
//=
Description
Example
Simple assignment operator, Assigns values
c = a + b will assigne
from right side operands to left side operand
value of a + b into c
Add AND assignment operator, It adds right
c += a is equivalent to c =
operand to the left operand and assign the
c+a
result to left operand
Subtract AND assignment operator, It subtracts c -= a is equivalent to c =
right operand from the left operand and assign c - a
the result to left operand
Multiply AND assignment operator, It multiplies c *= a is equivalent to c =
right operand with the left operand and assign c * a
the result to left operand
Divide AND assignment operator, It divides left c /= a is equivalent to c =
operand with the right operand and assign the c / a
result to left operand
Modulus AND assignment operator, It takes
c %= a is equivalent to c
modulus using two operands and assign the
=c%a
result to left operand
Exponent AND assignment operator, Performs c **= a is equivalent to c
exponential (power) calculation on operators
= c ** a
and assign value to the left operand
Floor Dividion and assigns a value, Performs
c //= a is equivalent to c =
floor division on operators and assign value to c // a
the left operand
Assignment Operators
A variable is a memory location used to
store a value (0.6)
0.6
0.6
0.6
x = 3.9 * x * ( 1 - x )
0.4
0.93
Assignment Operators
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).
0.6
0.93
x = 3.9 * x * ( 1 - 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
Python environment
PYTHONSTARTUP
$ls /usr/lib64/python2.7/site-packages
Personal startup file defined in startup file:
abrt_exception_handler.py
abrt_exception_handler.pyc
setenv PYTHONSTARTUP /home/personal/mystartup.py
abrt_exception_handler.pyo
all code in startup file will be executed upon start
abrt.pth
acutilmodule.so*
audit.py
tells the Python interpreter where to locate the module files you import into
audit.pyc
program
audit.pyo
setenv PYTHONPATH
_audit.so*
auparse.so*
/usr/lib64/python2.7/site-packages:/home/personal/python/site-packages
Avogadro.so*
basemap-1.0.6-py2.7.egg-info
_blueman.so*
Brlapi-0.5.6-py2.7.egg-info
brlapi.so*
cairo/
...
PYTHONPATH
ipython
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 environment
.ipython/:
.ipython/profile_default:
db/
history
Personal startup file defined in startup history.sqlite
file:
profile_default/
log/
setenv PYTHONSTARTUP /home/personal/mystartup.py
pid/
.ipython/db:
all code in startup file will be executed security/
upon start
shadowhist/
startup/
shadowhist_idx
~/.ipython directory structure
.ipython/profile_default/db:
rootmodules
.ipython/db/shadowhist:
all scripts in .ipython/profile_default/startup
are executed upon start
05
new profile can be created using:
21
.ipython/profile_default/log:
2e $ ipython profile create profile_name
61and used:
.ipython/profile_default/pid:
7a $ ipython --profile=profile_name
90
.ipython/profile_default/security:
95
b4
.ipython/profile_default/startup:
f3
README
PYTHONSTARTUP
db/
Profile
ipython
TAB completion
especially for attributes, is a convenient way to explore the structure of
any object youre dealing with
besides
Python
In [4]:
x.__objects and keywords, tab completion also works on file
x.__abs__names
x.__hash__
x.__reduce__
and directory
x.__add__
x.__class__
In [1]: from sys
import std
x.__coerce__
stderr stdin x.__delattr__
stdout
x.__init__
x.__int__
x.__le__
x.__long__
x.__div__
x.__lt__
In [1]: from urllib2
import
url
x.__divmod__
x.__mod__
url2pathname x.__doc__
urlopen
urlparse
x.__mul__
x.__eq__
x.__ne__
x.__float__
x.__neg__
x.__floordiv__
x.__new__
x.__format__
x.__nonzero__
x.__ge__
x.__pos__
x.__getattribute__ x.__pow__
x.__getformat__
x.__radd__
x.__getnewargs__
x.__rdiv__
x.__gt__
x.__rdivmod__
x.__reduce_ex__
x.__repr__
x.__rfloordiv__
x.__rmod__
x.__rmul__
x.__rpow__
x.__rsub__
x.__rtruediv__
x.__setattr__
x.__setformat__
x.__sizeof__
x.__str__
x.__sub__
x.__subclasshook__
x.__truediv__
x.__trunc__
ipython
Magic
obj?,
built in commands
obj??
: Get help, or more
%quickref
?obj, ??obj).
In [57]: lsmagic
?foo.*abc*
: List names in 'foo' containing 'abc' in them.
%magic
: Information about IPython's 'magic' % functions.
Available line magics:
%alias %alias_magic %autocall %autoindent %automagic %bookmark %cd %colors %config
Magic functions are prefixed by % or %%, and typically take their arguments
%cpaste %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history
without parentheses, quotes or even commas for convenience. Line magics take a
%install_default_config %install_ext %install_profiles %killbgscripts %load %load_ext
single % and cell magics are prefixed with two %%.
%loadpy %logoff %logon %logstart %logstate %logstop %lsmagic %macro %magic
%notebook %page %paste %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd
Example magic function calls:
%pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab
%quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %run
%alias d ls -F
: 'd' is now an alias for 'ls -F'
%save %sc %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls
alias d ls -F
: Works if 'alias' not a python name
%whos %xdel %xmode
alist = %alias
: Get list of aliases to 'alist'
cd /usr/share
: Obvious. cd -<tab> to choose from visited dirs.
Available cell magics:
%cd??
: See help AND source for magic %cd
%%! %%bash %%capture %%file %%perl %%prun %%ruby %%script %%sh %%sx %%system
%timeit x=10
: time the 'x=10' statement with high precision.
%%timeit
%%timeit x=2**100
x**100
: time 'x*100' with a setup of 'x=2**100'; setup code is not
Automagic is ON, % prefix IS NOT needed for line magics.
counted. This is an example of a cell magic.
ipython
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
In [1]: a=2
In [2]: b=3
later by referring to the name
In [1]:
In [2]:
In [3]:
In [4]:
Out[4]:
In [5]:
Out[5]:
In [6]:
Out[6]:
In [7]:
Out[6]:
In [8]:
Out[8]:
a=2
b=3
c=a+b
_ii
u'b=3'
_ih[1]
u'a=2
In[3]
u'c=a+b
print c
5
exec _ih[7]
5
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
ipython
Useful help commands
%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.
ipython
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.
ipython
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
('lf', 'ls -F -o --color %l |
('lk', 'ls -F -o --color %l |
('ll', 'ls -F -o --color'),
('ls', 'ls -F --color'),
('lx', 'ls -F -o --color %l |
('mkdir', 'mkdir'),
('mv', 'mv -i'),
('rm', 'rm -i'),
('rmdir', 'rmdir')]
| grep /$'),
grep ^-'),
grep ^l'),
grep ^-..x'),
ipython
?
%quickref
help
object?
Assignment Operators
End