In many occasions we'll want our programs to do different things depending on the user's input. For example, if we have a menu. Depending on which menu item is clicked, we want to do one thing or another.


This post is part of #100DaysOfPython, check out yesterday's post here if you haven't already. Or go to the index of all 100 days.


Booleans

A Boolean is a type of data which can be True or False. Look at the following examples:

5 == 5  # True
'hi' == 'hi'  # True
5 < 6  # True

4 == 3  # False
'hi' == 'ha'  # False
5 < 4  # False

In Python, True and False are keywords which we can use to assign and compare.


my_number = 5
user_number = int(input("Enter a number: "))

print(my_number == user_number)

In the snippet above, we:

  1. Define a variable, my_number, with value 5.
  2. Ask the user for a number with the input function. The result of this function, we convert to a number using the int function.
  3. Finally, we print the result of comparing my_number with user_number.

A couple things to note:

The input function gives us a string. my_number is not a string (notice how there are no quotation marks around it). It's a number. In order to compare my_number and user_number, they both must be the same type of data. That is, both must be numbers or both must be strings.

Two equal signs, ==, is used to compare things. A single equal sign, =, is used to assign values to variables.

The int function

my_string = '5'
print(my_string * 3)

my_number = int(my_string)
print(my_number * 3)

What do you think the first print function would print? What about the second one?

Think of multiplication as adding multiple times. That is, '5' * 3 is the same as '5' + '5' + '5'. When we add strings together, we join them (or concatenate them). We'd end up with '555'.

my_number, however, is not a string. We use the int function to convert my_string to a number, and assign that to my_number. When we then print it multiplied by three, we do 5 + 5 + 5, which is 15.

If statements

Having a Boolean data type isn't useful unless we can do something with it. Introducing: the if statement!

user_command = input("Enter 'g' to greet: ")

if user_command == 'g':
    print('Hello, world!')

In this case, we get the user to type 'g' if they want us to greet them.

Then, we have our if statement, always in this format:

if <boolean>:
    <do something>

Notice how once again there's indentation on the lines that will be executed if the <boolean> is True.

You'll find indentation underneath every colon (:), because colons are used in Python to signify the start of a block.

Multiple booleans together

You can use and and or in Boolean expressions. For example:

user_command = input("Enter 'g' to greet: ")
security_question = int(input('What is 5 * 5? '))

if user_command == 'g' and security_question == 25:
    print('Hello, world!')

In that case, Hello, world! would only be printed if they both typed g and 25. Failure to do either would result in nothing being printed out.

We can also do either of them, by replacing and with or. If we do that, we would print something out if either is True:

user_command = input("Enter 'g' to greet: ")
security_question = int(input('What is 5 * 5? '))

if user_command == 'g' or security_question == 25:
    print('Hello, world!')

This has been an introduction to Boolean values and flow control in your Python programs. This makes our programs much more powerful, and stops them from being overly static. Happy coding!

I'll see you on the next one!