Python: Booleans

What are they?

​Booleans are a very abstract concept. In simple terms, booleans have two states. True and False, or 1 and 0. True is 1, and False is 0. Usually, 1 and 0 are only used in numeric contexts, but we will learn about those later. For now, focus on the two states of a boolean.

Defining Booleans:

Next, we will learn how to define booleans. Defining booleans is very simple: the variable will either equal True or False. Make sure that the you capitalize True and False. Otherwise, the won’t turn blue like you see in the example below, which means that the repl.it interpreter doesn't recognize it.

Why do we use booleans?

Why are booleans important? They are used to see if something is true or false. You probably won’t use booleans at a very basic or a very simple coding level, but as you begin to learn more and code more complicated things, booleans become very important. For example, if-statements, for-loops, and while-loops all rely on booleans. (You haven’t learned these yet.) For these complicated applications of booleans, you can think of True as yes and False as no. In the screenshot below, you will see code that checks certain conditions of string. The formatting and declaration of a string should be a refresher for you! Review strings if you need to, as we will be using a lot of different applications of Strings later on in this Python course.

Also, isalpha() and isdigit() are things that you have not learned yet. If you don’t understand what they are, that fine! Just make sure that you understand what you have learned so far in this introduction.The first print line (line 2) prints the result of a method that checks if the string is in alphabetical order. In this case, it returns True.The second print line will print the result of a method that checks if the string is made up of digits. This string is made of characters, so this will print False.

Also, note that the results that are printed are always capitalized. This is because booleans are always “True” or “False”, not “true” or “false”. Make sure that you remember this!