Official website for Linux User & Developer
FOLLOW US ON:
Feb
3

Ten more essential Python tips

by Kunal Deo

Today, Python is one of the most popular programming languages in the open source space. Following on from the success of our original ‘ten essential Python tips for beginners’, we’ve compiled a further collection of ten gems to make your Python experience that much more worthwhile…

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.

import sys
print sys.argv

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.

[Example]
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…

1st method:

>>> mylist = [‘spam’, ‘ham’, ‘eggs’]
>>> print ‘, ‘.join(mylist)
spam, ham, eggs

2nd method:

>>> print ‘\n’.join(mylist)
spam
ham
eggs

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
readline.parse_and_bind(‘tab: complete’)

This will make Python complete partially typed function, method and variable names when you press the Tab key.

Pages: 1 2
Tags: , ,
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • kbird

      Pretty neat.

      How about the top ten Websites to help you learn Python?

    • Brent

      Your DateTime example is a little confusing as datetime is the built-in Python module and doesn’t allow that syntax.

      The intro for DateTime says the following:

      DateTime 2.12.0

      This package provides a DateTime data type, as known from Zope 2. Unless you need to communicate with Zope 2 APIs, you’re probably better off using Python’s bult-in datetime module.

      datetime.strptime(date_string, format) will accomplish the same thing only using the Python built-in module datetime.

    • Pingback: Another ten essential Python tips | Linux User

    • http://none Frank Townsend

      Please be careful to note both the version of
      python you are using and the reasons for not using
      later version such as 3.1.2 which is recommended
      for new projects. I am (sob,sob) trying to work cross
      platform between Windows Vista 64 bit, Mac OS X
      10.4, & various Ubuntu’s using 3.1.2. It is a real
      pain to read through a site such as PyGt only to
      realize after two days it doesn’t work with 64 bit
      python 3.1.2, only 32 bit.