A problem with str VS int.

dn PythonList at DancesWithMice.info
Tue Dec 12 04:08:58 EST 2023


On 12/12/23 21:22, Steve GS wrote:
> With all these suggestions on
> how to fix it, no one seems to
> answer why it fails only when
> entering a two-digit number.
> One and three work fine when
> comparing with str values. It
> is interesting that the
> leading 0 on a two digit
> worked.  Still, one digit and
> three digit work but not two.
> 
> This is now more of a
> curiosity as I did use the
> integer comparisons.


Emphasis on the word "seems"!

Did you try running the code provided earlier? Did you notice that such 
illustrated what happens when using strings which appear as numbers, and 
showed how a three digit number (expressed as a string) may well precede 
a two- (or even a one-) digit number in the same form - and that the 
end-result of the sequence of integers is quite-different to the 
sequence of integer-values expressed as strings!

Why does this happen? Because of the way data is encoded. It is language 
independent. The definition of order or sequence is called "collation".

"Comparisons" establish relative-sequence. You will find some handy 
explanations of how comparisons work (eg equals or less-than) in the 
Python manual.

After working through those three steps, if there's something that's 
still mystifying, please refine the question...


Web.Refs:
https://en.wikipedia.org/wiki/Collation
https://docs.python.org/3/reference/expressions.html?highlight=comparison#value-comparisons
https://docs.python.org/3/howto/sorting.html?highlight=sort


-- 
Regards,
=dn



More information about the Python-list mailing list