The History of Python PYTHON PROGRAMMING Dr Christin Hill 7 9 November 2016 Invented by Guido vn Rossum* t the Centrum Wiskunde & Informtic in Amsterdm in the erly 1990s Nmed fter Monty Python s Flying Circus Grew in populrity throughout the 2000s, initilly s web-scripting lnguge Now used extensively in computing nd scientific circles, e.g. by Google, YouTube, CERN, NASA, Disney,... * Python s Benign Dicttor For Life (BDFL) Fetures of Python Free nd open-source Multi-pltform Interpreted High-level, expressive nd redble Object-oriented Dynmiclly-typed Strongly-typed Automtic memory-mngement 3 dys is short time! This Course The focus is on bredth, not depth The exercises ( prcticls ) re where most of the lerning hppens Some of the prcticls re tough: it isn t necessry to complete them ll...... but solutions will be provided It tkes long time to remember everything to do with progrmming lnguge; luckily, the Python documenttion is very good.
We cover: This Course The Python shell: using Python s clcultor The essentil syntx of Python progrm; loops, flow-control, functions, etc. Mjor dt structures: strings, lists, tuples, sets nd dictionries File I/O, Errors nd Exceptions, cool Python stuff Clsses nd object-oriented progrmming (briefly) Floting point rithmetic n introduction to NumPy, SciPy nd Mtplotlib Lerning Python C. Hill, Lerning Scientific Progrmming with Python, CUP (2016) M. Lutz, Lerning Python, 5th ed., O Reilly (2013) M. Pilgrim, Dive Into Python, APRESS (2004); lso vilble online for free t: http://www.diveintopython.net/ M. Dwson, Python Progrmming for the Absolute Beginner, 3rd ed., Course Technology PTR (2010) D. M. Bezley, Python Essentil Reference, Addison Wesley (2009) S. Oliveir nd D. Stewrt, Writing Scientific Softwre: A Guide to Good Style, CUP (2006) Online Python Resources The Python Shell The officil Python site: http://www.python.org/ The officil Python documenttion: http://docs.python.org/ The officil Python tutoril: http://docs.python.org/tutoril/ My own scipython website: http://scipython.com The mighty StckOverflow: http://stckoverflow.com/ (serch the rchive before posting your question!) Lern Python the hrd wy: http://lernpythonthehrdwy.org/ Lern Python the very hrd wy: http://www.pythonchllenge.com/ From terminl (e.g. tcsh on socrtes): % python Python 2.6.4 (r264:75706, Jul 20 2010, 05:25:12) [GCC 4.4.3] on sunos5 Type "help", "copyright", "credits" or "license" for more informtion. >>>
Jupyter Notebook Serch for Jupyter t the Windows Strt menu Lunches in browser menu Crete folder to store your work Select New Python Notebook to open new window with your new Jupyter Notebook cells. Shortcuts: Shift-Enter: Execute the cell, showing ny output, nd then move the cursor onto the cell below. If there is no cell below, new empty one will be creted. CTRL-Enter: Execute the cell in plce, but keep the cursor in the current cell. Useful for quick disposble commnds to check if commnd works or for retrieving directory listing. Alt-Enter: Execute the cell, showing ny output, nd then insert new cell immeditely beneth it. Bsic Arithmetic Bsic opertors: + (ddition) - (subtrction) * (multipliction) / (division) // (integer division) % (modulus, i.e. reminder) ** (exponentition) >>> 3 + 4 7 >>> 6 * 7 42 >>> 2**3 >>> 14 // 3 4 >>> 14 % 3 2 >>> 14. / 3 4.666666666666667 Bsic Arithmetic You cn use the Python shell s clcultor! Exmples: Integer division rounds down ( floor division ) Floting point numbers re indicted by deciml point The integer 3 is utomticlly converted up to flot >>> // n Bsic Arithmetic A reminder bout modulr rithmetic mens floor(/n)= -, the n lrgest integer less thn or equl to /n >>> b = % n is the reminder, b = - - n n For exmple, 7 // 3 = 2 since 7 = 2*3 + 1 7 % 3 = 1
Bsic Arithmetic Opertor Precedence Opertors with the sme precedence evlute left-to-right But n unprenthesised sequence of power opertors re evluted right-to-left (e.g. 3**2**3 = 3**, not 9**3) Use prentheses to over-ride precedence In decresing order of precendence: ** unry - (i.e. negtive numbers) *, /, //, % +, - Opertor Precedence Exmples: >>> 3 + 4 * 2 11 >>> (3 + 4) * 2 14 >>> -2**4-16 >>> (-2)**4 16 >>> 2 * 14 % 3 1 >>> 14 % 3 * 2 4 Bsic Arithmetic Exponentition hs higher precedence thn unry minus Use prentheses to rise -2 to the fourth power. Evlution is left-to-right, so this is 2 % 3...... but this is 2 * 2 >>> = >>> b = 17 >>> >>> - b -9 >>> / 4 + b 19 >>> c = b - 7 >>> c 10 Vribles Vribles re used store, lbel nd mnipulte dt They do not need to be declred before use b 17 b - 7 = 17-7 = 10 c >>> = >>> b = >>> = >>> b = >>> = 3.14 Vribles nmes b b references objects 3.14 Numbers, like nd 3.14 re immutble: the line = 3.14 cretes new object nd lbels it. Note tht b is not chnged.
>>> = >>> = * 1.2 >>> 9.6 Dynmic typing This is n ssignment: multiply by 1.2 nd store the result in, not mthemticl eqution. The multipliction chnges the type of from int to flot (it is utomticlly promoted to the more generl type needed to store the result) An exmple of duck typing: we don t need to declre the type of : if it looks like duck, nd it wlks like duck nd it qucks like duck, then it s duck A shorter wy of writing: >>> = * 1.2 is to use the idiom: >>> *= 1.2 Rules for vrible nmes Vrible Nmes Vribles re cse-sensitive ( nd A re different vribles) Cn contin ny letter, the underscore chrcter ( _ ), nd ny digit (0-9)...... but must not strt with digit Must not be reserved keyword. In Python 3: Flse ssert del for in or while brek elif from is pss with None clss else globl lmbd rise yield nd continue except if nonlocl return s def finlly import not try Style guide for vrible nmes Vrible Nmes Mke your vrible nmes meningful: re is better thn But not too long: the_re_of_the_tringle is too unwieldy Use lower-cse nmes, with words seprted by underscores: men_height not MenHeight ( CmelCse ) Don t use I (upper-cse i), l (lower cse L), or O (upper-cse o): they look too much like the digits 1 nd 0. The vribles i, j, k re usully expected to be integer counters Comprison opertors Objects (such s numbers) re compred using the opertors: ==!= > < >= <= (equls) (not equl to) (greter thn) (less thn) (greter thn or equl to) (less thn or equl to) The result of comprison is boolen object (of type bool): or Flse. For exmple, >>> 7 == Flse >>> 4 >= 3.14 Agin, 4 is promoted up to flot so the comprison is between objects of the sme type.
Logic The keywords nd, or nd not cn be used to string together comprisons. In decresing order of precedence: not nd or For exmple: >>> 7 > 4 nd -1 <= 0 >>> not 7.5 < 0.9 or 4 == 4 >>> not (7.5 < 0.9 or 4 == 4) Flse Over-ride precedence with prentheses Truth tbles: P T F not P F T P Q P nd Q T T T F T F T F F F F F P Q P or Q T T T F T T T F T F F F Logic or is inclusive