Python If, If-else, Nested Statements – Python Decision Making Statements

Free Python courses with 57 real-time projects - Learn Python

Today, we talk about Python decision making constructs.  This includes Python if statements, if else statements, elif statements, nested if conditions and single statement conditions.

We will understand these with syntax and example to get a clear understanding.

So, let’s start the Python Decision Making Tutorial.

Python Decision Making Statements - Python If, If-else, Nested Statements

Python Decision Making Statements – Python If, If-else, Nested Statements

What is Python Decision Making Statements?

Before we begin with python decision making expressions, let us revise Python concepts.

Sometimes, in a program, we may want to make a decision based on a condition. We know that an expression’s value can be True or False. We may want to do something only when a certain condition is true.

For example, assume variables a and b. If a is greater, then we want to print “a is greater”. Otherwise, we want to print “b is greater”.

For this, we use an if-statement. Also, operators come in handy when you want to join conditions to make a composite one.

Let us see at various python decision-making expressions in details with syntax and example. So let’s install python on Windows first and revise Python syntax for programming in Python.

Python if Statements

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

An if statement in python takes an expression with it. If the expression amounts to True, then the block of statements under it is executed.

If it amounts to False, then the block is skipped and control transfers to the statements after the block. But remember to indent the statements in a block equally.

This is because we don’t use curly braces to delimit blocks. Also, use a colon(:) after the condition.

If statements in Python

Python Decision Making – Python if Statement

Before starting with the example, let us see various types of variables and data types in Python as it will help in better programming.

a=7
if a>6:
     print(f"{a} is good")

Output

7 is good

Here, since 7>6, the condition is true. So, it prints the given string.

if 1:
    print("yay")

Output

yay

We know, 1 has a Boolean value of True. So, the condition is true, and it prints ‘yay’.

if(1==1):
   print("1")

Output

1
>>>

You can also write the condition in parentheses. It does not cause a syntax error.

Python if-else Statements

What happens when the condition is untrue? We can mention that it in the block after the else statement. An else statement comes right after the block after ‘if’.

if else Statements Python

Python Decision Making – Python if else Statement

if 2<1:
     print("2")
     else:
SyntaxError: Invalid syntax
if 2<1:
     print("2")
else:
     print("1")

Output

1

Pay attention to the indent. The else keyword does not appear in the if-block.

Press Backspace to undo the automatic indent. Here, 2 is not less than 1. So, the statements in the else-block are executed. It prints 1.

if 2<1:
    print("2")
else:
    print("1")
else:
SyntaxError: invalid syntax
>>>

As appears in the above example, you cannot posit two else statements under an if. It causes a syntax error.

Chained Conditionals (elif ladder)

Python allows the elif keyword as a replacement to the else-if statements in Java or C++. When we have more than one condition to check, we can use it.

If condition 1 isn’t True, condition 2 is checked. If it isn’t true, condition 3 is checked.

elif condition in python

Python Decision Making Statements – Python Chained operators

if 2<1:
     print("2")
     else if 3<1:
SyntaxError: invalid syntax
if 2<1:
     print("2")
elif 1<3:
     print("1")

Output

1
if 2<1:
     print("2")
elif 3<1:
     print("3")
else:
     print("1")

Output

1
>>>

As we saw, else if causes a syntax error. We must use elif. Here, 2 is not less than 1, so, the condition with elif is checked.

Since it is true (1<3), it prints 1. Also, you can put an else statement after your elif statements if you want. Since in the last example, the first two conditions are false, the else is executed. So, it prints 1.

Nested if Statements in Python

You can put an if statement in the block under another if statement. This is to implement further checks.

Nested if statement in python

Python Decision Making – Nested if Statements in Python

>>>a=1
>>>b=2
>>>if a==1:
         if b==2:
               print("a is 1 and b is 2")

Output

a is 1 and b is 2
>>>

Here, a is 1. So, b is checked. Since it is 2, it prints the given string. Not every if block has to have statements though.

Single Statement Condition in Python

If you only need to write a single statement under if, you can write it in the same line using single statement python decision making constructs.

a=7
if a>4: print("Greater")

Output

Greater
>>>

Here, we wrote it in one line, but it works without a problem.

if a>4: print("Hi"); print("Works")

Output

Hi
Works
>>>

You can also use semicolons to write more than one statement in the same line as the condition. However, this may affect the readability of your code.

So, this was all about Python Decision Making Statements. Hope you like our explanation.

Python Interview Questions on Decision Making Statements

  1. What is decision making statement in Python?
  2. How do you make a decision in Python?
  3. How many types of decision making statement are there in Python?
  4. What is Elif statement in Python?
  5. Can you have two if statements in Python?

Conclusion

In this lesson, we learnt about the Python decision-making constructs. These include Python if, else, elif, and nested-if statements.

We have discussed all these statements with syntax and examples for better understanding. They allow us to make decisions in python so that we can choose a set of statements to execute.

Don’t forget to try your own combinations in the shell.

Furthermore, if you have any query, feel free to share with us!

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

5 Responses

  1. dns says:

    Great tutorial

    • DataFlair Team says:

      Glad, you like our article – Python Decision Making Statement. Hope, it helped and cleared all your Python concept. For more informative Python Tutorial follow our sidebar.
      Keep learning and keep exploring DataFlair

  2. terence says:

    this is a good lesson ,which can open up the eyes of most blinds in programming

  3. Nagaraju Diviti says:

    Yes, it is great tutorial

Leave a Reply

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