# If condition code, how common is it to write like this?

Hello

This code below:

``````if 10000 < user_ratings <= 100000:
``````

How common is it for Python developers to write that piece of code instead of writing:

``````if user_rating > 10000 and user_rating <= 100000
``````

Regards,
Lee

Can’t really say how common this would be, but I would assume fairly common. This is actually mentioned in the official documentation, so knowing this would be better -

Comparisons can be chained arbitrarily, e.g., `x < y <= z` is equivalent to `x < y and y <= z` , except that `y` is evaluated only once (but in both cases `z` is not evaluated at all when `x < y` is found to be false).

And because of that statement above (and Python’s underlying code/optimization) -

except that `y` is evaluated only once

The `x < y <= z` approach is faster than using the `and` approach.

1 Like

``````if 100000 < user_ratings <= 500000:
user_ratings_freq['100000 - 500000'] +=1
``````

I’ve been used to something like this, in Pascal, VISUAL BASIC, :

``````if user_ratings >= 100000 and user_rating <= 500000:
user_ratings_freq['100000 - 500000'] +=1
``````

I guess I need to look at the former code and think, if 99999 or less, is less than what is in user_rating. So say that it is true. Then if user_rating is also less than or equal to 500000, then add 1 to current count.

Am I right?

Regards
Lee

Not quite accurate. `100000` is a fixed value. So you can’t say if 99999 or less is less than what is in `user_rating`. Because, if you phrase it that way, it implies that you are considering the `99999` to be variable.

Better to state that `user_rating` is more than `100000` and less than or equal to `500000`.

1 Like