Re: [Tutor] Correct use of range function..

2007-06-11 Thread adam urbas
I see... Very Intriguing.> To: tutor@python.org> From: [EMAIL PROTECTED]> Date: 
Sun, 10 Jun 2007 18:16:00 +0100> Subject: Re: [Tutor] Correct use of range 
function..> > > "Adam Urbas" <[EMAIL PROTECTED]> wrote> >I discovered something 
about your revers word program here.  I used> > the "for c in word" one.> > if 
you type an indented print after print c, then it will print the> > words 
vertically.  Just thought I'd share that with you.> > You can achieve the same 
by missing out the comma at the end> of the print statement too. The comma 
suppresses a newline> character. By using a second print you put it back! So 
just> missing the comma achieves the same end result.> > Alan G.> > > On 
6/10/07, Kent Johnson <[EMAIL PROTECTED]> wrote:> >> David Hamilton wrote:> >> 
> I just finished doing an exercise in a tutorial on the range > >> > function> 
>> > and while I got it to work, my answer seems ugly. I'm wondering > >> > if 
I'm> >> > missing something in the way I'm using the range function.> >> > The 
tutorial ask me to print a string backwards. My solution > >> > works, but> >> 
> it it just doesn't "feel" right :).  My result is difficult to > >> > read 
and> >> > I feel like I'm probably over complicating the solution. > >> > 
Suggestions?> >> >> >> > word="reverse"> >> > #Start at the end of the string, 
count back to the start, > >> > printing each> >> > letter> >> > for  i in 
range(len(word)-1,-1,-1):> >> > print word[i],> >>> >> That's probably the 
best you can do using range(). You could write> >> ln = len(word)> >> for i in 
range(ln):> >>print word[ln-i-1],> >>> >> but that is not much different.> 
>>> >> You can do better without using range; you can directly iterate the> >> 
letters in reverse:> >>> >> for c in word[::-1]:> >>print c,> >>> >> Kent> 
>> ___> >> Tutor maillist  -  
Tutor@python.org> >> http://mail.python.org/mailman/listinfo/tutor> >>> > 
___> > Tutor maillist  -  
Tutor@python.org> > http://mail.python.org/mailman/listinfo/tutor> > > > > 
___> Tutor maillist  -  
Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor
_
Hotmail to go? Get your Hotmail, news, sports and much more! Check out the New 
MSN Mobile! 
http://mobile.msn.com___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Correct use of range function..

2007-06-10 Thread Alan Gauld

"Adam Urbas" <[EMAIL PROTECTED]> wrote
>I discovered something about your revers word program here.  I used
> the "for c in word" one.
> if you type an indented print after print c, then it will print the
> words vertically.  Just thought I'd share that with you.

You can achieve the same by missing out the comma at the end
of the print statement too. The comma suppresses a newline
character. By using a second print you put it back! So just
missing the comma achieves the same end result.

Alan G.

> On 6/10/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>> David Hamilton wrote:
>> > I just finished doing an exercise in a tutorial on the range 
>> > function
>> > and while I got it to work, my answer seems ugly. I'm wondering 
>> > if I'm
>> > missing something in the way I'm using the range function.
>> > The tutorial ask me to print a string backwards. My solution 
>> > works, but
>> > it it just doesn't "feel" right :).  My result is difficult to 
>> > read and
>> > I feel like I'm probably over complicating the solution. 
>> > Suggestions?
>> >
>> > word="reverse"
>> > #Start at the end of the string, count back to the start, 
>> > printing each
>> > letter
>> > for  i in range(len(word)-1,-1,-1):
>> > print word[i],
>>
>> That's probably the best you can do using range(). You could write
>> ln = len(word)
>> for i in range(ln):
>>print word[ln-i-1],
>>
>> but that is not much different.
>>
>> You can do better without using range; you can directly iterate the
>> letters in reverse:
>>
>> for c in word[::-1]:
>>print c,
>>
>> Kent
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Correct use of range function..

2007-06-10 Thread Alan Gauld
"Kent Johnson" <[EMAIL PROTECTED]> wrote

> You can do better without using range; you can directly iterate the
> letters in reverse:
>
> for c in word[::-1]:
>   print c,

Or even just

print word[::-1]

:-)

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Correct use of range function..

2007-06-10 Thread Adam Urbas
I discovered something about your revers word program here.  I used
the "for c in word" one.
if you type an indented print after print c, then it will print the
words vertically.  Just thought I'd share that with you.

On 6/10/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
> David Hamilton wrote:
> > I just finished doing an exercise in a tutorial on the range function
> > and while I got it to work, my answer seems ugly. I'm wondering if I'm
> > missing something in the way I'm using the range function.
> > The tutorial ask me to print a string backwards. My solution works, but
> > it it just doesn't "feel" right :).  My result is difficult to read and
> > I feel like I'm probably over complicating the solution. Suggestions?
> >
> > word="reverse"
> > #Start at the end of the string, count back to the start, printing each
> > letter
> > for  i in range(len(word)-1,-1,-1):
> > print word[i],
>
> That's probably the best you can do using range(). You could write
> ln = len(word)
> for i in range(ln):
>print word[ln-i-1],
>
> but that is not much different.
>
> You can do better without using range; you can directly iterate the
> letters in reverse:
>
> for c in word[::-1]:
>print c,
>
> Kent
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Correct use of range function..

2007-06-10 Thread Kent Johnson
David Hamilton wrote:
> I just finished doing an exercise in a tutorial on the range function 
> and while I got it to work, my answer seems ugly. I'm wondering if I'm 
> missing something in the way I'm using the range function.
> The tutorial ask me to print a string backwards. My solution works, but 
> it it just doesn't "feel" right :).  My result is difficult to read and 
> I feel like I'm probably over complicating the solution. Suggestions?
> 
> word="reverse"
> #Start at the end of the string, count back to the start, printing each 
> letter
> for  i in range(len(word)-1,-1,-1):
> print word[i],

That's probably the best you can do using range(). You could write
ln = len(word)
for i in range(ln):
   print word[ln-i-1],

but that is not much different.

You can do better without using range; you can directly iterate the 
letters in reverse:

for c in word[::-1]:
   print c,

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor