Move from Excel to Python with Pandas Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: Truthiness

Login or purchase this course to watch this video and the rest of the course contents.
0:03 Any interesting program has conditional tests and various branching control structures in it.
0:09 And many of these control structures you have to pass some kind of test, a boolean, a True or False value.
0:16 Go down this path, or don't. Continue looping through this loop or stop. Let's talk for a moment about this general idea of True and False in Python;
0:23 and I am referring to it as truthiness, because in Python all objects are imbued with either a True value or a False value.
0:31 And the easiest way to understand this is to think of the list of things that are False,
0:35 they are clearly spelled out, it's right here- False, the keyword False, the boolean keyword False is false obviously.
0:41 But things that might not be so obvious to that are False, are as well, for example any empty sequence,
0:47 so an empty list, an empty dictionary, an empty set, empty strings. All of these things are False, even though they point to a real life object.
0:56 We also have the zero values being False, so integer zero and floating point zero - False.
1:03 Finally, if you have some kind of pointer and it points to nothing, so the keyword none, that is also considered to be False.
1:10 Now, there is this small addition where you can overwrite certain methods in your custom types to define False,
1:16 but outside of this list, and those implementations, everything else is true.
1:20 So if it's not in this list and it's not a custom implementation of a magic method
1:24 that describes the truthiness of an object, you pretty much know the way it works.
1:28 Now, in Python, we often leverage this truthiness or falseness of objects, so we might do an "if" test just on a list to see if it's empty,
1:38 rather than testing for the length of the list to be greater than zero, things like that. So you'll run into this all the time
1:44 and it's really important to keep in mind what's True and what's False.

Talk Python's Mastodon Michael Kennedy's Mastodon