On Fri, Aug 19, 2011 at 3:03 PM, Alan Young <[email protected]> wrote:
> On Fri, Aug 19, 2011 at 12:49, Aaron Toponce <[email protected]> wrote:
>> python -c 'for i in range(1,101):print"FizzBuzz"[i*i%3*4:8--i**4%5]or i'
>
> Very cool.  Would you explain that for me please?  I can figure part
> of it, but I'm not familiar with the '--' operator in this context.
>
> Hazarding a guess, based on the output; "FizzBuzz"[x:y] ... x is
> offset.  I can't find anything referencing "string"[x:y] for python so
> I'm at a loss as to what that part is doing.

Its called slicing, and it lets you select a chuck of data out of
lists, tuples, strings, and other data structures

the first int is the start position, the second is the end (don't
include the end) (so [3:3] would return an empty string,

"0123456789"[3:5] returns "34"

you can also do [3:] to start at 3 and do the rest of the data
("3456789") or [:2] to start at the beginning and end at index 2
("01") or even negative indexing [:-1] "012345678", [-1] ("9"), etc.
--------------------
BYU Unix Users Group 
http://uug.byu.edu/ 

The opinions expressed in this message are the responsibility of their
author.  They are not endorsed by BYU, the BYU CS Department or BYU-UUG. 
___________________________________________________________________
List Info (unsubscribe here): http://uug.byu.edu/mailman/listinfo/uug-list

Reply via email to