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.
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
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:
- Define a variable,
my_number, with value
- Ask the user for a number with the
inputfunction. The result of this function, we convert to a number using the
- Finally, we print the result of comparing
A couple things to note:
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
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.
my_string = '5' print(my_string * 3) my_number = int(my_string) print(my_number * 3)
What do you think the first
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
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
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
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
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
25. Failure to do either would result in nothing being printed out.
We can also do either of them, by replacing
or. If we do that, we would print something out if either is
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!