Python intro


Published on

Yet another introduction to the Python environment for beginners.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Python intro

  1. 1. Python IntroductionEnrico Franchi (
  2. 2. We will perhaps eventually be writing only smallmodules that are identified by name as they are usedto build larger ones, so that devices like indentation,rather than delimiters, might become feasible forexpressing local structure in the source language.Modules (identified by name and composed)Indentation to express local structure2Donald E. Knuth,Structured Programming with go to Statements, 1974
  3. 3. 1989StartedImplementation1991FirstPublicVersion1994StableRelease2000Python2.02003Python2.32008Python3.0Python Timeline3
  4. 4. 1989StartedImplementation1991FirstPublicVersion1994StableRelease2000Python2.02003Python2.32008Python3.0Python Timeline31991StartedImplementation1995StableRelease2000Java1.32004Java1.52006Java1.6JAVA
  5. 5. Python 2 vs. 3In order to greatly improve the language, GvR decided that anewer major version that could break compatibility was neededPython 3 was released in 2008 with the idea that the transitionwould last at least 7 years (i.e., > 2015)Now the “standard” version of Python is Python 2.7 (released in2010). The world is mostly with Python 2.7Most libraries are now Python 3 compatible and it is possible towrite code that works on both versionsIn the future, Python 3 will be the standard4
  6. 6. Why learning a new languageBecause it makes easier to perform some tasksTo land a (better) jobTo work with some other software/in some environmentTo learn a new way of thinking5
  7. 7. Language Categories6Declarative ImperativeFunctionalHaskell, OCamlErlang, Lisp, SchemeLogicProlog, Mercury, SQLImperativePascal, CObject OrientedJava, Ruby, C++,Python, Smalltalk
  8. 8. Hybrid Languages7C++ TemplatesC++Object OrientedPartC++FunctionsDeclarative ImperativeFunctional ImperativeObject Oriented
  9. 9. “Functional” C++8template <int N>struct Factorial{enum { value = N * Factorial<N - 1>::value };};template <>struct Factorial<0>{enum { value = 1 };};int main() {int num = Factorial<4>::value;return 0;}fun fact 0 = 1| fact n = n * fact (n - 1)SMLf (n) =1 n = 0n⋅ f (n −1) otherwise⎧⎨⎩
  10. 10. More “functional” C++9#include <iostream>#include <algorithm>#include <iterator>#include <vector>#include <functional>int op_increase (int i) { return ++i; }int main () {const int foo[] = {10, 20, 30, 40, 50};const size_t foo_size = sizeof(foo)/sizeof(int);std::vector<int> bar(foo_size);std::transform (foo, foo+foo_size, bar.begin(), op_increase);// bar: 11 21 31 41 51std::copy(bar.begin(), bar.end(),std::ostream_iterator<int>(std::cout, ", "));return 0;}
  11. 11. Python10PythonFunctionalImperativeObject Oriented“Functional” partsof Python comefrom the true“everything is anobject” philosophy
  12. 12. Everything is an ObjectIn Python everything is an objectValues, Instances, Functions, ... even classes11def fact(n):# I hate naive factorialsif n == 0:return 1else:return n * fact(n-1)alias_for_fact = factvalues = map(fact, [1, 2, 3, 4])print values# [1, 2, 6, 24]class Person(object):def __init__(self, name) = namecrew = map(Person, ["Malcom Reynolds", "Zoe Washburne","Kaylee Frye", "Jayne Cobb",Hoban "Wash" Washburne])
  13. 13. Dynamic Typing Static TypingPython, Ruby Haskell, SML, Java, (C++)Perl, Javascript, PHP, Bash CStrongTypingWeakTypingStrongTypingWeakTypingSMLHaskellJavaC++CPythonTyping12
  14. 14. CPythonjavacJava Source CodeJVMPython Source CodeJava Bytecode Python BytecodeExecution13
  15. 15. CPythonjavacJava Source CodeJVMPython Source CodeJava Bytecode Python BytecodeExecution13
  16. 16. Interactive Shell14A read–eval–print loop (REPL) isa simple, interactive computerprogramming environmentStarted with Lisp, most “good”languages have it (Scala,Haskell, Ruby, Perl, Scheme, ...)It is useful to developinteractively small (and not sosmall) bits of code, andespecially to do scientific/algorithmic computations
  17. 17. Why learning a new languageBecause it makes easier to perform some tasksTo land a (better) jobTo work with some other software/in some environmentTo learn a new way of thinking15
  18. 18. To learn a new way of thinking...A language that does not affect the way you think aboutprogramming, is not worth knowing.Alan Perlis, 1966 Turing Award16
  19. 19. READABILITYGvRZen of PythonPEP8Readability CountsA program is read manymore times than it iswritten.Optimizing for readabilityactually makes sense.Python is built to behighly readable (semanticwhitespace, low clutter,no boilerplate)17
  20. 20. 18>>> import thisThe Zen of Python, by Tim PetersBeautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases arent special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless youre Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, its a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- lets do more of those!
  21. 21. Why learning a new languageBecause it makes easier to perform some tasksTo land a (better) jobTo work with some other software/in some environmentTo learn a new way of thinking19
  22. 22. What helps...Dynamic, High-Level: few lines of code make “lots” of stuffOften “higher level” than design-patterns: several dp are from trivialor even more verbose/less generalAlmost no boilerplate: write what you meanHighly readable: people strive to write elegant codeSmall but very well thought set of primitive concepts(Python “time-machine”)Meta-programming: even higher level of abstraction, minor lines ofcode and/or more general solutions (e.g., ORM)20
  23. 23. ... and librariesBut mostly, a simple language encourages developers towrite libraries for various tasksLots of academics and companies wrote libraries foralmost every conceivable thingPyPI (31113 libraries alone, plus all the project not indexedbut in GitHub, Bitbucket, Google Code or Sourceforge)More on that later...21
  24. 24. Why learning a new languageBecause it makes easier to perform some tasksTo land a (better) jobTo work with some other software/in some environmentTo learn a new way of thinking22
  25. 25. Python CommunityPycon US,, Python Italia A.P.S.Widely used in the open source community: several keyprojects are written in Python, and it is a defaultcomponent for several Linux distributionsFriendly and supportive community (very good socialcapital), some “huge” names are Italians (and can help)23
  26. 26. 24
  27. 27. Python JobsLearning new (good) languages is always good, even if youare not going to use them (remember Perlis)Python Paradox (see Graham, )Several companies use PythonAnd often they are the “right” companies to work withJob demand higher than job offer25
  28. 28. Companies26ITA SoftwareHeroku, eBay, Microsoft,Canonical, Spotify,RedHat, Amazon, Netflix,Adroll, Rackspace, Box,Dropbox, Facebook,Guidebook, Oracle,Reddit, ....
  29. 29. Why learning a new languageBecause it makes easier to perform some tasksTo land a (better) jobTo work with some other software/in someenvironmentTo learn a new way of thinking27
  30. 30. Scripting 3rd Party28SIMULIA Abaqus FEABlender, Cinema 4D,Lightwave, Houdini, Maya,modo, MotionBuilder,Softimage, Nuke, GIMP,Inkscape, Scribus and PaintShop Pro, GNU Debugger,ArcGIS, QGISUNIX
  31. 31. Python ConcurrencyThreads (bad solution, as always)Twisted (asynchronous programming)gevent (“light” threading for massive concurrency)multiprocess (yeah, it’s UNIX!)MPI, CUDA, etc...29
  32. 32. Python and the WebFull-stack frameworks (Django, Web2Py, Pylons, ...)MVC, Component BasedMulti-DB ORM – URL Dispatcher – Templating SystemLight-weight frameworks (Flask)WSGI (simple web-server interface language)30
  33. 33. DjangoOne of the most widely usedplatform in the worldPintertest, Instagram,Washington Post,ORM, URL Dispatcher, WebTemplating SystemAutomatic “Admin” generationfrom the ModelAutomatic translation from webforms to entitiesBuilt-in internationalizationBuil-in caching systemExtensible (built-in) authenticationsystemAuto RSS & Atom generationBuilt-in commenting systemSemi-auto Google SitemapGenerationMitigation system for severalsecurity issues (SQL Injection,Cross-site scripting, ...)31
  34. 34. Django Example32from django.db import modelsclass Poll(models.Model):question = models.CharField(max_length=200)pub_date = models.DateTimeField(date published)class Choice(models.Model):poll = models.ForeignKey(Poll)choice_text = models.CharField(max_length=200)votes = models.IntegerField(default=0)>>> Poll.objects.all()[<Poll: Whats up?>]# Django provides a rich database lookup API thats entirely driven by# keyword arguments.>>> Poll.objects.filter(id=1)[<Poll: Whats up?>]>>> Poll.objects.filter(question__startswith=What)[<Poll: Whats up?>]
  35. 35. Django Admin33
  36. 36. Flask Hello, World!34from flask import Flaskapp = Flask(__name__)@app.route("/")def hello():return "Hello World!"if __name__ == "__main__" minimal web frameworkMinimal memory requirementsAlso comes with built-in serverCompact and easy to writeE.g., web interface to a Raspberry PI
  37. 37. HIS EX,T E M P OR NUMERIC PYTHON• Numpy (low-level numerical computations) +Scipy (lots of additional packages)• Pandas (R-like)• IPython (wonderfull command line interpreter) +IPython Notebook (“Mathematica-like” interactive documents)• HDF5 (PyTables, H5Py), Databases• Specific libraries for machine learning, etc.• General Purpose Object Oriented Programming
  38. 38. TOOLSCUS E D
  39. 39. TOOLSCUS E D
  40. 40. TOOLSCUS E D
  41. 41. DENIQUEG U B E RG R E NOur CodeNumpyAtlas/MKLImprovementsImprovementsAlgorithms arefast because ofhighly optimizedC/Fortran code4 30 LOAD_GLOBAL 1 (dot)33 LOAD_FAST 0 (a)36 LOAD_FAST 1 (b)39 CALL_FUNCTION 242 STORE_FAST 2 (c)NUMPY STACKc = a · b
  42. 42. ndarrayndarrayMemorybehaviorshape, stride, flags(i0, . . . , in 1) ! IShape: (d0, …, dn-1)4x3An n-dimensional array references some(usually contiguous memory area)An n-dimensional array hasproperty such as its shape or thedata-type of the elements containesIs an object, so there is somebehavior, e.g., the def. of__add__ and similar stuffN-dimensional arrays are homogeneous
  43. 43. (i0, . . . , in 1) ! IC-contiguousF-contiguousShape: (d0, …, dn)IC =n 1Xk=0ikn 1Yj=k+1djIF =n 1Xk=0ikk 1Yj=0djShape: (d0, …, dk ,…, dn-1)Shape: (d0, …, dk ,…, dn-1)IC = i0 · d0 + i14x3IF = i0 + i1 · d1ElementLayoutinMemory
  44. 44. StrideC-contiguous F-contiguoussF (k) =k 1Yj=0djIF =nXk=0ik · sF (k)sC(k) =n 1Yj=k+1djIC =n 1Xk=0ik · sC(k)StrideC-contiguousF-contiguousC-contiguous(s0 = d0, s1 = 1) (s0 = 1, s1 = d1)IC =n 1Xk=0ikn 1Yj=k+1dj IF =n 1Xk=0ikk 1Yj=0dj
  45. 45. ndarrayMemorybehaviorshape, stride, flagsndarraybehaviorshape, stride, flagsView ViewView ViewViews
  46. 46. C-contiguousndarraybehavior(1,4)Memory
  47. 47. C-contiguousndarraybehavior(1,4)Memory
  48. 48. ndarrayMemorybehaviorshape, stride, flagsmatrixMemorybehaviorshape, stride, flagsndarraymatrix
  49. 49. BasicIndexing
  50. 50. AdvancedIndexingBroadcasting!
  51. 51. AdvancedIndexingBroadcasting!
  52. 52. AdvancedIndexingBroadcasting!
  53. 53. AdvancedIndexingBroadcasting!
  54. 54. AdvancedIndexing2
  55. 55. AdvancedIndexing2
  56. 56. AdvancedIndexing2
  57. 57. AdvancedIndexing2
  58. 58. AdvancedIndexing2
  59. 59. AdvancedIndexing2
  60. 60. Vectorize!Don’t use explicit for loops unless you have to!