Python
In this tutorial we will learn about If Else statement in Python.
The if
statement is a decision making statement and it controls the flow of the execution of the program.
We use the if
keyword to create an if statement. The body of the if statement is indented with tabs/spaces.
if condition:
#
# if block code
#
Code inside the if block is executed only when the given condition
is satisfied. Otherwise, it is ignored.
In the following Python code we are checking whether x is even.
# variable
x = 10
if x % 2 == 0:
print("x is even.")
print("End of code.")
The above code will print the following output.
x is even.
End of code.
If the value of x is odd then we will get the following result.
End of code.
This is because the if block is ignored as the condition is not satisfied.
If we want to create an "either-or"/"two-options" then we use the if-else statement.
We use the else
keyword to create the else block.
if condition:
#
# if block code
#
else:
#
# else block code
#
Code inside the if block is executed only when the given condition
is satisfied. Otherwise, else block code is executed.
In the following Python code we will get either "x is odd" or "x is even" message depending on the value of x.
# variable
x = 10
if x % 2 == 0:
print("x is even")
else:
print("x is odd")
print("End of code.")
As x is 10 so, we will get the following output.
x is even
End of code.
If we change the value of x to 11 then we will have the following result.
x is odd
End of code.
If we want more than two options then we create a chain of if else statement using elif
keyword.
if condition1:
#
# condition1 block code
#
elif condition2:
#
# condition2 block code
#
else:
#
# else block code
#
Code inside the condition1 if block is executed only when the given condition condition1
is satisfied.
If condition1
fails then condition2
is checked. If it is satisfied then the condition2 block is executed.
If no condition is satisfied then, else block code is executed.
You can have multiple elif
block based on your requirement.
In the following Python code we are checking is x an even number or odd number or zero.
# variable
x = 0
if x == 0:
print("x is zero")
elif x % 2 == 0:
print("x is even")
else:
print("x is odd")
print("End of code.")
As x is 0 so, we will get the following output.
x is zero
End of code.
If there is only one statement in the if-block then we can write that in one line like the following.
# variable
x = 10
if x % 2 == 0: print("x is even")
print("End of code.")
If there is only one statement for both if and else block then we can write them all in one line.
The following code will print "x is even" if the if-condition is satisfied otherwise, it will print "x is odd".
# variable
x = 10
print("x is even") if x % 2 == 0 else print("x is odd")
print("End of code")
and
, or
and not
operatorsWe can also use logical operators to check multiple conditions.
In the following Python program we are going to print "foo" if value of x is divisible by 3, "bar" if value of x is divisible by 5 and "foobar" if value of x is divisible by both 3 and 5.
# variable
x = 10
if x % 3 == 0 and x % 5 == 0:
print("foobar")
elif x % 3 == 0:
print("foo")
elif x % 5 == 0:
print("bar")
print("End of code.")
The above code will print "bar" as x is 10 and divisible by 5. Try changing the value of x to get different result.
ADVERTISEMENT