[Tutor] Newbie Anxiety

Liam Clarke-Hutchinson Liam.Clarke-Hutchinson at business.govt.nz
Fri Nov 11 00:59:14 CET 2005


Hi Terry,
 
Python is great, very much so if the last you used was Basic.
 
I highly recommend Alan Gauld's tutorial, but I look forward to your queries
here. :-)

Liam Clarke-Hutchinson

-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf
Of Terry Kemmerer
Sent: Friday, 11 November 2005 12:25 p.m.
To: Python_TUTOR
Subject: [Tutor] Newbie Anxiety



(I accidentally sent this to the originator. Sorry.)

Sweet!  

Almost everything is sooo familiar, yet, merged in many interesting ways. I
never had a DO WHILE statement, but 
in many ways, your further examples are like a combination of my old FOR
NEXT loop and IF logical evaluation statement put together for reading
in/out lists. JUST TOO COOL! 

And it looks so CLEAN!  --compared to my old BASIC of having to name the
variable belonging to each NEXT incrementation executed while keeping the
code nested properly relative to each FOR NEXT loop!!!

Thanks!  THIS LOOKS GREAT!

Terry



On Fri, 2005-11-11 at 11:49 +1300, John Fouhy wrote: 

On 11/11/05, Terry Kemmerer <wildcard2005 at comcast.net
<mailto:wildcard2005 at comcast.net> > wrote:

>  I'm working on Ch. 5, "Fruitful Functions", or "How To Think Like A

> Computer Scientist" and I still can't count.

>  (Don't laugh! I can't play the violin either...)

>

>  In Basic, I would have said:

>

>  10  x = x + 1 :  print x : goto 10

>

>  run

>  1

>  2

>  3

>  etc....

>

>  How is this done in Python? (So I can stop holding my breath as I study

> this great language....and relax.)



Hi Terry,



There's a couple of options.



First, we could do it with a while loop.  This is not the best or the

most idiomatic way, but it's probably most similar to what you've seen

before.



#### count forever

i = 0

while True:

    print i

    i = i + 1

####



Of course, we generally don't want to keep counting forever.  Maybe

we'll count up to 9.



#### count to 9

i = 0

while i < 10::

    print i

    i = i + 1

####



A while loop contains an implicit "GOTO start" at the end.  At the

start, it checks the condition, and breaks out of the loop if the

condition is false.



Like i said, though, this is not idiomatic Python.  Python has for

loops which are based around the idea of iterating over a sequence. 

So, we could count to 9 like this:



#### count to 9

for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:

    print i

####



The loop will go through the list, assigning each item to i in turn,

until the list is exhausted.  The range() function is useful for

building lists like that, so we don't have to type it out manually.



#### count to 9

for i in range(10):

    print i

####



And, of course, once we've got range(), we can give it a variable

limit (eg, n = 10; range(n)).  Iteration is the key to doing all kinds

of funky stuff in python (including new ideas like geneartor

functions), so it's good to get the hang of :-)



--

John.

_______________________________________________

Tutor maillist  -  Tutor at python.org <mailto:Tutor at python.org> 

http://mail.python.org/mailman/listinfo/tutor
<http://mail.python.org/mailman/listinfo/tutor> 


A new monthly electronic newsletter covering all aspects of MED's work is now available.  Subscribers can choose to receive news from any or all of seven categories, free of charge: Growth and Innovation, Strategic Directions, Energy and Resources, Business News, ICT, Consumer Issues and Tourism.  See http://news.business.govt.nz for more details.




http://www.govt.nz - connecting you to New Zealand central & local government services

Any opinions expressed in this message are not necessarily those of the Ministry of Economic Development. This message and any files transmitted with it are confidential and solely for the use of the intended recipient. If you are not the intended recipient or the person responsible for delivery to the intended recipient, be advised that you have received this message in error and that any use is strictly prohibited. Please contact the sender and delete the message and any attachment from your computer.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051111/09ddc15e/attachment-0001.html


More information about the Tutor mailing list