-2

I am using python 3.6.1 .

Question 1:

A)

False and 0 # gives False, but   

0 and False # gives 0 (both answers must be False)

like that

False or 0 # gives 0, but

0 or False # gives False (both answers must be False)

Why is it not giving all results as False but give the one result as 0 (in number)?

Likewise

B)

True and 1 # gives 1, but

1 and True # gives True

Why aren't all results in case 'B' True and 1st one gives 1? Why is it giving integers 1 and 0 as result (case A and B)? Is there any thing like direction in this?

Question 2:

Do numbers other than or 0 & 1 like 2,3,4,-2,-3 represent False or True in any way?

C)

True == 1 # gives True 

True == 2 # gives False, but

True and 2 # gives False.

D)

False or 2 # gives 2

2 or False # gives 2

Are the other integers like 2,3 etc taken as False? Why is it output in case D 2 and not True or False?

MSeifert
  • 145,886
  • 38
  • 333
  • 352
Zipbee
  • 11
  • 1
  • 1
    Could you please edit your question and make it readable? You can also mark text fragments as code, so that these stand out visually. – C-Otto Jun 09 '17 at 10:21
  • 1
    This question will be closed, because it is unclear/too broad in it's current form. If you ask one single clear question, then it can be marked with the appropriate duplicate – Chris_Rands Jun 09 '17 at 11:14

1 Answers1

2

and and or are boolean operators and their behaviour is explained in the 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.

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

If these were functions they would look like (roughly) like this:

def and_function(x, y):
    if bool(x) == False:  # if x is false, its value is returned
        return x
    else:                 # otherwise, y is evaluated and the resulting value is returned
        return y     

def or_function(x, y):
    if bool(x) == True:  # if x is true, its value is returned
        return x     
    else:                # otherwise, y is evaluated and the resulting value is returned.
        return y     

So these will always evaluate to either x or y, depending on the truth-value of the left side.

In this context "evaluate" refers to the expression not the boolean value. So x or somefunction() wouldn't evaluate the somefunction() if bool(x) == True. That's where the function equivalent fails because these functions don't postpone the evaluation of the expression. So don't take these two functions too seriously. They were only meant as explanation helpers.


The truth-value testing documentation explains how the truth-value of numbers is defined:

[...] The following values are considered false:

  • False

  • zero of any numeric type, for example, 0, 0.0, 0j.

  • [...]

All other values are considered true — so objects of many types are always true.


Then you asked about equality between the booleans True and False and numbers. To understand this you have to know that bools are a subclass of integers in Python. In some sense True is just another name for 1 and False for 0. So it's not surprising that True == 1 and False == 0 are both True.

NB: If you come from other languages it might be important to state that there is no implicit casting for comparisons in Python, I explained this in another answer in more details: "Why does … == True return False?".

Community
  • 1
  • 1
MSeifert
  • 145,886
  • 38
  • 333
  • 352