From the course: Advanced Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Truth value testing

Truth value testing - Python Tutorial

From the course: Advanced Python

Start my 1-month free trial

Truth value testing

- [Narrator] In Python, any object can be tested for Boolean truth value. So, let's take a few moments now to learn about how that works. In general, any object is considered to be equivalent to Boolean true, unless it's class defines a Bool method that returns false, or has a len method that returns a zero length. So, let's look first at the built-in objects that evaluate to false. There are two constants which are defined to evaluate to false, the false Boolean constant itself, of course, and the none constant, which you may have seen represented in other languages as null. Any of the built-in numeric types that evaluate to zero are also considered to be false. For example, the integer value zero, the floating-point value 0.0, and the complex value type of 0j are all false values. In addition, the decimal object, when given a value of zero, and the fractional object, when it has a numerator of zero, are also false. Next are empty sets and sequences. And you can see the examples…

Contents