×
By the end of this chapter, you should be able to:
==
and is
in PythonAn essential part of writing programs is being able to execute code that depends on certain conditions. There are many different examples when you'd want to conditionally execute code. Here are just a few:
And so on, and so on. It's very hard to write any kind of interesting software without making use of conditionals and boolean logic.
So let's talk about how to write conditional logic in Python. To do so, we'll make use of booleans (True
and False
), along with if
statements.
Like many other programming languages, Python has conditionals with if/else
statements. One difference with Python, however, is that it does not use the else if
construct for chaining multiple conditions together. Instead, Python uses the keyword elif
(short for else if
). Note also that Python does not require parenthesis around conditions, but each condition must end with a :
. If you forget the colon (a very common mistake when you're first learning Python!), you'll get a SyntaxError
. Change the value of the user
variable in the example below. Try to get each case to print to your console:
user = 'Elie' if user == 'Elie': print('Awesome!') elif user == 'Tom': print('Cool!') else: print('Nope!')
Python also allows you to use words like or
, and
, and not
for comparison
if 1 > 2 or 2 > 1: print("cool!") if 1 == 1 and 2 == 2: print("nice!") if not False: print("it is true!")
One nice thing about comparing numbers is that you can string inequalities together without using the and
keyword:
if 1 < 2 and 2 < 3: print("this is ok") if 1 < 2 < 3: print("this is better!")
Also, it's important to understand that the indentation in all of these examples matters tremendously. Indentation is how Python keeps track of what code should be executed conditionally, and which code should always be executed:
name = "Matt" if name == "Matt": print("Your name is Matt") print("Bye!")
In the example above, both messages will be printed. But if you change the name variable to something besides "Matt"
, the second print statement will still execute. Because of the indentation, Python knows that the last line is not part of the if
statement!
If you forget about indentation, Python will throw an error. Python enforces indentation fairly strongly, but if you make a mistake, it'll let you know!
name = "Matt" if name == "Matt": print("Your name is Matt") # IndentationError!
Python has quite a few falsey values (values that evaluate to False
when converted to a boolean). We can check whether a value is falsey by passing it into the bool
function! All of the following examples evaluate to false when converted to a boolean.
# False bool(False) # 0 bool(0) # None bool(None) # Empty string bool("") # Empty list bool([]) # Empty tuple bool(()) # Empty dictionary bool({}) # Empty set bool(set())
is
versus ==
Everything in Python is an object with an id. To see if two objects have the same id, you can use the is
operator. You can also inspect an object's id directly using the id
function.
is
operator (compares id)a = 1 b = a b is a # True b = 2 a is b # False id(a) # should give you a number
If you just want to check whether two objects have the same value, you can use the ==
comparator. In general, it's probably best to use ==
for comparison, unless you know that you're trying to determine whether two objects have the same id (i.e. they are the same object in memory).
list1 = [1, 2] list2 = [1, 2] list3 = list1 list1 is list3 # True list1 is list2 # False list1 == list3 # True list1 == list2 # True
Before we move on to the exercises, there's one more function you should know about. It is very common in command line programs to prompt the user for information. In Python, this function is called input
. Here's an example:
name = input("What is your name? ") # you can now type anything and it will be saved into the name variable
Since Python is very sensitive to indentation, there may be situations where you need to place indented code, but do not want to run anything. In this case, the pass
keyword must be used.
When you're ready, move on to Boolean Logic Exercises