How do you program in Python?

mensanator at aol.com mensanator at aol.com
Sun Jul 3 15:10:08 EDT 2005



anthonyberet wrote:
> My question isn't as all-encompassing as the subject would suggest...
>
> I am almost a Python newbie, but I have discovered that I don't get
> along with IDLE, as i can't work out how to run and rerun a routine
> without undue messing about.
>
> What I would really like is something like an old-style BASIC
> interpreter, in which I could list, modify and test-run sections of
> code, to see the effects of tweaks, without having to save it each time,
> or re-typing it over and over (I haven't even worked out how to cut and
> paste effectively in the IDLE environment).

I think you're missing something about IDLE if you are re-typing.

For example:

IDLE 1.1a3
>>> for i in range(25):
	print i,

When you type the above, you get

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Now let's say you want to re-run that with 10 instead of 25.
You don't have to re-type it. Click to place your cursor anywhere
in the original block of code and hit <return>. Idle will re-type
the entire block for you:

>>> for i in range(25):
	print i,

The cursor will be at the very end of the block so you can re-run
it by hitting <enter> twice. But you'll get the same output.
Instead of hitting <enter> twice, use your cursor keys (or mouse)
to move to the 25 in the range function and edit it to 10. Once
edited, move the cursor back to end of the block and hit <enter>
twice to execute

>>> for i in range(10):
	print i,


0 1 2 3 4 5 6 7 8 9

It sounds worse than it is, just takes a little getting used to.

Once you're happy with that code fragment, edit it again and add
a definition at the start.

[1] click in the block and hit <enter>, Idle re-types the block

>>> for i in range(10):
	print i,

[2] place cursor in front of the 'for' and add a function name
    and change the 'for' to use the parameter instead of a constant

>>> def t(n): for i in range(n):
		print i,

[3] hit <enter> (here we do want to insert a line) and IDLE will
    show you this (indents adjust automatically)

>>> def t(n):
	for i in range(n):
		print i,

[4] move cursor to the end of the block (after the comma)
    and hit <enter> twice

Nothing happens. You have defined a function, but did not execute
it. To execute it, type its name with some value of n.

>>> t(8)
0 1 2 3 4 5 6 7

Now you can try it out with different values of n without having
to edit the whole block. Now if you want to change the function
definition, say we want each number output on a seperate line,
do the same thing we did before. Click anywhere in the block and
hit <enter>. The entire block is re-typed for you. Make your change
(remove the comma) and hit <enter> twice.

>>> def t(n):
	for i in range(n):
		print i

Again, the function is not executed. And you can always simply
click on the previously typed line t(8) and hit <enter> twice

>>> t(8)
0
1
2
3
4
5
6
7



>
> I see lots of alternate IDEs etc, but which would allow me the simple
> interface that I have described? - I really don't know about IDEs in
> general, and I suspect I would be out of my depth with one of those.
> 
> Thanks, and feel free to mock ;)




More information about the Python-list mailing list