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

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

1. Python Decision Making Statements

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

2. 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.

Learn: Operators in Python

3. Python if Statements

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")

7 is good

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

if 1:
    print("yay")

yay

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

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

1

>>>

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

Learn: Namespaces and Variable scope in Python

4. 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")

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.

Learn: Variable and Data Types in Python

5. 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")

1

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

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.

Learn: Python Functions with Syntax and Examples

6. 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")

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.

Learn: Function Arguments in Python with Syntax and Example

7. 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")

Greater

>>>

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

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

Hi
Works

Python Interview Questions
>>>

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.

Learn: Python Lists with Examples – A Comprehensive Tutorial

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

8. 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!

See also – 

Python Number Types

For reference

3 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

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.