First Things First
- Where can I download Python?
- http://python.org – Windows, OS X, Unix (ships with almost every distro)
- Any good references?
- http://docs.python.org/tut/tut.html – tutorial by Python creator
- http://diveintopython.org – comprehensive Python reference
8 Reasons to Use Python
- Lists, Tuples, and Dictionaries
- Intuitive Looping Techniques
- Text Processing and String Operations
- Exception Handling
- Sockets
- Tons of Standard Libraries
- Working with Files
- 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
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 🙂
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???