python lectures 1

Upload: palimarium

Post on 10-Mar-2016

23 views

Category:

Documents


0 download

DESCRIPTION

Python guide

TRANSCRIPT

  • Python Crash CourseIntro, scriptsBachelorsV1.0dd 09-12-2014Hour 5

  • Why Python?Modern scripting languages:Python, Perl, Ruby, IDL, Matlab, High-levelInteractive interpreterEase of useSpeed of development

    Encourages scripting, rather than one-off analysisPermanent recordRepeatability

  • Why not python?If you want fastest possible performanceHighly parallel codeNeed low-level control

  • Why Python is greatDesigned to be easy to learn and use clear syntaxWell documentedPowerful, exible, fully-featured programming languageBatteries includedComprehensive scientic toolsFastInterpreter, introspectionRuns everywhereCompletely freeYou already have it

  • Why learn Python?Less stressGet more science doneWidely used and growing popularityThroughout academia and industryNASA, AstraZeneca, Google, Industrial Light & Magic, Philips,Web services, engineering, science, air trafc control, quantitative nance, games, education, data management, Python programmers in demandEasy introduction to general programming concepts

    Why not?Existing code for your project in another language, but still

  • Python in optical astronomySTScI PyRAF (IRAF) + additional Python only routinesESO PyMIDAS (MIDAS)STScI PyFITS (access to FITS les)Astro-WISE (wideeld imaging system)Pyephem - solar system ephemerisLSST will use Python/C+

  • Python in radio astronomyCasaPy (Casa) - AIPS++, default system for EVLA and ALMA data analysis.ParselTongue - call AIPS tasks from PythonPYGILDAS (GILDAS) - IRAM data analysis software ported to PythonBoA (Bolometer Analysis Package) for LABOCA on APEX and other bolometersAPECS (APEX control software)KAT-7 CMS is in PythonPresto - pulsar search and analysis suite; most recent routines in Pytho

  • Python in physicsCERN PyROOT (research engine for high energy physics)PyMad (simulate particle accelerators)Computational physicsALPS (Algorithms andLibraries forPhysicsSimulations)

  • Introduction to language - startLinuxAt command line: python myscript.pyWith script: chmod, #!/usr/bin/env pythonAt python prompt: execle(somele.py)At ipython prompt: %run somele.py

  • Introduction to language - startWindowsFiles that have the extension.pyare known as Pythonscripts. 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 - startuppczaal2: 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 "", line 1, in NameError: name 'n' is not defined

  • Introduction to language - scripts Can write in a text editor and copy and paste into interpreterCan save and execute from command line:$ python test.pyCan 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 scriptStart an editor and type some python commands. Save your file as: myfirst.pyOn the Unix command line type: python myfirst.pyWe 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 programRun 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 scriptAdd in your script a shebang line as the first line: #!/usr/bin/env pythonRun it with: ./myfirst.py It should give you the right answer.

  • Introduction 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 - numbersInteger 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 OperatorsAssume variable a holds 10 and variable b holds 20 then:

    OperatorDescriptionExample+Addition - Adds values on either side of the operatora + b will give 30-Subtraction - Subtracts right hand operand from left hand operanda - b will give -10*Multiplication - Multiplies values on either side of the operatora * b will give 200/Division - Divides left hand operand by right hand operandb / a will give 2%Modulus - Divides left hand operand by right hand operand and returns remainderb % a will give 0**Exponent - Performs exponential (power) calculation on operatorsa**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

  • Arithmetic Operators PrecedenceHighest precedence rule to lowest precedence ruleParenthesis are always respectedExponentiation (raise to a power)Multiplication, Division, and RemainderAddition and SubtractionLeft to rightParenthesisPowerMultiplicationAdditionLeft to Right1 + 2 ** 3 / 4 * 51 + 8 / 4 * 51 + 2 * 51 + 1011

  • Numerical typesIntegers:>>> 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/2Floats:>>> 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>>> 111111111111111111111111111111111111111111>>> 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 - 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 **= 2jSome 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 keywordReserved keywords:and del from not whileas elif global or withassert else if pass yieldbreak except import printclass exec in raisecontinue nally is returndef for lambda tryNoneas with

  • Introduction to language - type>>> eee = 'hello ' + 'there

    >>> eee = eee + 1Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'int' objects

    >>> type(eee)

    >>> type('hello')

    >>> type(1)

    Python knows what type everything is Some operations are prohibitedYou cannot add 1 to a stringWe can ask Python what type something is by using the type() function.

  • Assignment OperatorsAssume variable a holds 10 and variable b holds 20 then:

    OperatorDescriptionExample=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 operandc += 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 operandc -= 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 operandc *= 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 operandc /= a is equivalent to c = c / a%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operandc %= a is equivalent to c = c % a**=Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operandc **= a is equivalent to c = c ** a//=Floor Dividion and assigns a value, Performs floor division on operators and assign value to the left operandc //= a is equivalent to c = c // a

  • 0.6 Assignment OperatorsxRight side is an expression. Once expression is evaluated, the result is placed in (assigned to) X..0.93A variable is a memory location used to store a value (0.6)x = 3.9 * x * ( 1 - x )0.60.40.6

  • 0.6 0.93Assignment OperatorsxRight 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.93A 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 environmentPYTHONSTARTUPPersonal startup file defined in startup file:setenv PYTHONSTARTUP /home/personal/mystartup.pyall code in startup file will be executed upon startPYTHONPATHtells the Python interpreter where to locate the module files you import into a programsetenv PYTHONPATH /usr/lib64/python2.7/site-packages:/home/personal/python/site-packages$ls /usr/lib64/python2.7/site-packagesabrt_exception_handler.pyabrt_exception_handler.pycabrt_exception_handler.pyoabrt.pthacutilmodule.so*audit.pyaudit.pycaudit.pyo_audit.so*auparse.so*Avogadro.so*basemap-1.0.6-py2.7.egg-info_blueman.so*Brlapi-0.5.6-py2.7.egg-infobrlapi.so*cairo/...

  • ipythonWhat is itinteractive shell for the Python programming language that offers enhanced introspection, additional shell syntax, tab completion and rich history.Whydefault interactive Python shell can sometimes feel to basicgives 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 environmentPYTHONSTARTUPPersonal startup file defined in startup file:setenv PYTHONSTARTUP /home/personal/mystartup.pyall code in startup file will be executed upon startProfile~/.ipython directory structureall scripts in .ipython/profile_default/startup are executed upon startnew profile can be created using:$ ipython profile create profile_nameand used:$ ipython --profile=profile_name

  • ipythonTAB completionespecially for attributes, is a convenient way to explore the structure of any object youre dealing withbesides Python objects and keywords, tab completion also works on file and directory namesIn [1]: from sys import stdstderr stdin stdout In [1]: from urllib2 import urlurl2pathname urlopen urlparseIn [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__

  • ipythonMagicbuilt 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 argumentswithout parentheses, quotes or even commas for convenience. Line magics take asingle % 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 namealist = %alias : Get list of aliases to 'alist'cd /usr/share : Obvious. cd - 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**100x**100 : time 'x*100' with a setup of 'x=2**100'; setup code is not counted. This is an example of a cell magic.

  • ipythonInput caching systeminput is saved and can be retrieved as variables_i, previous, _ii, next previous, _iii etc.Macrosmacros are great for executing the same code over and overassociate a name with a section of Python code so the code can be run later by referring to the nameIn [1]: a=2In [2]: b=3In [3]: c=a+bIn [4]: print c5In [5]: %macro xxx 1-2 4Macro `xxx` created. To execute, type its name (without quotes).=== Macro contents: ===a=2b=3print c

    In [6]: xxx5In [1]: a=2In [2]: b=3In [3]: c=a+bIn [4]: _iiOut[4]: u'b=3'In [5]: _ih[1]Out[5]: u'a=2In [6]: In[3]Out[6]: u'c=a+bIn [7]: print cOut[6]: 5In [8]: exec _ih[7]Out[8]: 5

  • ipythonUseful 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.

  • ipythonShell accessAny 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 bytes64 bytes from 173.194.67.104: icmp_seq=0 ttl=49 time=6.096 ms64 bytes from 173.194.67.104: icmp_seq=1 ttl=49 time=5.963 ms^C

  • ipythonAliasesAll 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]: %aliasTotal number of aliases: 12Out[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')]

  • ipythonthe four most helpful commands?Introduction and overview of IPythons features.%quickrefQuick reference.helpPythons own help system.object?Details about object, use object?? for extra details.

  • Assignment OperatorsEnd

    *****************