Thread: Starting Python
View Single Post
Join Date: Oct 2004
Posts: 3,855
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Starting Python

 
0
  #1
Mar 23rd, 2005
The idea of this thread is to help the beginning Python programmer with hints and helpful code. Please feel free to contribute! If you have any questions start your own thread!


The creators of Python are very active, improving the language all the time. Here is a little of the version history:
Python24 March 2005
Python25 Sept 2006
Python26 Sept 2008
Python31 July2009
You can download the version you like free from: www.python.org
Selfextracting MS Installer files have a .msi extension and contain the version number, for instance python-3.1.msi.
Editors note:
Python3 versions are not totally campatible with Python2 versions. A few clumsy things have been removed to modernize the language. One obvious one is the print statement, which is now a print() function. See:
http://www.daniweb.com/forums/showpo...&postcount=154

The good news is that it contains a conversion program 2to3.py that will convert your present Python2 code to Python3 code.

You can find an excellent discussion of Python2 to Python3 changes here:
http://diveintopython3.org/porting-code ... -2to3.html
Linux users can install Python installers from:
http://www.activestate.com/activepython/python3/
For the folks who like to do the unusual, Python is equally at home on the Apple computers. It is my understanding that Linux and Apple OS X have Python already installed. Apple users should check http://undefined.org/python/

I installed my Python on the D: drive, so I ended up with a D:\Python31 folder after the installation. The first step is to create a subfolder (subdirectory) for all your test programs, like D:\Python31\Atest31.

There is a Python editor included with the Python installation. The program is called IDLE, a small, but capable Integrated Development Environment (IDE), that you can use to write, edit, run and debug your code from. The problem is that it is hidden in subfolders. I found it in
D:\Python31\Lib\idlelib\idle.pyw

The .py or .pyw extension is used for Python code files. With Windows, all you need to do is to double click on idle.pyw and after a fraction of a second the compiled program appears on your screen.

If your version of IDLE starts up in the Shell Window, the one with the >>> prompts, go to 'Options' on the top line and then click on 'Configure IDLE'. In the configure menu go to the 'General' page and set the 'Open Edit Window' at startup option, then click on 'Apply'.

I think that coding in the interactive shell, also called the command line option, is limited to one liners, simple calculations and calls for help. Anything else is just major confusion, and has turned off a lot of fine folks from using Python! Lamentably, much of the Python docs are littered with command line examples with its many >>> prompts and ... line indent markers. Do not use those >>> prompts and markers in your code editor.

Once you are in the Edit Window with its simple flashing cursor, type in your first Python program (no, the line number is not part of the code). Press (Toggle Plain Text), if you want to copy and paste into your editor.
  1. print( "Hello Monty Python!" )
On the top menu bar click on File and save the short program in the Atest folder as HelloMP1.py now press the F5 key (or click on Run then on Run Module). The Python interactive Shell appears telling what version of Python you are running, and wedged between >>> prompts is Hello Monty Python!

Not much, but it worked, your first Python program. After you admired this for a while, press the Alt and F4 keys at the same time (or File then Close) to leave the Shell and get back to the Editor.

Let's create a second version of the simple program. Replace 'print' with 'str1 =' and add a few more lines ...
  1. str1 = "Hello Monty Python!"
  2. # this is a comment
  3. # notice you don't have to declare the variable type
  4. print( str1 )
If you want to, you can save this under a different filename, then press F5. The result looks the same, but we introduced a string variable and the fact that Python does not require us to declare the type. Oh gee! My C brain is going nuts!

One more change for the fun of it ...
  1. str1 = "Hello Monty Python!"
  2. # let's replace the M with Spam
  3. str2 = str1.replace('M', 'Spam')
  4. # one more Spam for the P
  5. str3 = str2.replace('P', 'Spam')
  6. # now look at the result
  7. print( str1 )
  8. print( str2 )
  9. print( str3 )
Notice, as you type 'str1.replace(' a hint line pops up showing you what can be entered between the function's parentheses, a cool thing. When you are done typing the code, press F5 and enjoy your labor. I leave it to you to dream up better stuff! Also notice, that in all of this the original string str1 has not changed.

Want more? When you are in the IDLE editor simply press the F1 key and a copy of the Python documentation comes up. There you can click on Tutorial and now you have a ton of sample code to play with. Just cut and paste and experiment.

If you are in the Python interactive Shell, the one with the >>> prompt, you can just type help('string') to get a list of string functions and constants. A little cryptic at first, but still useful. In the Shell you can also do calculator stuff and get the result. For instance type 12345679 * 63 then press enter. By the way, you can also get into a somewhat uglier looking Shell when you simply run Python.exe from the Python31 folder.

For convenience sake make a shortcut of idle.pyw and drag it onto your desktop.

Just a note, I said earlier that with Python you don't have to declare the type of a variable. That is not totally true, you have to give Python an idea what it is. By inference Python figures out that if you type z = 77 then z is the reference to an integer object. Conversely z = "Hello" would be a string, z = 7.12 is a floating point number, and z = [] is a list (in this case an empty list ready to be populated).


There are a fair number of free third party modules (some of them still limited to Python2 versions), for instance ...

The wxPython GUI toolkit is available at:
http://www.wxpython.org/download.php

The PyQT GUI toolkit is available for Python 3.1 already:
http://www.riverbankcomputing.com

The Python Image Library (PIL) is at:
http://www.pythonware.com/products/pil/index.htm

The PyGame module for game programming is at:
http://www.pygame.org


A detailed and interesting essay about Python's creation and architecture:
http://stride-online.com/filestorage/Python.pdf

Editor's note:
I have started to update some of the posts to reflect changes in Python3. Also changed some of the code to make it work with Python2 and Python3 versions. This is work in progress, tough for my old tired eyes.
Last edited by vegaseat; Aug 23rd, 2009 at 11:44 am. Reason: Python31 updates
May 'the Google' be with you!
Reply With Quote