0

I am new to python. I have tested my interpreter using following code,

In [1]: 2 and 3
Out[1]: 3

In [2]: 3 and 2
Out[2]: 2

In [3]: 3 or 2
Out[3]: 3

In [4]: 2 or 3
Out[4]: 2

In the above, take 2=0010 and 3=0011. the result is,

+ 0010
  0011
  ----
  0010=2

But Out[1] gave the 3(not exact) and out[2] gave the 2(exact).

What is the difference in two cases?

1
  • usually people mix it up the other way around Commented Jun 17, 2013 at 9:44

4 Answers 4

10

You are using boolean logic or and and, which short-circuit (return the first operand for which the outcome of the operator is fixed).

You are looking for the binary bitwise operators instead, | and &:

>>> 0b10 & 0b1
0
>>> 0b10 | 0b1
3

The or operator returns the first operand if it is true-y (not empty or numeric 0), the second operand otherwise, the and operator returns the first if it is false-y, the second operator otherwise. This is why you see 3 and 2 return 2, and 3 or 2 return 3. Both 2 and 3 are non-zero, so true in a boolean context.

Using 0 as a false value you'd see:

>>> 3 and 0
0
>>> 3 or 0
3
>>> 0 and 3
0
>>> 0 or 3
3
Sign up to request clarification or add additional context in comments.

5 Comments

In python, a few values are considered to be "false": False, None, 0, and any object obj where len(obj)==0, for example "", [], () and {}. All other objects are considered to be "true". Here are a few examples using the logical operators: [] or 5 == 5, [] or () == (), True and None == None, [1,2,3] or False == [1,2,3], etc.
@MiniQuark: Apart from None they can all be reduced to 'empty' or 'numeric 0'.
you're right, but I wanted to spell it out because it's not trivial that int(False)==0 and int(None)==0. The precise rules are available here: docs.python.org/2/library/stdtypes.html#truth-value-testing
@MiniQuark: int(None) is just the int() constructor with no argument; True == 1 is True, as is False == 0, but None == 0 is False. That's because the bool type is a subclass of int.
@MiniQuark: And I am focusing here on the bitwise operators with a brief explanation as to why the OP got confused. There is no need to go into the nitty gritty details of what makes something falsey or truey, the documentation I link to contains that detail for someone that wants to know more.
6

You are looking for the bitwise operators,

>>> 2 & 3
2
>>> 2 | 3
3

By just doing 2 and 3 you are evaluating 2, which is True, then 3 (also True) and Python returns that second number. So you get 3.

With 2 or 3, it short-circuits and just returns 2 since 2 is True.

Comments

2

You are looking for the bitwise and, &.

and and or are boolean operators in Python, whereas & and | are bitwise operators.

Example -

>>> 2 and 3
3
>>> 2 & 3
2

Comments

1

use &, and is boolean AND in python:

>>> 2 & 3
2
>>> 3 & 2
2

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.