Binary value comparison issue in python

Viewed 14

I came up with this comparison issue in python in binary values which is mentioned here in the image.

Python IDLE Screenshot

What are the possible reasons for returning "True" to the comparison between bin(1538)<bin(5138) since numerical value and binary digit length of the two binary values are contradictory to the output in python? Are there any specific comparison rules in python when it comes to the comparisons between binary values generated by bin() function (python default function) or are they similar as normal number comparisons?

1 Answers

because you are comparing two strings and the first string is "larger" than the second one...

try this as an example:

x = bin(1538)
print(x)
print(type(x))

y = bin(5138)
print(y)
print(type(y))

returns this:

0b11000000010
<class 'str'>
0b1010000010010
<class 'str'>
Related