[Tutor] Explanation of this print statement

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Feb 9 01:46:07 CET 2015


On 09/02/2015 00:19, Alan Gauld wrote:
> On 08/02/15 23:01, Shawn Byers wrote:
>> Hello I was wondering if someone could explain this print statement
>>
>> for r in range(6,0,-1):
>>           print((6-r)*''+r*'o')
>
> Have you tried running it?
> Do you see what it is doing?
>
> Try evaluating range(6,0,-1) at the interpreter (you may want to convert
> it to a list) to see what it does.
>
>  >>> print( list(range(6,0,-1)) )
>
> Try substituting values for (6-r) and see what string
> results you get for each value.
> eg
>  >>> print(6 * '' + 0 * 'o')
>  >>> print(0 * '' + 6 * 'o')
>  >>> print(3 * '' + 3 * 'o')
>  >>> print(3 * '' + 4 * 'o')
>  >>> etc
>
> In other words, experiment in the >>> interpreter. That's
> what it's there for. And its quicker than sending an email
> and waiting for a reply (which may not cover the bit you
> want to understand anyway).
>
> Then if there's still something you don't understand come
> back and ask us about that specific aspect.
>

If it is a print statement the brackets are not needed, otherwise it's 
either the Python 3 or (Python 2 with from __future__ import 
print_function) print function.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list