Do you want to learn more about the if-else statement? How to implement the if-else statement in Python?
Let’s dive in…
Table of Contents:
In programming and algorithm, we always have a condition.
If it is true, do this.
If it is false (not true), do that.
The first thing is to check the condition. Based on the result of the condition, decide what to do the next.
Let me make it simple for you.
Our whole life is conditional…
Even the alarm at our home has an if-else statement implemented.
This is the simplest explanation for decision-making I can give to any novice to understand.
Now, moving to Python programming…
How to implement if-else logic in Python?
For implementing the if-else statement in Python 3, you have to follow a certain syntax. Your system only understands the syntax, the way you write.
if <condtion>: <block_of_your_code>
The ‘if’ condition will be followed by the code block. And it will be executed if the condition is true. If the condition is false, the control pointer will move out of the block without executing it.
Understanding syntax:
Example: Write a Python program to check if the number is zero.
nNum = 0 if nNum == 0: print("Number is Zero.") print("Out of if statement")Output:
Number is Zero. Out of if statementNow let's say assign the value 1 to
nNum
to make the if condition false.nNum = 1 if nNum == 0: print("Number is Zero.") print("Out of if statement")Output:
Out of if statementHere, the print statement in if-block is not executed.
if-else statement in Python 3
What if you want to execute some code if the given condition is false?
Here is the if-else statement you need to implement.
Python syntax for the if-else statement:
if <condtion>: <your_if_code> else: <your_else_code>Python Program for if-else statement:
Example: Write a Python program to print whether if it is a zero or non-zero number.
nNum = 1 if nNum == 0: print("Number is zero.") else: print("Number is not zero.")Output:
Number is not zero.elif statement in Python 3
We can also add one more if condition inside the else block. This is called nested if-else in programming.
Python syntax for the nested if-else statement:
if <condtion>: <your_if_code_block> elif <condtion>: <your_elif_code_block> else: <your_else_code_block>What is the elif keyword in Python?
The
elif
keyword is short for else-if.The block inside the
elif
will be executed when two conditions meet.
Example: Write a program to print if the given number is a positive or a negative using elif statement.
nNum = 1 if nNum == 0: print("Number is zero.") elif nNum &gt; 0: print("Number is a positive.") else: print("Number is a negative.")Output:
Number is a positive.Nested if else in Python 3
Can we add if statement inside another if statement in Python?
Yes.
Python syntax for the nested if-else statement:
if <condtion>: <your_code_block_1> if <condition_1_1>: <your_code_block_1_1> else: <your_code_block_1_2> elif <condtion>: <your_elif_code_block> else: <your_else_code_block>Python Program for nested if-else in Python:
Example: Write a Python program to print if the number is positive, negative or zero, using nested if-else statement.
nNum = 1 if nNum != 0: if nNum &gt; 0: print("Number is a positive.") else: print("Number is a negative.") else: print("Number is zero.")Output:
Number is a positive.With this, you can check multiple conditions in Python if statements.
Practice Solving if-else Python Programming Questions:
What's next?
Here is a simple Python program exercise for practice.
Read how to take user input from the keyboard in Python.
Check more Python coding questions for practice.
This is all about decision-making using if-else and nested if-else in Python 3. If you have any doubts in this complete tutorial for decision-making in Python, feel free to write in the comment section.
Happy Pythoning!
Hi Aniruddha,
Please modify the if-else condition First line.
Thanks,
Anil
Done.
Thanks for the correction, Anil Kumar!