In this lesson we will learn how to control the flow of your code, and a new datatype.
The if
statement allows you to run code only if a certain condition is met. Here’s an example:
canDrive = True
if canDrive == True:
print("You can drive!")
This has several new things here, so let’s break them down:
canDrive = True
: this is a new datatype called a boolean. It can have two states: True
and False
. Note that True
and False
should not be in quotes, since they aren’t a string.
if canDrive == True
: This has 4 main parts: The if
statement itself, the first item to compare (in this case canDrive
), how to compare it, and the second value to compare (in this case True
).
print("You can drive!")
: This is the code to run if the above statement meets its criteria. Notice that it is indented, this is important.
Altogether, the code does this:
- Sets the variable
canDrive
toTrue
- Checks “Is
canDrive
equal toTrue
” - If so, it prints
You can drive!
If you run this code, it will output You can drive
. If you change the canDrive
variable to False
, it won’t output anything. This is not a bug, the if
statement just didn’t meet its criteria.
Let’s go back to that ==
part. This is a comparison operator, it compares two values on either side of it. This one will allow the if
statement to run only if the two values are equal. Here’s a list of comparison operators:
==
: The values must be identical
!=
: The opposite of ==
; The values must not be identical
>
: The value on the left must be greater than the value on the right
>=
: The value on the left must be greater than or equal to the value on the right
<
: The value on the left must be less than the value on the right
<=
: The value on the left must be less than or equal to the value on the right
Here’s another way to use the if
statement, using the >=
comparison operator:
age = 25
if age >= 18:
print("You can vote!")
A couple things to keep in mind:
- You can’t directly compare values of different types with each other. For example, you can’t compare
"18" == 18
, because they are two different types. But you can compareint("18") == 18
, sinceint()
converts it. - You can use
>
,>=
,<
, and<=
on strings, but it will compare them alphabetically. For example,"apple" <= "banana"
will succeed, becauseapple
comes beforebanana
in the alphabet.
Now, let’s say we want a different output if the check fails, we can use the else
statement. The else
statement will only run if the above if
statement’s check doesn’t meet its criteria. Here’s an example:
age = 15
if age >= 16:
print("You can get a license!")
else:
print("Sorry, you can't get a license.")
The second print line will only run if the age is less than 16.
But what if we want to run a second if
statement only if
the first one fails. We can use the elif
statement!
Here’s an example:
money = 15.00
if money >= 15.00:
print("You can afford the item!")
elif money >= 14.00:
print("Just a little more money and you'll be able to buy the item!")
else:
print("Sorry, you can't afford this item.")
Notice how you can use if
, elif
, and else
statements alongside each other. That allows some pretty complex workflows.
Of course, if
statements aren’t very useful unless the code runs differently each time. That’s where the input()
function comes in handy. Here’s an example:
age = int(input("How old are you?"))
if age >= 21:
print("You can drink!")
else:
print("Sorry, you can't drink.")
This code will run differently depending on whether you input an age higher or lower than 21.