0

I have a bunch of variables that I need to verify whether they are within a determined range.

Below is a quick example to demonstrate what I want to achieve.

a=1
b=3
c=5

if 0 <= {a,b,c} <= 6:
    print("yes")
else:
    print("no")

I expect it to print yes but it prints no. What I'm I doing wrong?

Malyk
  • 143
  • 2
  • 13

2 Answers2

3

What am I doing wrong?

{a,b,c} is a set of three values. It's not a value between 0 and 6.

The condition 0 <= {a,b,c} <= 6 actually produces an exception in Python 3, because it doesn't make sense to ask if a set is more or less than a number. If your code prints "no", then you're probably using Python 2.

If you want to check if a condition holds for every value in some sequence, you can use the built-in function all.

if all(0 <= x <= 6 for x in (a,b,c)):
    print("yes")
else:
    print("no")
khelwood
  • 46,621
  • 12
  • 59
  • 83
  • Thanks this solved my problem. I was using Python 2 as you said. Your solution works with both Python 2 and 3. In the future, if I had float numbers would I just cast the variables to int or is there a better solution? – Malyk Jan 16 '19 at 11:05
  • 2
    @Malyk , if you're using Python 2 why you tagged question with `python-3.x`? – Andersson Jan 16 '19 at 11:09
  • @Malyk This would work the same with floats. There's no problem using `<=` to compare ints and floats. – khelwood Jan 16 '19 at 11:24
1

With Python 3.x, for integers you can use all with range:

if all(x in range(7) for x in (a, b, c)):
    # your logic

For non-integers or Python 2.x, you can use chained operations:

if all(0 <= x <= 6 for x in (a, b, c)):
    # your logic

The problem with your logic is Python does not implement vectorised operations with comparison operators.

jpp
  • 134,728
  • 29
  • 196
  • 240
  • While this works in this specific example, don't do that. `3.14 in range(7)` will be `False` while `0 <= 3.14 <= 6` continues working as expected. – Fynn Becker Jan 16 '19 at 10:55
  • @FynnBecker, That's why I stated explicitly `range` works only for integers. – jpp Jan 16 '19 at 10:55
  • @jpp I though about this solution but my range is large between 0 and 100,000 and my doing this for many columns. Isn't this time expensive? – Malyk Jan 16 '19 at 10:56
  • @Malyk, No, `range` objects are very efficient, they don't create an in-memory object. Related: [Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?](https://stackoverflow.com/q/30081275/9209546) – jpp Jan 16 '19 at 10:56
  • I think the OP is actually using Python 2, so using `range` for this kind of thing isn't necessarily a good idea. In Python 3, the OP's code would have raised an exception. – khelwood Jan 16 '19 at 10:57
  • 1
    @jpp What I'm trying to say is that this is an anti-pattern IMO. Even for integers you shouldn't use `range()`. – Fynn Becker Jan 16 '19 at 10:57
  • @khelwood, The questions is tagged 3.x – jpp Jan 16 '19 at 10:58
  • @FynnBecker, Can you explain / suggest *why* it shouldn't be used for integers? – jpp Jan 16 '19 at 10:58
  • @jpp But the OP's code would not produce the output they describe in Python 3, only in Python 2. – khelwood Jan 16 '19 at 10:58
  • Sorry guys I was using Python 2. It's my mistake I should remove the tag. – Malyk Jan 16 '19 at 11:08