booleans

A forum for general discussion of the Python programming language.

booleans

Postby slala2121 » Tue Jan 28, 2014 6:04 pm

Can someone please explain why False or False returns False?
slala2121
 
Posts: 1
Joined: Tue Jan 28, 2014 6:02 pm

Re: booleans

Postby micseydel » Tue Jan 28, 2014 6:20 pm

If someone said "can you perform X action OR can you perform Y action?" and both of those things were False, you'd still say False to the whole of it, right? I'm not sure what you want in terms of an answer to this question. It seems pretty intuitive when compared to natural language. There's also the definition, which includes a truth table that you might find useful.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1391
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: booleans

Postby ochichinyezaboombwa » Tue Jan 28, 2014 9:34 pm

Basically, because
Code: Select all
not (True and True or not False)
does the same.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: booleans

Postby micseydel » Tue Jan 28, 2014 9:42 pm

Here's another way to look at it. or is a reserved word in Python for a binary (as in two operands) operator. It could be re-written as a function like this
Code: Select all
def or_function(first, second):
    if first:
        return first
    else:
        return second

And then and would be this
Code: Select all
def and_function(first, second):
    if not first:
        return first
    else:
        return second

Note however that these functions aren't exactly the same as regular Python code, since second using the operator would cease to be evaluated, but for this function it must be evaluated before being passed to the function. This is important if the second expression either (1) has side effects or (2) is an expensive operation. This is called short circuit logic meaning that a shortcut will be taken and ignore the second expression when possible.

And here are some truth tables because why not
Code: Select all
>>> possibilities = ((0, 0), (0, 1), (1, 0), (1, 1))
>>> for first, second in possibilities:
   print first, second, '|', or_function(first, second), first or second

   
0 0 | 0 0
0 1 | 1 1
1 0 | 1 1
1 1 | 1 1
>>>
>>> for first, second in possibilities:
   print first, second, '|', and_function(first, second), first and second

   
0 0 | 0 0
0 1 | 0 0
1 0 | 0 0
1 1 | 1 1
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1391
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: booleans

Postby workhorse » Thu Jan 30, 2014 2:36 am

Basic propositional logic. i.e

False or True will return True
True or False will return True
False or False will return False


It's one thing or the other.
workhorse
 
Posts: 5
Joined: Mon Jan 13, 2014 10:35 pm


Return to General Discussions

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron