3

Example is very simple:

a = 2
b = 7

a >= 1 & a <= 10**10 & b >= 1 & b <= 10**2
>False
a >= 1 & a <= 10**2 & b >= 1 & b <= 10**2
>True

This problem came to me as a simple typo. And got me curious. But in the end I can't really understand why does it behave like that?

statespace
  • 1,644
  • 17
  • 25
  • 1
    `&` doesn't do what you think it does. – tzaman Mar 12 '16 at 20:45
  • Use `and`, instead of `&`. – ekhumoro Mar 12 '16 at 20:47
  • Ouch... There goes my attempt to experiment with different languages. Even common operators are different. – statespace Mar 12 '16 at 20:47
  • 1
    This post deserves a delete. And I deserve to fix my dignity by starting all over with reading Python manual from page 1. *sigh* – statespace Mar 12 '16 at 20:48
  • 1
    @A.Val. `&` means bitwise AND in almost all C-influenced languages. If logical AND is written with the "&" symbol, it's almost always as `&&`. – Ben Mar 12 '16 at 20:53
  • @Ben I've been using exclusively R language for data analytics for last few years. That might explain this problem... Apparently re-learning syntax from scratch will be a bumpy ride. Thanks for explaining though. – statespace Mar 12 '16 at 20:57
  • @A.Val. please consider accepting the answer since it solved your question – Idos Mar 15 '16 at 09:26
  • @Idos I don't really mind... I just thought it is inappropriate for duplicated questions with answer linked. – statespace Mar 15 '16 at 11:02

1 Answers1

1

You should use the logical AND operator which is and in Python, and not &.

>>> a >= 1 and a <= 10**10 and b >= 1 and b <= 10**2
True
Idos
  • 15,053
  • 14
  • 60
  • 75