What Makes Python So Popular?

21
WHAT MAKES PYTHON SO POPULAR? Jung-Hoon Rhew intel.dts.ptm.pdmg 10/7/2010 Jung-Hoon Rhew 1

description

An engaging introduction to Python programming language exploring the ultimate cause that makes it so popular.

Transcript of What Makes Python So Popular?

What makes Python so Popular?

What makes Python so Popular?Jung-Hoon Rhewintel.dts.ptm.pdmg

10/7/2010Jung-Hoon Rhew1OverviewWhat is Python? Brief HistoryWhere is Python Popular?Is Python getting Popular?What makes Python so Popular?Code ExamplesPython vs. Ruby DebateHow to StartSummary10/7/2010Jung-Hoon Rhew22I. What is Python - Brief HistoryConception: 1980Implementation: 1989Creator: Guido van Rossum @ CWI (once @Google)Heritage: SETL ABC PythonNamed from Monty Pythons Flying CircusKey FeaturesSimple and intuitive language constructMulti-paradigm (Structured, OOP, FP, AOP, )Dynamically (but strongly) typed with garbage collectionInterpreted but highly scalable and extensibleSpiritual (Zen of Python)Major Releases0.9: Feb. 20, 1991 (First)2.0: Oct. 16, 2000 (2.x branch is still active, most popular)3.0: Dec. 3, 200810/7/2010Jung-Hoon Rhew3II. Where is Python Popular?GoogleMajor contributor to Googles engineering successOne of the three official languages along with C++ & JavaGoogle App Engine provides Python and Java SDKs onlyMicrosoftIronPython (Python for .NET)FinanceProposed by U.S. SEC for use in financial modeling for transparency (e.g. Pandas library)AcademiaComputer science (Python as a First Language)Scientific computing (NumPy, SciPy, NanoHub)Social science (Python extension in SPSS)10/7/2010Jung-Hoon Rhew4III. Is Python getting Popular?

The blog offers the following reasons for Pythons high usage and growth. Adoption, support, and investment from Google and Microsoft Strong support and community Established developer base Enterprise friendly licensing Popularity for web applications with prevailing cloud computing Proposed by the SEC for use in financial modeling

However, these are just an outcome of a more fundamental cause.Lets explore!Yes: The Growth of Dynamic Languages (7/28/2010 Active Blog)10/7/2010Jung-Hoon Rhew5IV. What makes Python so Popular?Superb productivity in creating, (re)using, sharing, and maintaining knowledge across different domains.Thanks to Pythons letter and spirit that relentlessly pursue beautiful codingThe philosophy is established both in its core language construct and in its community cultureBeautiful = Explicit, Simple, Flat, Sparse, Readable as summarized in Zen of Python (PEP-20)Syntax/Semantics is kept simple and intuitive for good citizens to use easily and correctly.Culture raises good citizens by promoting good practices while discouraging bad ones. Idiomatic Python, Style guide (PEP-8)Bruce Eckel (C++ & Java Guru) explains in Why I love Python how minimizing clutter improves productivity, and the relationship between backwards compatibility and programmer pain.10/7/2010Jung-Hoon Rhew6IV.0 Read-Time ConvenienceProgrammers spend >95% of time onReading the surrounding code.Development productivity, code maintainability, effectiveness of knowledge sharing, etc. are strongly correlate to Read-Time Convenience.In this sense, Python defines what the beautiful code is.This might not be as much fun as coding in a language oriented to Write-Time Convenience.Read-time convenience requires the language to beExplicit, Simple, Flat, and Sparse because Readability counts.These characteristics may compromise convenience or fun of writing code in local scope.However, it promotes convenience or fun of writing code into a scalable, maintainable structure.

10/7/2010Jung-Hoon Rhew7IV.1. Python CultureWhile offering choices in coding, the Python philosophy rejects exuberant syntax, such as in Perl, in favor of a sparser, less-cluttered grammar. Python developers expressly promote a particular "culture", favoring language forms they see as "beautiful", "explicit", and "simple" To describe something as clever is NOT considered a compliment in the Python culture." Python's philosophy rejects the Perl "there is more than one way to do it" approach to language design in favor of "there should be oneand preferably only oneobvious way to do it." Alex MartelliPython developers eschew premature optimization, and moreover, reject patches to non-critical parts of CPython which would offer a marginal increase in speed at the cost of clarity.Python is sometimes described as "slow". However, most problems and sections of programs are not speed critical. When speed is a problem, Python programmers tend to try using a JIT compiler or rewriting the time-critical functions in C/C++.10/7/2010Jung-Hoon Rhew8IV.2. Zen of PythonAuthor: Tim PetersGuiding principles but open to interpretationPart 1Beautiful 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 aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.

10/7/2010Jung-Hoon Rhew9IV.2. Zen of Python Cont.Part 2In the face of ambiguity, refuse the temptation to guess.There should be oneand preferably only oneobvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than right now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idealet's do more of those!

10/7/2010Jung-Hoon Rhew10IV.3. Master Yodas LessonYoda: Code! Yes. A programmers strength flows from code maintainability. But beware of Perl. Terse syntax more than one way to do it default variables. The dark side of code maintainability are they. Easily they flow, quick to join you when code you write. If once you start down the dark path, forever will it dominate your destiny, consume you it will.Luke: Is Perl better than Python?Yoda: No no no. Quicker, easier, more seductive.Luke: But how will I know why Python is better than Perl?Yoda: You will know. When your code you try to read six months from now.

10/7/2010Jung-Hoon Rhew11V.1. Code Examples - Readable10/7/2010Jung-Hoon Rhew12def list_to_0_index(lst): """ # docstring start Returns a list that contains the 0-index of the first occurrence of elements in the list lst. So the list x = [0, 1, 4, 2, 4, 1, 0, 2] is transformed into y = [0, 1, 2, 3, 2, 1, 0, 3]. >>> x = [0, 1, 4, 2, 4, 1, 0, 2] # doctest >>> list_to_0_index(x) [0, 1, 2, 3, 2, 1, 0, 3] """ # docstring end return [lst.index(i) for i in lst] # list comprehension

if __name__ == "__main__": # command-line mode import doctest # import doctest module doctest.testmod() # run doc testCompare the readability of list_to_0_index to C++, Java, and Haskell counterparts athttp://rgrig.blogspot.com/2005/11/writing-readable-code.htmlThe following Easter egg displays Zen of Python in the interpreter>>> import thisThe Zen of Python, by Tim Peters

Beautiful is better than ugly.Namespaces are one honking great idea -- let's do more of those!this module creates Zen of Python by decoding ROT13 encryptedstring stored in this.s.The ROT13 mapping is stored in this.d.Below are both non-pythonic codes and a pythonic one (preferably only one obvious way).V.2. Code Examples - Pythonic

10/7/2010Jung-Hoon Rhew13# Non-pythonic code 1

result = []for c in this.s: if c in this.d: result.append(this.d[c]) else: result.append(c)print ''.join(result)# Non-pythonic code 2

print ''.join([this.d[c] if c in this.d else c for c in this.s])# Pythonic code using built-in dict.get method and generator expression

print ''.join(this.d.get(c,c) for c in this.s)13V.3. Code Examples - Namespaces10/7/2010Jung-Hoon Rhew14Zen of Python rule #19Namespaces are one honking great idea -- let's do more of those!

The following directory structure and corresponding import statements show how to create and manage modules and packages.package/ __init__.py # makes the directory recognized as a namespace module1.py # file name is a namespace subpackage/ __init__.py module2.py # module2 contains object name>>> import package.module1>>> from package.subpackage import module2 as mymodule2>>> from package.subpackage.module2 import nameSo easy even Grandma can do!VI. Python vs. Ruby DebateWhy do I bring this up?Quite often, promoting Python comes down to this debate.The debate is among daily programmers who are experienced in either language or both.Examination of the debate reveals why Python is great not just over Ruby but in general.The starting question was Should I learn Python or Ruby next? from a Perl programmer.Someone in the thread analyzed 100+ posts.50% voted for Python30% voted for Ruby20% voted for learning bothThis is not the end of the story.10/7/2010Jung-Hoon Rhew15VI. Python vs. Ruby Debate - Cont.DisclaimerNot trying to say dont learn Ruby. Learning other languages is important if you can afford and they meet your organizational need (e.g. Ruby on Rails for web development).MythsFrom Equivocal sideRuby has more powerful features but Python has more libraries.Both languages are equivalent or equally capable.From Ruby sideRuby provides powerful features Python cant or does with limitation.So, Ruby allows more creative ways of doing things and fun to use.Even some of experienced Python programmers are swayed by those myths.10/7/2010Jung-Hoon Rhew16VI. Python vs. Ruby Debate - Cont.What is Powerful?Usually a capability that enables different things, implicitly.Fits well into Perl mantra more than one way to do it.Takes a lot of time to (or learn how to) use correctly and safely.due to lack of read-time convenience.Python deliberately avoids or limits such features as stated in Python's Design Philosophy.Borrow ideas from elsewhere whenever it makes (Pythonic) sense: read-time convenience over write-time convenience.Do one thing well (The "UNIX philosophy").What is Creative?It is NOT creativity favoring personal pleasure of coding over responsibility to the public.Simplicity is the essence of creativity, especially for continuous, scalable innovation. Python design embraces simplicity over what Ruby/Perl proponents consider creative.10/7/2010Jung-Hoon Rhew17VI. Python vs. Ruby Debate - Cont.Still not convinced?Let me remind you of the evidences

Major contributor to Googles engineering success as one of their three official languages along with C++ & JavaWidely adopted in various domains; scientific computing, finance, statistics, GUI, DB, web applications, and so on.Python has a lot more (~4x) quality libraries across different domains than Ruby or any other in the list thanks to its amazing productivity. Python is different in philosophy so is the outcome!

10/7/2010Jung-Hoon Rhew18VII. How to StartGuidos Python Tutorial. (~60 pages light-weight reading)Understand Pythons design philosophy (including the Zen).Get used to Idiomatic Python.Better to follow Style guide (PEP-8).Read good books on PythonSearch web for references and video clips (Official Python docs, Google python class, PyCon talks, etc.)You can become a capable (Python) programmer much quicker.

10/7/2010Jung-Hoon Rhew19

VIII. Summary10/7/2010Jung-Hoon Rhew20

Life of three smart programmerswho disregarded PythonLife of their simple coworkerwho happened to pick up Pythonthey had kicked away

10/7/2010Jung-Hoon Rhew21