So I have
a=set(["a"])
b=set(["b"])
c=a&b
would return a empty set, c=set([])
But when i type c = a and b
, why does it give me a set(["b"])
What is the "and
" and "or
" doing when used in two sets?
So I have
a=set(["a"])
b=set(["b"])
c=a&b
would return a empty set, c=set([])
But when i type c = a and b
, why does it give me a set(["b"])
What is the "and
" and "or
" doing when used in two sets?
A few things.
a = {"a"}
.and
and or
are logical operators. They're used for Python's conditional logic. The |
and &
operators are bitwise; they are overloaded by the set()
class. What you should be doing is a & b
. You can read more here: http://docs.python.org/2/library/sets.html#set-objectsIf you try a = 5 and 6
you will see the result is 6
. That is how boolean operators work in many programming languages, including Python. It's essentially saying "check if the value on the left evaluates to True
, if so, return the value on the right."
The answers here regarding or
and and
are a little wanting here. It's true that they're boolean operators and have nothing to do with sets, though. They're short-circuit boolean operators and are fairly commonly used as a short cut for assigning values with fallback values.
Here's how it works...
If you have a statement saying a or b
and you had to evaluate if that whole statement was true, you'd first start by figuring out if a
was true. If you found a
was true, then you don't need to bother evaluating b
because true or'ed with anything is always true. On the other hand, if a
is false, you have to evaluate b
to determine if the whole statement is false or not. Also, whatever b
is will be the result of the whole a or b
statement.
So, x = a or b
will first test if a
is true and if it is the whole right side of the statement will become a
. Likewise, if a
is false, the whole right side becomes b
because b
will determine if a or b
is true since we know a
is false.
and
is also a short circuit operator. With a and b
, if a
is false we can stop evaluating and say the whole statement is false and ignore b
. If a
is true, the whole statement relies on the boolean value of b
.
So, x = a and b
will first test if a
is true. If a
is true, then the result of a and b
can be simplified to b
and then the statement becomes x = b
. If a
is false, then we can stop at a
and just replace a and b
with a
making it x = a
.
I don't think I've ever seen the use of and
in this fashion, but it does work.
a and b
is returning b
. This is boolean logic, not a set intersection. In Python, the following are "falsy" objects: None
, False
, 0
, ""
, []
, tuple()
, set([])
, etc. Since a
and b
are both "truthy" objects, b
is returned from a and b
.
http://docs.python.org/2/tutorial/datastructures.html#more-on-conditions
One use of this is to initialize a variable like this:
def f(x):
x = x or MyObject()
c=a&b
returns the intersection of the two sets, which in this case is nothing. c=a and b
uses the standard Python logical operators. So when you say c=a and b
you are basically saying c=(a is True) and (b is True). Naively, since both a and b in this case are not empty and evaluate to True, you would expect c to be True. However, when you use the logical operators to set a value, Python returns not just True but the value of the last True object, which in this case is b.