Python Conditional Statements

Introduction:

In Python, the if-else statement allows you to execute different blocks of code based on certain conditions. It provides a way to make decisions and control the flow of your program.

The `if` statement is the fundamental building block of a conditional statement. It allows you to execute a block of code if a certain condition is true. The syntax of the `if` statement is as follows:

if condition:
# Code block to execute if the condition is True

The `else` statement is used in conjunction with the `if` statement. It provides an alternative block of code to execute when the condition in the `if` statement is false. The syntax is as follows:

The basic syntax of an if-else statement in Python is as follows:

if condition:
# Code block to execute if the condition is True
else:
# Code block to execute if the condition is False

The condition is an expression that evaluates to either True or False. If the condition is True, the code block immediately following the if statement will be executed. Otherwise, if the condition is False, the code block following the else statement will be executed.

Let’s look at an example to understand it better. Suppose we want to check whether a given number is positive or negative:

number = int(input("Enter a number: "))
if number > 0:
print("The number is positive.")
else:
print("The number is negative or zero.")

In this example, we use the input() function to get a number from the user, convert it to an integer using int(), and store it in the number variable. The if-else statement then checks whether the number is greater than zero. If it is, it prints “The number is positive.” Otherwise, it prints “The number is negative or zero.”


The `elif` statement allows you to check additional conditions after an initial `if` statement. It provides a way to handle multiple cases within the same conditional statement. The syntax is as follows:

Here’s an example that checks whether a number is positive, negative, or zero:

number = int(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")

In this case, the elif statement allows us to check an additional condition. If the first condition is False, it moves to the elif statement and checks whether the number is less than zero. If that condition is True, it prints “The number is negative.” Finally, if both the first and second conditions are False, it executes the code block under the else statement and prints “The number is zero.”


In Python, nested if-else statements allow you to have if-else statements within other if-else statements. They provide a way to handle complex conditions and execute different blocks of code based on multiple conditions. The basic syntax of a nested if-else statement in Python is as follows:

if condition1:
# code to execute if condition1 is true
if condition2:
# code to execute if condition1 and condition2 true
else:
# code execute if condition1 true but condition2 false
else:
# code to execute if condition1 is false

In a nested if-else statement, the inner if-else statement is indented further to the right than the outer if-else statement. The inner if-else statement is evaluated only if the condition of the outer if statement is `True`. Here’s an example:

age = 25
has_membership = True

if age >= 18:
if has_membership:
print("Welcome to the exclusive club!")
else:
print("Membership required for entry.")
else:
print("You must be 18 or older to enter.")