Python: How to express less or equal, equal more, environment?

I want to make the following example in python:

x = 10 
y = 8

if x-5 <= y <= x+5:
     print(y)

I see that this works, but I would like to know if it is so good, if there is a better solution or something that I should consider doing so.

+4
source share
1 answer

Secondary expressions are acceptable in Python:

The comparison can be copied arbitrarily, for example, x < y <= zequivalently to x < y and y <= z, except that it yis evaluated only once (but in both cases the cases zare not evaluated at all when x < yit turns out False).

In fact, since andlazy, and the syntax is cleaner, they are preferable.

, . , . 0 < 0 == 0 return False Python?.

0

Source: https://habr.com/ru/post/1693451/


All Articles