[Decision Making] if-else, nested elif Statements in Python 3 | Example Code

[Decision Making] if-else, nested elif Statements in Python 3 | Example Code

Do you want to learn more about the if-else statement? How to implement the if-else statement in Python?

Let’s dive in…

What is the if-else statement?

In programming and algorithms, 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…

  • If it is 10 pm, sleep.
  • If it is at 9 am, go to the office.

Even the alarm at our home has an if-else statement implemented.

  • If it is 6 am, play the alarm.
  • Else, be silent.

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?

if statement in Python

For implementing the if-else statement in Python 3, you have to follow a certain syntax. Your system only understands the syntax and the way you write.

Python syntax for the if statement:

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:

  • The colon (:)  after the if-condition depicts the start of the if-block. Don’t forget that.
  • All the lines of code inside if-block should start with a consistent number of white spaces, called an indentation in Python

Python Program for if statement:

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 statement

Now 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 statement

Here, 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 the keyword is short for else-if.

The block inside elif will be executed when two conditions are met.

  • The first if-condition should be false.
  • The elif-condition should be true.

Python Program for elif statement

Example: Write a program to print if the given number is positive or negative using an elif statement.

nNum = 1
if nNum == 0:
  print("Number is zero.")
elif nNum &amp;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 an 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 &amp;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.

  • Write a Python program to print the month of the year based on the given user input.

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 about this complete tutorial for decision-making in Python, feel free to write in the comment section.

Happy Pythoning!

2 Comments

  1. Hi Aniruddha,

    Please modify the if-else condition First line.

    nNum == 1
    if nNum == 0:
      print("Number is zero.")
    else:
      print("Number is not zero.")
    

    Thanks,
    Anil

Leave a Reply

Your email address will not be published. Required fields are marked *