8 Reasons Why Python Rocks November 27th, 2006

First Things First


8 Reasons to Use Python

  1. Lists, Tuples, and Dictionaries
  2. Intuitive Looping Techniques
  3. Text Processing and String Operations
  4. Exception Handling
  5. Sockets
  6. Tons of Standard Libraries
  7. Working with Files
  8. Simple HTTP/Web Services


1. Lists, Tuples, Dictionaries

  • Lists – basically arrays (actually linked lists), can contain multiple data types, accessible by index, can be used as a stack or queue, can be sliced/concatenated
  • append, insert, remove, pop, count, sort, reverse
  • Tuple – statically defined, can be accessed by index but cannot be modified after instantiation, can be used as dictionary keys
  • Dictionary – accessed by key, value pairs, constant lookup time, unordered in memory
  • keys, items, del


2. Intuitive Looping Techniques

  • for i in range(10):
  • for name in [‘karl’, ‘lenny’, ‘barney’, ‘moe’]:
  • for i, j in enumerate(myList):
    print i, j
  • for k, v in myDictionary.iteritems():
    print k + “‘s value is”, v
  • questions = [‘who’, ‘what’, ‘when’, ‘where’]
    answers = [‘Mr. Green’, ‘revolver’, ’11:00′, ‘Lounge’]
    for q, a in zip(questions, answers):
    print q + “:”, a


3. Text Processing and String Operations

  • Strings can be indexed like C, but substrings can also be specified with slice notation
  • s[0], s[:2], s[4:], s[-3:],…
  • Standard string library provides dozens of useful functions
  • split, join, lower, upper, replace, title, isalnum, isdigit,
    whitespace, punctuation, printable, etc., etc.
  • Very easy to implement “filter-type” programs like grep
  • input = sys.stdin.readlines()
  • makes parsing formatted text files trivial, e.g. Apache log parsing


4. Exception Handling

Very easy to catch exceptions and run separate code for error handling routines

try:
something = that_could + produce_an_exception
except:
this_code = gets_run( only_if_exception_thrown )


5. Simple Sockets

  • Very simple to implement TCP/IP sockets
  • Easy to add networking support to your Python scripts
  • Inexpensive way to communicate between programs, even locally
  • Server:
  • 1. bind
  • 2. listen
  • 3. accept
  • 4. recv/send
  • Client:
  • 1. connect
  • 2. send/recv


6. Tons of Standard Libraries

  • Python ships with over 300 standard libraries for programmers to use:
  • >>> help()
    help> modules
  • Just to name a few:
  • calendar, Cookie, cookielib, datetime, distutils, htmllib,
    HTMLParser, httplib, math, md5, optparse, os, random, re,
    smtplib, socket, string, struct, sys, time, urllib, urlparse,
    xml, xmllib


7. Working with Files

  • Reading and writing files is very simple in Python
  • open(), read(), write(), close()
  • os library provides many useful function calls for filesystem access
  • chdir, chmod, execv, getcwd, listdir, mkdir, remove, rmdir,
    rename, walk


8. Simple HTTP / Web Services

  • Almost everything provided to the programmer by httplib and other HTTP/URL libraries
  • httplib.HTTPConnection
  • request
  • getresponse
  • getheaders
  • status
  • reason
  • read
  • close


Putting it all Together

  • Easy to code ⇒ rapid prototyping of new functions/programs
  • Easy to read ⇒ whitespace dependence leads to highly readable and reusable code
  • Let’s write a couple of Python scripts
  • factorial.py
  • primefactors.py
  • feedMonitor.py

I am definitely open to discussion on this topic as it’s one that’s close to my heart. If you’re a Perl advocate or would otherwise like to make a case against the use of Python, I strongly encourage you to send me an email. I am more than happy to try to defend it 🙂

1 comment on “8 Reasons Why Python Rocks

  1. carla says:

    hi…I am glad to find your website because i really wanted to know more about python.My teacher in college gave us an assignment to code greedy and a-star algorithm in python. but unfortunately, i don’t have any knowledge about python…can you help me???

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>