37

I'm a little confused with the results I'm getting with the logical operators in Python. I'm a beginner and studying with the use of a few books, but they don't explain in as much detail as I'd like.

here is my own code:

five = 5
two = 2

print five and two

>> 2

It seems to be just outputting the two variable.

five = 5
two = 2
zero = 0

print five and two and zero

So, I added another variable integer. Then I printed and got the following output:

>> 0

What is going on with Python in the background? Why isn't the output something like 7 or 5, 2.

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
BubbleMonster
  • 1,366
  • 8
  • 32
  • 48
  • 1
    Thanks guys, I do understand that I could simply do five + two which would equal 7, I just wanted to know exactly how the AND operator works. – BubbleMonster Aug 12 '13 at 19:57
  • It might be interesting to note that had you swapped the order of the variables in your last statement --- `print five and zero and two` --- you would have gotten `0` as the result. You could say that `and` "returns the first [falsey](http://docs.python.org/release/2.5.2/lib/truth.html) value it finds, and if it doesn't find anything, returns the last result. – Justin L. Sep 23 '13 at 17:50
  • The reason for this is because `and` answers the question: "Are all of these things truthy?", and returns something truthy/falsey as its answer. Consider `a and b`. `and` would check `a` first. If `a` is falsey, then the entire `and` statement must return false (because if one thing is false, then it is impossible that both are true). So `and` would return something falsey, and it uses `a` (because `a` is already falsey) as a matter of convenience. However, if `a` is true, then `and` needs to only return `b`. – Justin L. Sep 23 '13 at 17:54
  • Why? Because if `b` is truthy, then both `a` **and** `b` are truthy. If `b` is fasley, then it must be that the entire `and` statement is false (because not both are true). So the entire result of the `and` statement replies completely on `b`. If `b` is true, it gives the right answer. If `b` is false, it gives the right answer. So `and` will return the first value if the first value is false, and otherwise, return the second value. – Justin L. Sep 23 '13 at 17:56
  • Sounds like it is returning you the minimum value. –  Jul 13 '17 at 06:55

7 Answers7

48

Python Boolean operators return the last value evaluated, not True/False. The docs have a good explanation of this:

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • 1
    Also summarized on this docs page: http://docs.python.org/2/library/stdtypes.html#boolean-operations-and-or-not – Peter DeGlopper Aug 12 '13 at 19:43
  • Now that Python has conditional expressions, you can think of `x and y` as a shorter way of writing `y if x else x`. – dan04 Sep 28 '16 at 22:23
  • 2
    @dan04 that's mostly true and for most people its more clear than the booleans so should be preferred. But since `x` may need to be calculated twice, its not the right way to go if `x` is expensive or has side effects (functions or complicated expressions, etc... like perhaps `expensive_x() and expensive_y()`). But then its more clear to write an `if` clause anyway. – tdelaney Sep 29 '16 at 03:01
5

As a bit of a side note: (i don't have enough rep for a comment) The AND operator is not needed for printing multiple variables. You can simply separate variable names with commas such as print five, two instead of print five AND two. You can also use escapes to add variables to a print line such as print "the var five is equal to: %s" %five. More on that here: http://docs.python.org/2/library/re.html#simulating-scanf

Like others have said AND is a logical operator and used to string together multiple conditions, such as

if (five == 5) AND (two == 2):
    print five, two
aChipmunk
  • 101
  • 1
  • 7
3

Boolean And operators will return the first value 5 if the expression evaluated is false, and the second value 2 if the expression evaluated is true. Because 5 and 2 are both real, non-false, and non-null values, the expression is evaluated to true.

If you wanted to print both variables you could concatenate them to a String and print that.

five = 5
two = 2
print five + " and " + two

Or to print their sum you could use

print five + two

This document explains how to use the logical Boolean operators.

deadboy
  • 849
  • 2
  • 9
  • 28
1

This AND in Python is an equivalent of the && in Java for instance. This doesn't mean the and in the English language. The AND is a logical operator. Assume five holds 5 and two holds 2. From Python documentation: The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned. Basically, it evaluates the last integer in your case which is true.

if (five and two):
...     print "True"
... else:
...     print "False"

The AND is a logical operator, to test the logic for a specific case, not an arithmetic operator. If you want to get results like 7 for five and two, you should rather use "+" which means adding two integers. See below:

>>> five = 5
>>> two = 2
>>> print five + two
7
Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
  • I'm not sure that this answers the OP's question though, he's asking `why` he gets the result that he is, why it returns the value of the last variable instead of just a true/false answer. – Lochemage Aug 12 '13 at 19:48
  • I just told the answer :) Check again – Ali Gajani Aug 12 '13 at 19:49
  • He's expecting `5 and 2` to return `7`, I don't think he's asking why it doesn't return specifically true/false. – kindall Aug 12 '13 at 19:52
1

Try 0 and 9.

The result is 0 because the value of 0 is false. The operand on the left of the and operator is False so the whole expression is False and returns 0

SiHa
  • 7,830
  • 13
  • 34
  • 43
0

In Python any non-zero integer value is true; zero is false.

The OP’s values are both non-zero.

The AND operator tests from left to right,

with and, if all values are True, returns the last evaluated value. If any value is false, returns the first one.

Since both are non-zero, both are true, so the last value is returned

-2

To convert the integer variable to a string , add str in front of each variable

five = 5 two = 2

print(str(five) + " and " + str(two))

Alex
  • 1
  • 1
    The question is about the logical operators and not about how to print the values as string; also, I would rather say it's more about the sum rather than the string print as how the question is formulated – Juan Medina May 12 '21 at 18:22