In this section, we will learn what if-else statements are and how to use them in Python.
Conditional Statement in Python
Conditional statements in Python are those statements that their execution depends on a condition to be True! For example, loops like `while` and `for`, or if statement, which is the discussion of this section. These are basically a block of code with a condition on top of it. Now if the condition (which is simply an expression that results a boolean value) was True, then the body (the instructions) will run, otherwise they will be skipped.
Python if Statement
The if statement in Python is used to create a block of instructions and set a condition on top of it. If the condition of the block resulted True, then the body of the if statement will run! Otherwise, if the result of the condition was False, then the body is skipped.
Note: The use of if statement is pretty much the same as what we use it in real life. For example, we say if the weather was good, then I’ll go for a vacation etc. Otherwise I won’t go!
Python if Statement Syntax:
Here’s the structure of the if statement:
if condition: #body…
`if`: in order to create an if statement, we start with the keyword `if`.
`condition`: after the keyword `if` comes the condition of this statement. Here we need to put an expression that result a boolean value (Either True or False). If the result of the condition was True, then the instructions within the body of the `if` statement will run, otherwise this body will be skipped.
`:`: the colon after the condition defines the beginning of the body of the if statement. After the colon, any indented line below the if statement will be part of the body of the if statement.
if in Python Example:
li = [1,2,3,4,5,6,7] if 4 in li: print("Yes, the value 4 is in the body of the list")
Output:
Yes, the value 4 is in the body of the list
Here, the condition of the `if` statement is to check and see if the target list has the value 4 as part of its members. So because the target list has this value, then the result of the condition is True and so the body of the if statement which is only one call to the print() function will be executed.
if not in Python Example:
li = [1,2,3,4,5,6,7] if 2 not in li: print("Yes, the value 2 is not in the body of the list")
Output:
Here, the condition of the `if` statement is to check and see if the target list has not the value 2 as part of its members! But because the list has this value, then the final result of the condition becomes False and so the body of the `if` statement won’t run (Is skipped and the execution engine will move to the instructions after this statement which for this example there’s none).
Python else Statement
When working with the `if` statement, there’s a chance that the condition of the `if` statement might result False and the body of this statement will be skipped as a result! In a situation like this, we might want to run another set of instructions (consider it as the plan B).
This is where we can use the else statement.
The else statement, which comes after an if statement, represents a block that we can put instructions in it and it will only run if the condition of its related `if` statement resulted False!
Basically, if the `if` statement resulted True, then its body will execute and the body of the related `else` statement will be ignored. But if the condition resulted `False`, then the body of the `else` statement will run instead.
Python else Statement Syntax:
if condition: #body... else: #body…
`else`: in order to create an else statement, we start with the keyword `else`. Note that the `else` statement comes right after the body of an `if` statement. This means there should be one if statement first and then comes the `else` statement.
`:`: the colon `:` after the `else` keyword declares the beginning of the body of the `else` statement. Now the indented lines below the `else` statement represent the body of the `else`.
Note: the use of `else` statement is optional for an `if` statement.
Also remember that we can only have one `else` statement per `if` statement.
Note that either the body of the `if` statement will run or the body of the `else`; But not both!
Example: using if else statement in python
li = [1,2,3,4,5,6,7] if 2 not in li: print("Yes, the value 2 is not in the body of the list") else: print("No, the list has the value 2 as well")
Output:
No, the list has the value 2 as well
In this example, the condition of the `if` statement is False! For this reason, the body of this statement is skipped and so the body of the `else` statement ran instead.
elif Python Statement
Here’s the thing: Let’s say you have an `if` statement and its condition resulted False! But before jumping to an else statement, you still have other conditions to check! Basically, there are cases where we have multiple conditions to check and for each one of them we might have a block of code to run if they result True!
This is where we can use the `elif` statement!
The `elif` statement is just like the `if` statement, except they come after the body of an `if` statement.
Each `elif` statement has a condition as well as a block to put instructions there.
Just like the `if` statement, first the condition of an `elif` statement will be checked and if it resulted True, then the body of the `elif` statement will run.
Note:
- As mentioned before, an `elif` statement comes after an `if` statement. This means first the condition of an `if` statement will be checked and only if the condition resulted False, then the condition of the `elif` statement will be checked.
- An if statement can have as many `elif` statement as needed. The use of `elif` statement is optional and there can be none.
- If there’s an `else` statement as well, it will be put as the last statement in the series of `if` and `elif` statements.
Python elif Statement Syntax:
if condition: #body… elif condition: #body… elif condition: #body… else: #body…
Example: Python if elif else
val = 100 if val >500: print("The value of the val is higher than the value 500") elif val >200: print("The value of the val is higher than the value 200") elif val < 50: print("The value of the val is less then the value 50") else: print("I don't know about the value of the val variable")
Output:
I don’t know about the value of the val variable
In this example, none of the conditions for the `if` and `elif` statements resulted True and so the body of the `else` statement ran as a result.
Python if Statement Multiple Conditions
Using logical operators like `and` and `or` we can put multiple conditions for an if statement as well.
Example: if statement with multiple conditions in python
val = 200 if val <400 and val >100: print("The value of the val variable is higher than 100 but less than the value 400")
Output:
The value of the val variable is higher than 100 but less than the value 400