Thursday, 7 September 2017

python - Simplify Chained Comparison




I have an integer value x, and I need to check if it is between a start and end values, so I write the following statements:



if x >= start and x <= end:
# do stuff


This statement gets underlined, and the tooltip tells me that I must




simplify chained comparison





As far as I can tell, that comparison is about as simple as they come. What have I missed here?


Answer



In Python you can "chain" comparison operations which just means they are "and"ed together. In your case, it'd be like this:



if start <= x <= end:


Reference: https://docs.python.org/3/reference/expressions.html#comparisons



No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...