Connecting Tech Pros Worldwide Help | Site Map

Evaluation of a comparison in combination with the in operator

bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#1: 3 Weeks Ago
I ran into something that seemed strange at first. I knew you could chain comparisons like x < y < z which evaluates to x < y and y < z. I did not realize this works in a similar manner:
Expand|Select|Wrap|Line Numbers
  1. >>> x = '21'
  2. >>> s = ['21', '22', '23']
  3. >>> y = '21'
  4. >>> y == '21' in s
  5. True
  6. # evaluates as y == '21' and '21' in s
  7. >>> y = "20"
  8. >>> y == "20" in s
  9. False
  10. # evaluates as y == '20' and '20' in s
It's not really a question, but tell me if I am understanding this incorrectly.

BV
Member
 
Join Date: Nov 2008
Posts: 47
#2: 2 Weeks Ago

re: Evaluation of a comparison in combination with the in operator


that is interesting. not very intuitive
Reply