In Python, conditional statements are used to make decisions based on conditions. They execute the different blocks of code depending on whether a condition is True or False.
In this chapter, you will learn different type of conditional (if else) statements, how to use them to make decisions in your programs.
Python conditional (if else) statements are used to make decisions in a program. They allow the program to check a condition and choose what to do next based on whether the condition is True or False. If the condition is True, the code inside the if block runs. If it is False, the code inside the else block runs. This helps control the flow of the program and makes it behave differently in different situations.
Python provides support for the following types of conditional statements:
Let us discuss these conditional statements in detail.
Programs execute the code inside the if block when the condition evaluates as True.

This represents the simplest decision-making construct. Programs can determine dynamic responses through the if statement because it activates specific actions based on defined conditions.
It has the following syntax:
Let us consider a simple example showing the implementation of if statement in Python:
Output:
You are eligible to vote.
Explanation:
In this example, the variable age is set to 18. The if statement checks whether the age is greater than or equal to 18. Since the condition is True, the message "You are eligible to vote." is printed.
Let us now consider another example, where the program will return a message according to the entered age of the user:
Output:
# Output 1: Enter your age: 20 You are eligible to vote. # Output 2: Enter your age: 65 You are eligible to vote. You are allowed to consume alcohol in some countries. You are eligible for senior citizen benefits.
Explanation:
In this example, the program takes the user's age as input. Each if statement checks a different condition based on the age. All conditions are checked independently, so multiple messages can be printed depending on the age entered.
Programming contains the if…else statement as its core element for making decisions in code execution. One block of code executes through the if statement when conditions prove true, but a different block activates with conditions evaluated false.

Different actions will follow according to different conditions because of this structure. The if block examines a condition while true results activating the block of code; otherwise, the else block executes.
The structure of if…else statement works as a valuable tool for managing two possible results from a condition which enhances the program flexibility and responsiveness. A programming language requires either indentation or brackets to display code blocks in a clear manner.
Here is the syntax for the if...else statement.
Let us a simple example showing the implementation of the if...else statement in Python:
Output:
# Output 1: Enter your age: 20 You are eligible to vote. # Output 2: Enter your age: 16 You are not eligible to vote.
Explanation:
In this example, the program takes the user's age as input. The if statement checks if the age is 18 or more. If the condition is True, it prints that the user is eligible to vote; otherwise, the else block runs and prints that the user is not eligible.
A nested if…else statement places an if…else block within both if statements and else statements. A program is then able to perform advanced decision-making through this structure by allowing multiple tests of conditions.

The first step evaluates the outer if condition. The nested control block executes the if or else sections depending on newly introduced conditions when the first if condition returns true. The block serves efficient decision-making operations that use various conditions.
In Python, the correct indentation stands as the main indicator of nested blocks although other coding languages use curly braces {} to define these structures. The if…else block within an if…else block simplifies execution in applications involving user authentication and grading systems and order processing.
The use of conditional blocks requires caution since their excessive application can generate code that becomes unreadable but also remain difficult to maintain code clarity and code efficiency.
Here is the syntax for a nested if...else statement.
Programs can check multiple conditions in an organized order through a nesting structure of if else statements.
First, condition1 is evaluated. The program enters the first if block because condition1 evaluates as true before checking condition2. Both the first and second condition evaluations lead to program execution of their associated blocks. The program will execute the else block within the first if when condition2 proves to be false.
The outer else block runs directly when condition1 remains false after the initial evaluation. The practice delivers practical benefits for determining decisions that rest on multiple preconditions like user verification and complex business rule processing or multiple structured constraints. Every Python program requires correct indentation to ensure clear program representation.
Let us consider an example showing the implementation of the nested if…else statement in Python.
Output:
# Output 1: Enter your password: secure123 Password is strong. # Output 2: Enter your password: password Password must contain at least one number.
Explanation:
In this example, the program takes a password as input from the user. It first checks if the password has at least 8 characters, and then checks if it contains at least one number. Based on these conditions, it prints whether the password is strong or what requirement is missing.
Let us see another example showing the working of the nested if…else statement in Python:
Output:
# Output 1: Enter your marks: 80 Congratulations! You passed with distinction. # Output 2: Enter your marks: 35 You failed the exam. Better luck next time.
Explanation:
In this example, the program takes the user's marks as input. It first checks if the marks are 40 or more to determine if the user has passed. If passed, it further checks if the marks are 75 or more to decide if the user passed with distinction; otherwise, it prints that the user has passed or failed accordingly.
A program requires the if…elif…else statement to evaluate sequential conditions for multiple tests. A program can check several conditions in succession to locate a condition which produces a True outcome.

The first step checks the if condition because it determines if the corresponding code block needs execution before moving to the next elif conditions. An if statement will be evaluated when all previous conditions prove false.
A match of any elif condition results in program execution of its associated code thus skipping all following conditions. The execution path ends at the else block whenever none of the conditions are satisfied. The decision structure provides efficient outcome management for software with numerous possible results.
Here is the syntax for an if...elif...else statement.
Multiple conditions within a program requires an if…elif…else structural evaluation process.
Let us consider an example showing the implementation of the if…elif…else statement in Python.
Output:
# Output 1: Enter the temperature in Celsius: 35 It's a hot day. # Output 2: Enter the temperature in Celsius: 22 The weather is warm.
Explanation:
In this example, the program takes the temperature as input from the user. It uses if-elif-else statements to check different temperature ranges. Based on the value entered, it prints whether the day is hot, warm, cool, or cold.
Here is another example of an if…elif…else statement that categorizes exam grades based on marks.
Output:
# Output 1: Enter your marks: 95 Grade: A # Output 2: Enter your marks: 64 Grade: C
Explanation:
In this example, the program takes the user's marks as input. It uses if-elif-else statements to check different ranges of marks. Based on the marks entered, it assigns and prints the corresponding grade.
We request you to subscribe our newsletter for upcoming updates.