User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,905 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,670 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:

Starting Python

Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: Starting Python

  #3  
Mar 23rd, 2005
For beginners in Python, this shows you how to define a function. Notice the format (parameters are often called arguments):
  1. def function_name(arg1, arg2, ...):
  2. statement block
  3. return arg3, arg4, ...
The statement lines that are part of the function have to be indented, since Python does not have a begin/end or {} pair for such things. The size of the indentation is a matter of preference. To me 2 spaces are more readable, the more or less official standard is 4 spaces. One word of advice, don't mix spaces and tabs for indentations! I avoid tabs.

In general, if a statement line ends with a colon (like a function define, a class, a for or while loop, or an if, elif, else), you have to indent the statement block that belongs to this line.

You must define a function before you call it. It is good practice to comment the function. It is also good practice to prefix a function with an action verb like format, get, convert, set etc.
  1. # a function to format to $ currency
  2. def formatDollar(amount):
  3. return "$%.2f" % amount
  4.  
  5. print formatDollar(123.9 * 0.07)
  6. print formatDollar(19)
Many Pythonians prefer this style of commenting functions ...
  1. def formatDollar(amount):
  2. """
  3. a function to format to $ currency
  4. (this allows for multiline comments)
  5. """
  6. return "$%.2f" % amount
  7.  
  8. print formatDollar(123.9 * 0.07)
  9. print formatDollar(19)
There is another use for the triple quoted comment or documentation string. You can access it like this ...
  1. # accessing the documentation string
  2. # (these are double underlines around doc)
  3. print formatDollar.__doc__
A more complete example ...
  1. import math
  2.  
  3. def getDistance(x1, y1, x2, y2):
  4. """
  5. getDistance(x1, y1, x2, y2)
  6. returns distance between two points using the pythagorean theorem
  7. the function parameters are the coordinates of the two points
  8. """
  9. dx = x2 - x1
  10. dy = y2 - y1
  11. return math.sqrt(dx**2 + dy**2)
  12.  
  13. print "Distance between point(1,3) and point(4,7) is", getDistance(1,3,4,7)
  14. print "Distance between point(1,3) and point(11,19) is", getDistance(1,3,11,19)
  15.  
  16. print '-'*50 # print 50 dashes, cosmetic
  17. print "The function's documentation string:"
  18. # shows comment between the triple quotes
  19. print getDistance.__doc__
Actually, if your documentation string is just a one-liner, you could enclose it in just single quotes on the same line. If you don't like the indentation to show up in the documentation string, Python relaxes the indentation rules within a multiline string, so you can use use something like this:
  1. def getDistance(x1, y1, x2, y2):
  2. """
  3. getDistance(x1, y1, x2, y2)
  4. returns distance between two points using the pythagorean theorem
  5. the function parameters are the coordinates of the two points
  6. """
  7. dx = x2 - x1
  8. dy = y2 - y1
  9. return math.sqrt(dx**2 + dy**2)

Just a note on function or variable names, avoid using Python language keywords or Python's builtin function names. For a list of Python's builtin functions (also called methods) you can use this little code:
  1. builtin_fuction_list = dir(__builtins__)
  2. print builtin_fuction_list
  3.  
  4. print "-"*70 # print a decorative line of 70 dashes
  5.  
  6. # or each function on a line using a for loop
  7. for funk in builtin_fuction_list:
  8. print funk
  9.  
  10. print "-"*70
  11.  
  12. # or each function on a line joining the list to a string
  13. print '\n'.join(builtin_fuction_list)
  14.  
  15. print "-"*70
  16.  
  17. # or, a little more advanced, combine it all and do a case insensitive sort too
  18. print '\n'.join(sorted(dir(__builtins__), key = str.lower))
Sorry, couldn't resist showing off the different ways to present the data.

To get a list of Python keywords use:
  1. from keyword import kwlist
  2.  
  3. print kwlist
One more note, don't use names of variables you are using in your program for function names. They will compete within the Python interpreter internal dictionary and may lead to errors!

Since the correct indentations are so critical in Python code, it is best to use an editor written for Python. Those editors will auto-indent properly, warn you of mixed tab/space indentations and do some other hand holding. I have experimented with IDLE, PythonWin, DrPython, pyPE, and BOA constructor. They all have certain features I like. If you fiddle, I mean experiment with the code a lot, DrPython or PyPE comes in the handiest. BOA constructor makes GUI programming with wxPython much easier!

For a listing of Integrated Development Environment (IDE) programs for Python see:
http://wiki.python.org/moin/PythonEditors

Also not strictly an IDE, this is a very nice editor (written in Python/wxPython) with great features (multiple windows, syntax highlighting, find/replace, line numbers, autocomplete, macros, fold/expand functions and classes, bookmarks, todo, Python Shell, run your code, hotkeys, spellcheck and much more). Simply extract the zip file into your Python folder. Execute the program by running 'pype.pyw'. You can run your code with 'Run current file' and it will display in a nice output window (similar to the IDLE shell)
(written for Windows, but also tested on Linux, latest version PyPE-2.8-src.zip 12/14/2006)
http://pype.sourceforge.net/index.shtml

Another very nice IDE for Python is PyScripter (still beta). This one is written with Delphi and is a standalone executable file. You can download the free setup file (for Windows only) from:
http://mmm-experts.com/Downloads.aspx?ProductId=4

This free IDE is more general, but has a truckload full of real nice programmer's features:
http://www.pspad.com/en/

Boa Constructor is an IDE with a visual frame builder/designer built in (like Delphi). It is still alive and kicking, and the latest version is nicely improved. Download the Windows version for free from:
http://sourceforge.net/project/downl...p.exe&74856058
Boa uses wxPython and works with Python2.5 and wxPython2.8 on Windows XP and Vista.
Last edited by vegaseat : Jul 7th, 2007 at 10:07 pm. Reason: IDEs
May 'the Google' be with you!
Reply With Quote  
All times are GMT -4. The time now is 10:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC