1

Sorry for asking a very simple question. I'm trying to teach myself python which seems pretty easy but I get stuck on some things.

x = input("Enter a number: ")

if x > 10 & x < 20:
   print "YES!!!"

else:
   print "Not between 10 and 20"

In C (pseudocode) I would do

if(x > 10 && x < 20)
     "yes"
else
     "no"

and it would work.. but in Python it just keeps printing out YES!!! no matter what I input. What am I doing wrong?

When I run it:

Enter a number: 3
YES!!!
>>> ================================ RESTART ================================
>>> 
Enter a number: 13
YES!!!
>>> ================================ RESTART ================================
>>> 
Enter a number: 6
YES!!!
>>> ================================ RESTART ================================
>>> 
Enter a number: 13
YES!!!
>>> ================================ RESTART ================================
>>> 
Enter a number: 4
YES!!!
>>> ================================ RESTART ================================
>>> 
Enter a number: 8
Not between 10 and 20
>>> ================================ RESTART ================================
>>> 
Enter a number: 13
YES!!!
>>> ================================ RESTART ================================
>>> 
Enter a number: 3
YES!!!
>>> ================================ RESTART ================================
>>> 
Enter a number: 8
Not between 10 and 20
2
  • @arshajii: Wouldn't Python 3.x require parentheses around the argument to print? Commented Sep 5, 2013 at 22:42
  • @KeithThompson That's right, I just realized that. Commented Sep 5, 2013 at 22:42

4 Answers 4

8

& is the bitwise "and" operator in python. What you want is the logical "and", which is just the keyword: and.

if x > 10 and x < 20:

---Pedantic Explanation---

Now, if that were the only issue at play, you'd actually be fine, since Python True and False are just 1 and 0. However, there's something else going on that actually causes the problem. Specifically, the bitwise operators have higher precedence than the comparison operators. As a result, Python sees (for example, if x = 5) 5 > 10 & 5 < 20 and parses it as 5 > (10 & 5) < 20, which is a chained comparison. This is further expanded to:

(5 > (10 & 5)) and ((10 & 5) < 20)

Which evaluates to True, since 10 & 5 is zero:

(5 > 0) and (0 < 20)
True

Notice that 10 & 8 evaluates to 8, which is why when you input 8 you get the "correct" result of not satisfying the condition (since 8 is not less than 8 for the first condition):

(8 > (10 & 8)) and ((10 & 8) < 20)
(8 > 8) and (8 < 20)
False
Sign up to request clarification or add additional context in comments.

3 Comments

This is primarily important here because of precedence; x > 10 & x < 20 is interpreted as the chained comparison x > (10 & x) < 20.
And unlike in most other languages, 10 < x < 20 is equivalent to x > 10 and x < 20. (Don't try that in C; it's legal but has a completely different meaning.)
@user2357112 Yeah; I'm editing in a clarification to that effect as we speak :-)
1

Operator & is binary operator. You probably want to use 'and' logical operator.

example: (a and b) is true.

Replace '&' for 'and'.

Comments

1

In Python you need to use the Boolean operators and and or to combine Boolean expressions, not &, |, or any other variant involving those characters:

if x > 10 and x < 20:
   print "YES!!!"
else:
   print "Not between 10 and 20"

In this case you can also shorten it a bit:

if 10 < x < 20:

As for what is happening with your current code, & is a bitwise "and" operator, and it has higher operator precedence than comparisons so you are ending up with x > (10 & x) < 20 which is equivalent to (x > (10 & x)) and ((10 & x) < 20). Obviously this is pretty far from what you are trying to do, but as it turns out this won't always evaluate to True, it evaluates to False if x is a negative number, 0, 2, 8, or 10.

Comments

0

Logical AND in python is and keyword, not &. That's bitwise and operation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.