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…

Python documentation tool
You can pop up a graphical interface for searching the Python documentation using the command:

$ pydoc -g

You will need python-tk package for this to work.

Python documentation server
You can start an HTTP server on the given port on the local machine. This will give you a nice-looking access to all Python documentation, including third-party module documentation.

$ pydoc -p <portNumber>

Python development software
There are plenty of tools to assist you with Python development. Here are a few important ones:

IDLE: The Python built-in IDE, with autocompletion, function signature popup help, and file editing.
IPython: Another enhanced Python shell with tab-completion and other features.
Eric3: A GUI Python IDE with autocompletion, class browser, built-in shell and debugger.
WingIDE: Commercial Python IDE with free licence available to open-source developers.

Executing functions at the time of Python interpreter termination
You can use ‘atexit’ module to execute functions at the time of Python interpreter termination.

[Example]
def sum():
print(4+5)
def message():
print(“Executing Now”)
import atexit
atexit.register(sum)
atexit.register(message)
Output:
Executing Now
9

Converting from integer to binary, hexadecimal and octal
Python provides easy-to-use functions – bin(), hex() and oct() – to convert from integer to binary, decimal and octal format respectively.

[Example]
>>> bin(24)
‘0b11000’
>>> hex(24)
‘0×18’
>>> oct(24)
‘030’

This article originally appeared in issue 83 of Linux User & Developer. Click here for more tutorials from the magazine.

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.

    4 Comments »

    • kbird said:

      Pretty neat.

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

    • Brent said:

      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.

    • Frank Townsend said:

      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.

    Trackbacks

    What's your opinion?

    Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

    Be nice. Keep it clean. Stay on topic. No spam.

    * Required fields