python - 以下 Pyhon 代码打印了什么?
问题描述
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)
问题一:
怎么理解 (animal >= 'M') and (animal <= 'Z')
?
问题2:
正确答案是Pig
Owl
Mouse
,为什么Zebra
在这种情况下不打印?
解决方案
就像 zvone 指出的那样,字符串按字母顺序进行比较,因此不打印 Zebra。
在这种情况下,字母顺序将是。
X Y Z Za 斑马
推荐阅读
- drupal - Drupal 更新 8.9.11 到 8.9.15 生成未知列错误
- jasper-reports - .jrxml 文件中的字段不会因负值的动态值溢出而拉伸
- c - 将 char 数组传递给方法时,该数组会丢失其值
- java - 考虑到 500 个请求/秒,我如何优化 for 循环
- node.js - 使用增量函数进行验证
- python - 通过用户输入在 CSV 文件中使用查询
- javascript - 要求 remoteParticipant 在房间视频通话中共享他们的屏幕之前获得批准 twilio
- anylogic - Anylogic,如何创建取决于每天到达代理数量的动态批量大小?
- java - gradlew bootRun 与 java -jar
- python - 我可以在英国开放天气地图中按邮政编码搜索吗