+ 1
Please explain
a=[7,3,9,5] b=[8,0,4] If a>b: print(len(a)) else: print(len(b))
2 Respuestas
+ 5
When you compare two lists, Python does a pairwise comparison on each pair of elements.
7 < 8
Therefore a < b
Therefore the 'else' branch is evaluated and the size of list b (3) is printed.
https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types
By the way, "if" must be lowercase.
+ 5
Here is a description from the python docs, that shows very well how this comparison of 2 lists is done:
5.8. Comparing Sequences and Other Types¶
Sequence objects typically may be compared to other objects with the same sequence type. 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. If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one. Lexicographical ordering for strings uses the Unicode code point number to order individual characters.