First off how do you pronounce this word? if you have never worked in a programming environment you may have never heard it spoken aloud. It sounds a little like bouillon in beef bouillon cubes.
It is shortened to “bool” in Python but means the same thing. It is a very simple concept that can have some confusing details. Bool describes a type of data that can have one of two possible values, True or False.
Bool Numbers
For a numbers bool returns False when the number is zero and True for all other numbers. You can test things back in the IDLE if you wish.
>>>print(bool(0))
False>>>print(bool(1))
True
>>>print(bool(123456789))
True
Bool Strings
For strings bool returns False when there s no value. Do not confuse this with a blank space (” “). A blank space is something. We are testing for something or nothing.
>>>print(bool())False
>>>print(bool(None))
False
>>>print(bool(” “))
True
Bool Lists, Maps, Tuples
For bool lists, maps and tuples it is very similar to strings. If they contain no data then they return False and if they contain any data they return True.
>>>my_list = []
>>>print(bool(my_list))
False
>>>my_list = [‘a’,’b’,’c’]
>>>print(bool(my_list))
True
How to Use
Now that you know this information how do you use it? You could use it to see if a user has entered data or still needs to enter data.
my_list = [] if bool(my_list) == False: print("You need to enter some information.") else: print("You have successfully enter some information")
Boolean is easy but a little confusing in the details. That is why I dedicated a page to it. Good luck and keep going!