[Tutor] Creating Sudoku
Luke Paireepinart
rabidpoobear at gmail.com
Mon Apr 7 17:53:11 CEST 2008
W W wrote:
> On 4/7/08, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
>
>> W W wrote:
>> What are you talking about? I don't understand what you mean by "ignores
>> whitespace between dictionary elements."
>>
>>
>>> foo = {'1a': 1, '1b':2, '1c':3,
>>> '2a': 0, '2b': 9, '2c': 6}
>>>
>
> Exactly that. If you were to write:
>
> foo = {'1a': 1, '1b':2, '1c':3}
> foo['2a'] = 0
>
> You would get a nifty error.
>
You mean that the dictionary _definition_ ignores whitespace between
elements?
That's quite different than the dictionary itself ignoring whitespace.
That implies that
foo['1b'] is the same element as foo['1 b'], hence the source of my
confusion.
That's not a feature of dictionaries, but of the comma.
You can easily do the following:
x = [1, 2,
3, 4]
if you want.
Same with tuples and various other things.
Python just realizes that if it doesn't see a closing brace/bracket, but
sees a comma, that more will probably be coming on the next line.
You can do the same thing with backslash, if your statement does not end
in a comma: for example,
x = 1 + 1 + \
2 + 3 + 5 \
+ 8 + 13
Also, did you test the code that "generates an error?"
It works fine for me.
>>> foo = {'1a': 'b'}
>>> foo['2b'] = 0
>>> print foo['2b']
0
>>>
Hope that helps,
-Luke
More information about the Tutor
mailing list