-1
animals = ['horse', 'Pig', 'dog', 'Owl', 'lion', 'Hare', 'baboon', 'Fish', 'tiger', 'Zebra', 'Cow', 'Mouse', 'quail', 'Elephant']

for animal in animals:

    if (animal >= 'M') and (animal <= 'Z'):

        print(animal)

Question 1:

How to understand (animal >= 'M') and (animal <= 'Z') ?

Question 2:

The correct answer is Pig Owl Mouse, why Zebra is not printed in this case?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Leo
  • 93
  • 2
  • 11

4 Answers4

0

Like zvone pointed out, string are compared alphabetically and therefor Zebra is not printed.

The alphabetic order in this case would be.

X Y Z Za Zebra

0
  1. Animals that come alphabetically between "M" and "Z".
  2. "Zebra" comes after "Z" when sorted in python.
Ben
  • 2,348
  • 1
  • 20
  • 23
0

from a to z ordering is like that in alpha characters:

["a","aa","aaa" ... "ab","aba","abaa" ... "b"... "z", "za", "zaa" ... "zebra" ... "zzzzz" ...]

depending on their string representation of ascii equvalent is a < b < c ... < y < z and z always comes before z* (asterix means any)

obayhan
  • 1,636
  • 18
  • 35
0
  1. it will print animals that start from letters between 'M' and 'Y' (both included) and also single letter "Z".

  2. because "Z" < "Zebra"

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 26 '22 at 10:48