Previous Lesson | Table of Contents | Next Lesson |
Suppose we are trying to write a division function that accepts only integers.
We have written out our division algorithm, but regardless of what it is, we know
it cannot be executed when the divisor is 0
. Thus, we should execute the algorithm
only when the divisor is not 0
.
In order to write our code so that it only executes when the divisor is non-zero,
we need to use something called conditionals. These are statements that are
checked for being True
before executing a given block of code. Here is a
basic example:
x = 2
if x == 2:
print("Hello!")
In this case, we will output:
Hello!
To create a basic conditional, we write the keyword if
followed by the
condition that we want to check. In this case, we want to check if is x
is equal to 2
, as denoted by the operator ==
. We then terminate the
line with a colon.
Afterwards, all code that we want to exclusively execute given that the
condition is True
is written underneath, with each line tabbed in at
least once. In this case, it is to print the phrase, "Hello!"
. Suppose
we had the following code instead:
x = 3
if x == 2:
print("Hello!")
In this case, nothing would have been printed out because x == 2
is now False
.
Suppose that we want to execute another block exclusively if the condition we
are checking is in fact False
, we can use another keyword, else
:
x = 3
if x == 2:
print("Hello!")
else:
print("Hi!")
In this case, we will output:
Hi!
As with the if
statement, any code exclusively executed for else
must be
tabbed in at least once. Notice that we print out "Hi!"
because the if
condition is False
, so we execute the code under the else
block.
What if we want to check more than two conditions? We can use another keyword elif
:
x = 3
if x == 2:
print("Hello!")
elif x == 3:
print("Hey!")
else:
print("Hi!")
In this case, we will output:
Hey!
In this case, Python checks if x == 2
. That statement is False
, so we move
on to the elif
statement. This statement is True
, so we execute the code
under that block (tabbed in as with if
and else
). Note that the else
block is only executed when all other statements (if
and elif
) are False
.
It should also be noted that the else
block is entirely optional. Sometimes
the only thing we want to do exclusively if all the other statements are
False
is nothing. :)
We can have as many conditions as we like by including more elif
statements:
x = 3
if x == 2:
print("Hello!")
elif x - 1 == 2:
print("Howdy!")
elif x == 3:
print("Hey!")
else:
print("Hi!")
In this case, we will output:
Howdy!
Several observations can be made here:
All the
elif
conditions must be placed in-between theif
andelse
conditions.Python checks conditional statements in order from top to bottom. Once one of the statements is
True
, Python will not check any of the other conditionals. In this case, the first conditional that isTrue
isx - 1 == 2
, which is why we print"Howdy!"
and not"Hey!"
, even though that conditional is alsoTrue
.By “not check any of the other conditionals,” we mean any subsequent
elif
conditionals or theelse
block. If we write anotherif
conditional later on, Python will resume checking conditionals:x = 3 if x == 2: print("Hello!") elif x - 1 == 2: print("Howdy!") elif x == 3: print("Hey!") else: print("Hi!") if x == 3: print("Done!")
In this case, we will output:
Howdy!
Done!
Conditionals open up a whole new world of possibility for our code, especially
since you can write as many elif
statements as you like. That being said,
it also introduces a lot of complexity in our writing, so we’ll stop for now
and practice what we have learned:
Create a variable
x
. Then write code such that we print"Success"
only whenx
is equal to"foo"
. (solution)Add two
elif
statements so that we print"Okay"
ifx
is equal to"car"
, and we print"Nice!"
if the length ofx
is equal to3
. Note that the"car"
conditional should come first. (solution)Add an
else
conditional so that we print"Oops!"
if all of the other conditionals from above failed. (solution)Add a new
if
conditional subsequent to that firstif
-else
code block that prints"Cow"
if the length ofx
is equal5
. (solution)Change your code so that
x
is initialized toMouse
. What will be output? (solution)
This is not an easy concept to master initially, so definitely practicing (multiple times) the syntax that has been taught here will be extremely important in order to become comfortable with this new functionality of ours.
When you’re feeling comfortable, feel free to move on to for loops!