The bool type is used for logical and boolean values and expressions. Other types may also be used to express true and false values.
- [Instructor] The bool type is used for logical values and expressions. Here in Komodo I've opened a working copy of types.py from chapter three of the exercise files, and if I save this and run it, you'll see I get x is 7 and class int. And so, that's coming from that line five, which prints the value of x, and line six, which prints the type of x. Here in line four I assign a value to x and if I assign it the special literal True with a capital T, you'll see when I run it that the class is bool, and the representation in the string is the word True.
And likewise if I use False here, I also get the class is bool. So the bool type is for logical values and expressions. For example here, if I say seven is greater than five, I'll get a bool type True. And I'll save and run that, x is True. This is because the comparison operator has returned True or False in the bool type.
It's worth noting at this point and I have a reason for this, we're going to use this and you need to understand that there is a special type, None, and it's the None type, if I save this and run it, you see the value is None and the class is NoneType. The NoneType is used, and it has only one possible value, which is None and this is used to represent the absence of a value, and there's a reason I am showing you this at this point, if I come down here and I say, if x: print ("True"), else: print ("False"), and these are strings here, so when I save and run this, you'll notice that with the value of None, the logical test here in if, returns False.
So, it's worth noting that there are a few things that evaluate as false, any numeric zero, so if I save and run this, x is zero, class is int, and the test is False. Any empty string, so if I just try this, I have an empty string, class string, and it evaluates as false, so I put anything at all in that string is no longer false, so save and run this, and it's now true. And if a number is not zero, again, it'll return true, it'll evaluate as true.
So, like many languages, you can use any type as a logical value, as long as you know the rules. None evaluates as false, zero evaluates as false, and an empty string evaluates as false. Pretty much anything else, at least in the default types will evaluate as true. So the bool type has two possible values, true and false, and additionally, many other types may be used as logical expressions, and will be evaluated as false, if they're effectively zero or empty, otherwise they will be true.
We'll see other examples of this behavior, later in this course.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.