Many times, in programming, we need our programs to execute a particular part of code depending upon certain conditions. This is called conditional execution. The function is mostly performed using If, Ifelse and Elif in python.
This article conditional Execution using If, Else in Python is a part of our Python learnings as from the below list.
- Introduction to Python
- Conditional Execution using If, Ifelse and Elif in Python
We are looking to add more and more leaning articles soon!
Example of If, Ifelse and Elif in Python
For example, suppose a company wishes to give a bonus to an employee who has worked for more than 5 years and only salary to someone has worked for less than 5 years. If we are writing a code for this we would need a condition to be checked (term of an employee) and perform a different action if that condition satisfies and a different action if it doesn’t.
In python, we can perform such conditional execution using conditional statements of if and else.
Conditional Statements in Python are:
- if statement
- if – else statement
- elif statement
if statement in python
The if statement in python is used to check a particular condition. If the condition is satisfied, a specific block of code runs. If the condition is not satisfied, that specific block of code doesn’t run and we do not receive output.
#If statement example
x = 3 if x > 2: print('x is greater than 2') x is greater than 2 #Output
Here, we can see that we specify the condition after the if keyword and end the statement with a colon character (:) and the following lines of code are indented.
x = 1 if x > 2: print('x is greater than 2')
Here, in this case, we do not receive any output since the if condition is not satisfied.
We can overcome this problem by using another control statement.
if-else statement in Python
Since we do not receive an output in the if statement if the condition is not satisfied, we use else keyword which runs another block of code when the condition is not met and gives us the required output. Therefore, here we receive an output when the condition is met and also when the condition is not met.
#If else statement example #To check for whether a number is positive or negative x = 2 if x > 0: print("It is a positive number") else: print("It is a negative number") It is a positive number #Output
x = -2 if x > 0: print("It is a positive number") else: print("It is a negative number") It is a negative number # Output
Here, we can see that we get an output even when the if condition is not met.
However, there is an issue with the else part since it gives us an output when the condition is not met. The output that we receive sometimes is not valid.
x = 0 if x > 0: print("It is a positive number") else: print("It is a negative number") It is a negative number # Output
Here, we can see that our if condition is not satisfied but the number entered is 0, so we require to add more condition to make it more apt for the examples like these.. Therefore, else in python check any for a single condition and gives us an output when the condition is not satisfied. This is why our next topic is elif statements.
Learn: The best python courses available online
elif statement in Python
The elif statement helps us to write multiple if conditions and the specific block of code is executed where the condition is met. We can have any number of elif statements in our program however, elif statements are optional.
Suppose, we want to modify the above example and write one more condition to identify zero, we can do it as follows:
Elif statement example
x = 0 if x > 0: print("It is a positive number") elif x == 0: print("It is a zero") else: print("It is a negative number") It is a zero # Output
This block of code separately identifies zero and hence we can get proper output for any number.
Feel free to ask any query on the Python forums.