Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Types and Branching Booleans

henry duquesnay
henry duquesnay
6,976 Points

I found True/False with AND/OR works... different?

I played around in my workspace for a while with this.
Unless I totally spoofed, I think the video missed it with how True/False with AND/OR works.

OR: - ANY TRUE is in the "or" boolean string automatically makes it True. The reverse is True with AND = FALSE: Any False in the string of Boolean "Or" comparisons and the whole thing automatically turns to False.

Am i crazy or is this how it actually works?

If you have an 'if' statement with 'or' example:

if 5 == 5 or 6 == 7:
       print('condition passes') 

when using or if one of the condition is TRUE then python will execute the block of code. But when using 'and' both conditions have to be true for python to run the code.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

As mentioned in the Python docs:

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.

Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument.

Note that, in the case of OR, the first "truthy" result or the last result is return and, in the case of AND, the first "falsey" value is returned or the last item is returned"

>>> digits = [5, 3, 0, 2, 1]
# and: first falsely is digits[2]
>>> digits[0] and digits[1] and digits[2] and digits[3] and digits[4]
0
# or: first truthy is digits[0]
>>> digits[0] or digits[1] or digits[2] or digits[3] or digits[4]
5
# and: first falsey is "digits[0] == digits[1]"
>>> digits[0] == digits[1] and digits[2] == digits[3] and digits[4]
False
# or: last item returned digits[4]
>>> digits[0] == digits[1] or digits[2] == digits[3] or digits[4]
1
# or: last item returned
>>> False or False or False or "truthy"
'truthy'
# or: last item returned
>>> False or False or False or 0
0
# and: last item returned
>>> True and True and True and None  # no response since its "None"
>>> None == (True and True and True and None)
True

A good use of this feature is setting default values if not passed in. Especially if the default is an empty list!

def some_func(arg=None):
    # if arg not set, set it to empty set
    arg = arg or []

Do do not want to use def some_func(arg=[]): since the [] is mutable, it's contents is not cleared on subsequent calls:

>>> def some_func(arg=[]):
...     arg.append(arg[-1:] or 'x')
...     return arg
... 
>>> some_func()
['x']
>>> some_func()
['x', ['x']]
>>> some_func()
['x', ['x'], [['x']]]
>>> some_func()
['x', ['x'], [['x']], [[['x']]]]

Post back if you need more help. Good luck!!