Introduction to If Else in Python
You already know that a program is a series of written instructions. To no one’s surprise, there can be so many instances where a program may have to take a decision before proceeding. For example, deciding if the user is old enough to attend the exam or if the temperature is high enough to start the air conditioner or selecting the grade a student has passed with, etc. These decisions are made based on the input conditions and by looking at what to do in that situation.
When a condition statement is used, the program decides to run a particular code block depending on the input and the conditions. Just like any other fully-featured programming language, Python supports multiple ways of decision making. If else is one of the most used ways to get the job done.
Other decision-making statements in Python are the following:
- If Statement: It is used to analyze if the condition at hand is true or false. The code block below it is only executed when the condition is met.
- If Else Statement: This statement is similar to the If statement but it adds another block of code which is executed when the conditions are not met. In this article, we will take a look at this statement type and its example.
- Nested If: In situations when we have to check more than one condition and execute instructions, nested if it is used.
The Flowchart of an If Else Statement
As you can see in the flowchart above, the condition in an if-else statement creates two paths for the program to go on. If the condition is not met, the code below it is not executed and the program executes statement in the Else block.
On the other hand, when the “if” condition is met, only then if a block of code is executed and the program then jumps below exiting the If else statement.
The Syntax of an If Else
The syntax of an If Else Statement is the following:
# statements to execute when the conditions are met are inserted here
# Statements to be executed when the conditions are not met.
4.8 (4,355 ratings)
As you can see above, all if-else conditions have two statements and a condition written. Before the statements, the condition is made clear, once the condition has been processed, the program takes a look at the input and decides if it fulfills the conditions. If it does, statements in the first block are executed and the program skips the statements in the “else:” section of the syntax.
And if the condition is not met, the program skips the first block and executed statements in the “else:” block.
Examples of If Else Conditional Programming
Now that we have seen the syntax, flowchart, and need of if else statements, let’s take a look at some practical examples to see it in action:
Example – 1
A Python Program to check if the input number is even or odd.
number = int(input(" Please enter the number to check : "))
if number %2 == 0:
print(" The input number is even ")
print(" The input number is odd ")
Example – 2
A Python Program to check whether the applicant is eligible to vote in the elections or not :
age = int (input(" Please enter the age of applicant: "))
if age>=18 :
print(" The applicant is eligible to vote in the elections " );
print(" The applicant is not eligible to vote in the elections " );
Python Program to check the input text for right or wrong answer :
code = input(" What is the capital of Japan? ")
if code == 'Tokyo':
print("Congratulations ! You passed the test.")
print(" Oops, the answer you entered is wrong.")
print(" Thanks for participating. “)
Conclusion – If Else Statement in Python
Decision making is one of the core pillars of programming. Being able to lay down proper conditional statements is not only necessary to get good at programming, but it is also essential to get things done more times than often. Armed with the knowledge of conditional statements such as if, if-else and nested if, you will be able to logically make decisions with the program and get correct results.
This is a guide to If Else Statement in Python. Here we discuss an introduction, flow chart, syntax, and examples of if else Statement in Python with some sample code. You can also go through our other suggested articles to learn more