Newcomer to Python tutorial question

Alan Cameron alan.cameron at iname.com
Thu May 7 15:15:44 EDT 2009


"Chris Rebert" <clp2 at rebertia.com> wrote in message 
news:mailman.5238.1241723354.11746.python-list at python.org...
> On Thu, May 7, 2009 at 11:58 AM, Alan Cameron <alan.cameron at iname.com> 
> wrote:
>> "Alan Cameron" <alan.cameron at iname.com> wrote in message
>> news:hRFMl.50224$tb.4955 at newsfe07.ams2...
>>>I am not sure of this is the right place to ask a question about the
>>>tutorial
>>>
>>> http://docs.python.org/3.0/tutorial/datastructures.html#sets
>>>
>>> why is the printed result of
>>>
>>>>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>>>>> print(basket)
>>> {'orange', 'banana', 'pear', 'apple'}
>>>
>>> in the sequence given?
>>>
>>>
>>
>> Thanks to all who replied.
>> I assume therefore that the order in which the items of the set are 
>> printed
>> could vary each time it is printed?
>
> Due to the underlying dict-based implementation, the order will stay
> the same until you modify the set (i.e. add or remove an element), at
> which point it may change; it's basically the same behavior as with
> printing a dict.
>
> So this will always print the same thing twice:
> print basket
> print basket
>
> Whereas this might not:
> print basket
> #modify the set
> basket.discard("banana")
> basket.add("banana")
> print basket
>
> Cheers,
> Chris

Thanks Chris,

It appears that I used a reserved term when I used 'sequence'. I just had 
not reached that far in the tutorial.
I have many years of programming (roughly 50) and want to learn new 
languages.
I find tutorials always fraught with problems due to the knowledge of the 
writer exceeding the knowledge of the reader and using terms and examples 
not yet covered in the tutorial thus far.
I am persevering with my initial foray into Python.

-- 
Alan Cameron 





More information about the Python-list mailing list