4. If Not Python, Then What?
Updated: Jan 15, 2021
The video tutorial for this article can be found at:
In this tutorial we will learn how to use the if-else conditional statement. Along with variables, conditional statements are likely the most basic and important concepts of programming. This is how you check for whether a certain condition is met and then based on that, decide the course of action.
To explain this, let's consider this scenario. Say you want to tell python what item you want to take out depending on the weather. We have the choice of umbrella and sunglasses. Here is the scenarios:
If it rains, take an umbrella with you and not the sunglasses.
If it is sunny, take sunglasses with you and not the umbrella.
We will consider two boolean variables for weather. A boolean variable is one that can have a value of either True or False. You can think of it as a switch which must have one of the two states, on or off. We set them to False by default in this tutorial:
rain = False sunny = False
Since we have two items, we have two more booleans representing items:
umbrella = False sunglasses = False
Now we have to write code that sets the value of the item variables based on the value of the weather. If you read the code carefully, it is very intuitive. In more natural language, we are saying:
if (weather is rain): set umbrella to True as we will need it but if (weather is sunny): set sunglasses to True as well need a pair
To translate it for python, here is what we code:
if (rain == True): umbrella = True elif (sunny == True): sunglasses = True
Let's dissect this. The if statement is started with if (duh!), followed by the condition you are looking for in parenthesis, and then to tell python what to do if that condition is met, you type ':' and then in the next line, with indentation, write the code to be executed.
if (condition to look for and if met): do this
Next notice 'elif', which is basically "else if" shortened. If the first condition is met, the code for that condition is run but if it is not met, then elif tells python what condition next to look for. Important to note that if the first condition is met, elif will NOT be checked and the rest of the code will not be run.
Also notice the use of the equal to sign. To set a value of a variable, we use one sign "=" but to check whether a variable value is set to a certain value or not. So writing:
rain = True
will set the value of the variable rain to True. But writing
rain == True
will check if the value of weather is 'rain' or not.
But what if I want two conditions?
Just like in natural language, you can you 'and' and 'or' with the conditional statements in python. So for example:
if (weather is rain and weather is sunny): set umbrella to true set sunglasses to true
This can be coded as:
if (rain == True and sunny == True): umbrella = True sunglasses = True
Now let's say you plan on taking the umbrella weather it is sunny or raining, in any of that case you want to take the umbrella out. Here comes the 'or' statement.
if (either raining or sunny): set umbrella to True
if (rain == True or sunny == True): umbrella = True
If this, then if that, then do this!
We will talk about if statement with-it an if statement. So imagine you want to take both umbrella and glasses out if its raining and is sunny at the same time. You also want to take the umbrella out if it's either raining or is sunny. Lastly, if it's sunny and it's not raining, you want to take the glasses out but not the umbrella. So:
If (raining and sunny): set both umbrella and glasses to True else if (either raining or sunny): set umbrella to True, but also check if it's sunny then set glasses to True
Translating that to python:
if (rain == True and sunny == True): umbrella = True glasses = True elif (sunny == True or rain == True): umbrella = True if (sunny == True): glasses = True
What if none of the conditions is met?
You might want python to do something is none of the specified conditions are met or is any condition other than the specified ones is met, you may want it to do something. This is where 'else' comes if. So in natural descriptive form:
if (condition 1): action 1: else: action in case of any condition other than condition 1
So in our example, what is its neither raining nor sunny, say when it's cloudy. You don't want any of the item. This is where the 'else' statement comes in. Basically it tells python what to do in case none of the conditions specified are met, which in out case would mean what to do if both rain and sunny are False.
So in python, we can write the following code:
if (rain == True and sunny == True): umbrella = True glasses = True elif (sunny == True or rain == True): umbrella = True if (sunny == True): glasses = True else: umbrella = False glasses = False
It is important to remember that python will run the code in sequence. If the first condition is met, it will run the code for the first condition and will skip the rest of the code. So even though you may be covering all the conditions, they may not be getting checked as if a certain condition is met, python with not check the conditions after that. This can be overcome by right separate if statements or if statement with in an if statement like we did.
Now boolean variables can be written in a simpler way. For True, you replace "rain == True" with "if rain:", and for "rain == False", you can write "if not rain:". So our code becomes:
if (rain and sunny): umbrella = True glasses = True elif (sunny or rain): umbrella = True if (sunny == True): glasses = True else: umbrella = False glasses = False