Python if Statement
The if
statement in Python is used for conditional execution. It allows you to execute a block of code only if a certain condition is true.
The following flowchart illustrates the if statement:

The basic syntax of the if
statement is as follows:
1 2 |
if condition: # Code block to be executed if the condition is True |
Here, the condition
is an expression that evaluates to either True
or False
. If the condition is True
, the indented code block below the if
statement will be executed. If the condition is False
, the code block will be skipped, and the program will continue to the next statement after the indented block.
You can also extend the if
statement with elif
(short for “else if”) and else
clauses to handle multiple conditions and provide fallback options:
1 2 3 4 5 6 |
if condition1: # Code block to be executed if condition1 is True elif condition2: # Code block to be executed if condition1 is False and condition2 is True else: # Code block to be executed if neither condition1 nor condition2 is True |
Example:
1 2 3 4 5 6 7 8 |
x = 10 if x > 0: print("x is positive") elif x == 0: print("x is zero") else: print("x is negative") |
In this example, depending on the value of variable x
, one of the three print statements will be executed.
Remember to indent the code blocks properly since Python relies on indentation to define block structures. The standard indentation is four spaces, but you can also use tabs or a different number of spaces, as long as it’s consistent throughout your code.
Python if…else statement
The if...else
statement in Python is an extension of the if
statement that allows you to perform different actions based on the evaluation of a single condition. The basic syntax of the if...else
statement is as follows:
1 2 3 4 |
if condition: # Code block to be executed if the condition is True else: # Code block to be executed if the condition is False |
Here, the condition
is an expression that evaluates to either True
or False
. If the condition is True
, the code block indented under the if
statement will be executed. If the condition is False
, the code block indented under the else
statement will be executed instead.
The else
block provides an alternative action to take when the initial condition is not met.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
# Function to check if a number is positive or non-positive def check_positive(number): if number > 0: print(f"{number} is positive.") else: print(f"{number} is either zero or negative.") # Test cases check_positive(7) check_positive(-3) check_positive(0) |
Output
7 is positive.
-3 is either zero or negative.
0 is either zero or negative.
Explanation:
- In the
check_positive
function, we pass a number as an argument. - Inside the function, we use the
if...else
statement with the conditionnumber > 0
. If the condition isTrue
, the first block of code (indented underif
) will be executed, printing “{number} is positive.” - If the condition is
False
, the second block of code (indented underelse
) will be executed, printing “{number} is either zero or negative.”
In the example, we call the check_positive
function with different numbers to demonstrate how the if...else
statement handles both positive and non-positive cases.
Python if…elif…else statement
The if...elif...else
statement in Python allows you to check multiple conditions and execute different code blocks based on which condition evaluates to True
. It provides a way to handle several cases in a more complex scenario. The basic syntax of the if...elif...else
statement is as follows:
1 2 3 4 5 6 7 8 9 |
if condition1: # Code block to be executed if condition1 is True elif condition2: # Code block to be executed if condition1 is False and condition2 is True elif condition3: # Code block to be executed if condition1 and condition2 are False, and condition3 is True ... else: # Code block to be executed if none of the above conditions are True |
You can have multiple elif
clauses to handle different cases, and the else
clause provides a fallback option if none of the preceding conditions are satisfied.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Function to classify a number as positive, negative, or zero def classify_number(number): if number > 0: print(f"{number} is positive.") elif number < 0: print(f"{number} is negative.") else: print(f"{number} is zero.") # Test cases classify_number(7) classify_number(-3) classify_number(0) |
Output
7 is positive.
-3 is negative.
0 is zero.
Explanation:
- In the
classify_number
function, we pass a number as an argument. - Inside the function, we use the
if...elif...else
statement to check three conditions:- If
number > 0
, it will print “{number} is positive.” - If
number < 0
, it will print “{number} is negative.” - If none of the above conditions are
True
(i.e.,number == 0
), it will print “{number} is zero.”
- If
In the example, we call the classify_number
function with different numbers to demonstrate how the if...elif...else
statement handles different cases and provides the appropriate output based on the number’s value.