Python tips
From Tovid Wiki
This article lists some tips on learning and using Python.
Contents |
[edit] References
[edit] Python interpreter
Running Python interactively, with the interpreter, is a great way to experiment and learn. From the command-line, run:
$ python
You'll be greeted with a version number and a prompt:
Python 2.4.2 (#1, Apr 22 2006, 09:14:13) [GCC 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
You can begin an interactive help session by entering:
>>> help()
This provides access to documentation on all available modules, as well as on Python syntax and other fundamentals.
[edit] Interpreter basics
You can use the interpreter as a simple calculator:
>>> 1152 + 224 1376 >>> 720 * 480 345600
Or for manipulating text:
>>> "two cups of coffee".replace("two", "four")
'four cups of coffee'
You can assign things to variables, for later use:
>>> pal = 720 * 576 >>> ntsc = 720 * 480 >>> print "PAL has %s more pixels than NTSC" % (pal - ntsc)
Or make lists of things:
>>> groceries = ['milk', 'eggs', 'bananas'] >>> groceries ['milk', 'eggs', 'bananas'] >>> groceries.sort() >>> groceries ['bananas', 'eggs', 'milk'] >>> print "%s things on the grocery list" % len(groceries) 3 things on the grocery list
See An Informal Introduction to Python and Guide to Python introspection for more on the basics of using the interpreter.
[edit] Other tips
- Docstrings: Keep your documentation close to the source!
[edit] Software
- 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 license available to open-source developers
