Today, Python is one of the most popular programming languages in the open source space. Look around and you will find it running everywhere, from various configuration tools to XML parsing. Python is a vast language and there are many gems to discover. Following on from the popularity of our Ten essential Python tips for beginners article, we’ve compiled a further collection of ten gems to make your Python experience that much more worthwhile…
Passing arguments to a Python script
Python lets you access whatever you have passed to a script while calling it. The ‘command line’ content is stored in the sys.argv list.
Loading modules or commands at startup
You can load predefined modules or commands at the startup of any Python script by using the environment variable $PYTHONSTARTUP. You can set environment variable $PYTHONSTARTUP to a file which contains the instructions load necessary modules or commands .
Converting a string to date object
You can use the function ‘DateTime’ to convert a string to a date object.
from DateTime import DateTime
dateobj = DateTime(string)
Converting a list to a string for display
You can convert a list to string in either of the following ways…
>>> mylist = [‘spam’, ‘ham’, ‘eggs’]
>>> print ‘, ‘.join(mylist)
spam, ham, eggs
>>> print ‘n’.join(mylist)
Tab completion in Python interpreter
You can achieve auto completion inside Python interpreter by adding the following lines to your .pythonrc file (or the file you have set Python to read on startup):
import rlcompleter, readline
This will make Python complete partially typed function, method and variable names when you press the Tab key.