[Tutor] a little loop

John Steedman johnsteedman360 at gmail.com
Tue May 28 08:36:53 CEST 2013


Some other tools, if you haven't come across them yet.

You already know about str.join ()

Slicing

>>>b=['s','p','a','m']
b [ : 1 ]
['s']
b [ : 2 ]
['s', 'p']

Also, consider

>>>len ( b)
4

>>>range ( 4 )
[ 0, 1, 2, 3, 4]
# which I can iterate over.












On Tue, May 28, 2013 at 4:54 AM, Tim Hanson <tjhanson at yahoo.com> wrote:

> Okay, so I made it to FOR loops in the Lutz book.  A couple of days ago I
> was
> helped here with the .join method for creating strings from lists or
> tuples of
> strings.  I got to wondering if I could just, for the sake of learning, do
> the
> same thing in a FOR loop, since that's today's chapter:
>
> x=0; ham=''; b=['s','p','a','m'] #or, b=('s','p','a','m')
> for t in b:
>         ham=ham+b[x]
>         print(ham);x+=1
>
>
> s
> sp
> spa
> spam
>
> Alright, it works, eventually.  Can someone help me find a little more
> elegant
> way of doing this?  I'm sure there are several.
>
> Incidentally, I put the print statement within the FOR loop so I could
> watch
> progress.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130528/f75c3aab/attachment.html>


More information about the Tutor mailing list