"If, else" statements
I'm sure you're familiar with the three most basic rules of traffic. If you see a green light, continue driving. If you see an orange light, slow down. If you see a red light, stop the car. You never want to drive aimlessly straight. Similarly, you never want to run your code in a top-down approach.
if-else
statements are used to enforce the "traffic rules" in your code.
By the end of this lesson, you will learn how to use the if-else
statement to control when certain parts of your code are executed.
How does it work?
The
if-else
statement is based on a condition.
The skeleton of an if-else
statement is as follows:
if (condition): //code executes if condition is True else: //code executes if condition is False
If condition
is True
, the code inside the if
statement is executed. Otherwise (else
), the code inside the else
statement is executed.
The if
keyword will always precede the condition and is followed by a colon :
. The else
statement is also followed by a colon :
.
let's look at a very simple example:
status = True if (status): print('The status is True') else: print('The status is False')
>> The status is True
Since our value status
is equal to the boolean True
, the if
statement will execute.
Setting the status
to false will execute the else
statement.
status = False if (status): print('The status is True') else: print('The status is False')
>> The status is False
Using the "in" operator
Recall the grocery_items
dictionary from earlier:
grocery_items = {'bananas': 2.99, 'apples': 1.29, 'papayas': 2.39}
Now, we want to execute code if and only if a specific item exists in grocery_items
. We will make use of the in
operator to do so:
item = 'brussel sprouts'
if (item in grocery_items):
print('The item exists')
else:
print('The item does not exist')
>> The item exists
Try changing the item to bananas
you should obtain a result of : The item exists
"else" statements are not mandatory
You could also completely remove the else statement:
item = 'brussel sprouts' if (item in grocery_items): print('The item exists')
>>
Since the item does not exist nothing runs.
Indentation in Python
Notice the indentation spacing before the print
keyword. When you press enter after the if
statement, it creates that indentation for you. Indentation is really important in Python because it indicates the scope that a block of code belongs to. In this case, the indentation makes it such that the code print('found the', item)
belongs to the if
statement.
Updating the dictionary to include the foreign item
We can actually take this one step further such that if the item does not exist in the dictionary, update the dictionary to include the foreign item. In the else
statement, add the lines
if item in grocery_items:
print('The item exists')
else:
print('The item does not exist')
grocery_items.update({item: 2.99})
print("just added the item: ", grocery_items)
>> The item does not exist
>> just added the item: {'bananas': 2.99, 'apples': 1.29, 'papayas': 2.39, 'brussel sprouts': 2.99}
If you re-run the exact same cell again, the condition should now evaluate to True
since the item was previously saved into the dictionary.