Notice: Undefined index: order_next_posts in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 194

Notice: Undefined index: post_link_target in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 195

Notice: Undefined index: posts_featured_size in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 196
News

Ten more essential Python tips

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.

×