Learn how to use the if...else statement in Python in this tutorial. Discover how to control the flow of your Python programs based on certain conditions.
In computer programming, we use the if
statement to run a block code only when a certain condition is met.
For example, assigning grades (A, B, C) based on marks obtained by a student.
In Python, there are three forms of the if...else
statement.
if
statementif...else
statementif...elif...else
statementThe syntax of if
statement in Python is:
if condition:
# body of if statement
The if
statement evaluates condition
.
condition
is evaluated to True
, the code inside the body of if
is executed.condition
is evaluated to False
, the code inside the body of if
is skipped.Working of if Statement
number = 10
# check if number is greater than 0
if number > 0:
print('Number is positive.')
print('The if statement is easy')
Output
Number is positive.
The if statement is easy
In the above example, we have created a variable named number. Notice the test condition,
number > 0
Here, since number is greater than 0, the condition evaluates True
.
If we change the value of variable to a negative integer. Let's say -5.
number = -5
Now, when we run the program, the output will be:
The if statement is easy
This is because the value of number is less than 0. Hence, the condition evaluates to False
. And, the body of if
block is skipped.
An if
statement can have an optional else
clause.
The syntax of if...else
statement is:
if condition:
# block of code if condition is True
else:
# block of code if condition is False
The if...else
statement evaluates the given condition
:
If the condition evaluates to True
,
if
is executedelse
is skippedIf the condition evaluates to False
,
else
is executedif
is skippedWorking of if...else Statement
number = 10
if number > 0:
print('Positive number')
else:
print('Negative number')
print('This statement is always executed')
Output
Positive number
This statement is always executed
In the above example, we have created a variable named number. Notice the test condition,
number > 0
Since the value of number is 10, the test condition evaluates to True
. Hence code inside the body of if
is executed.
If we change the value of variable to a negative integer. Let's say -5.
number = -5
Now if we run the program, the output will be:
Number is negative. This statement is always executed.
Here, the test condition evaluates to False
. Hence code inside the body of else
is executed.
The if...else
statement is used to execute a block of code among two alternatives.
However, if we need to make a choice between more than two alternatives, then we use the if...elif...else
statement.
The syntax of the if...elif...else
statement is:
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3
Here,
true
, code block 1 is executed.false
, then condition2 is evaluated.true
, code block 2 is executed.false
, code block 3 is executed.Working of if...elif Statement
number = 0
if number > 0:
print("Positive number")
elif number == 0:
print('Zero')
else:
print('Negative number')
print('This statement is always executed')
Output
Zero
This statement is always executed
In the above example, we have created a variable named number with the value 0. Here, we have two condition expressions:
Here, both the conditions evaluate to False
. Hence the statement inside the body of else
is executed.
We can also use an if
statement inside of an if
statement. This is known as a nested if statement.
The syntax of nested if statement is:
# outer if statement
if condition1:
# statement(s)
# inner if statement
if condition2:
# statement(s)
Notes:
else
and elif
statements to the inner if
statement as required.if
statement inside the outer else
or elif
statements(if they exist)if
statements.number = 5
# outer if statement
if (number >= 0):
# inner if statement
if number == 0:
print('Number is 0')
# inner else statement
else:
print('Number is positive')
# outer else statement
else:
print('Number is negative')
# Output: Number is positive
In the above example, we have used a nested if statement to check whether the given number is positive, negative, or 0.
#python