+ 2
Why is ('bcd' >= 'azd') true?
Played around with the string comparison, and got wrong results. Or perhaps I didn't get the concept. Also tried the lenght of the array, and strill ( 'bcd' >= 'aaaa') gives me true https://code.sololearn.com/cjOqc4NBgLsf/?ref=app
5 Respostas
+ 3
It's because a<b in lexicographical (that is, alphabetical) ordering.
See the docs:
https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types
The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.
So you have this:
>>> print('abc'<'abc')
False
>>> print('abc'<'abd')
True
>>> print('abc'<'abcde')
True
>>> print('abc'<'abb')
False
>>> print('abc'<'ab')
False
>>> print('abc'<'aa')
False
+ 1
Also tested the lenght, still doesn't work as I thought it would
0
Since strings, this is an array of letters, actually logical applications to them, will ponder the length of the array.
0
b = 98, c = 99, d = 100;
a = 97, z = 122, d = 100;
I don't know why
- 1
You know, it displays the value of the operator. Strings are ignored