Strange behavior with sort()

Gary Herron gary.herron at islandtraining.com
Thu Feb 27 02:16:27 EST 2014


On 02/26/2014 10:24 PM, ast wrote:
> Hello
>
> box is a list of 3 integer items
>
> If I write:
>
>    box.sort()
>    if box == [1, 2, 3]:
>
>
> the program works as expected. But if I write:
>
>    if box.sort() == [1, 2, 3]:

Most such questions can be answered by printing out the values in 
question and observing first hand what the value is.

So, print out box.sort() to see what it is.  You might be surprised.

Hint: box.sort() does indeed cause box to be sorted, and the sorted list 
is left in box, but the sorted list is not returned as a function value.

>
> it doesn't work, the test always fails. Why ?
>
> Thx




More information about the Python-list mailing list