Python is readable

Thu Mar 15 12:27:28 CET 2012

On 3/15/2012 11:50, Chris Angelico wrote:
> On Thu, Mar 15, 2012 at 9:44 PM, Kiuhnm
> < at>  wrote:
>> Let's try that.
>> Show me an example of "list comprehensions" and "with" (whatever they are).
> I'll do a list comp, because they lend themselves well to one-liners.
> what_am_i = '\n'.join(["%X\t%c"%(i,i) for i in range(128)])

A few conjectures:
1) '\n' is an object and join one of its methods;
2) [...] is a list comprehension;
3) that 'for' suggests that range isn't (or doesn't return) a list but 
an iterator;
4) points 2 and 3 suggest that [...] builds a list (or array?) by 
querying an iterator.
5) "%X\t%"(i,i) is probably equivalent to the C-like Perl's
   sprintf("%X\t%c", i, i)

So what_am_i is a simple ASCII table.

> Okay, that one also uses printf formatting, which may be a smidge
> obscure. Here's a simpler example:
> what_am_i = [x*x for x in range(11)]

what_am_i = 0, 1, 4, 9, ..., 100

Your first example suggests that range(n) is a sequence iterator which 
returns, if queried n times,
(which is a bit counterintuitive, IMHO).


More information about the Python-list mailing list