Table of contents
1.
Introduction
2.
If Statement
3.
If-else Statement
4.
Nested if-else
5.
If-elif-else ladder
6.
Shorthand if statement
7.
Shorthand if-else block
8.
Frequently Asked Questions
9.
Key Takeaways
Last Updated: Mar 27, 2024

Conditional Statements in Python

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In programming, we must make judgments and then execute the following code block depending on those decisions. In programming languages, decision-making statements determine the course of program execution.

If, else, elif if used for decision-making in Python.

If Statement

if condition is the most basic decision-making statement. It is used to determine whether a particular statement or block of statements will execute or not, i.e., if a condition is true, a block of statements will be performed, otherwise not.

Syntax

if condition:
   # Statements to execute if
   # condition is true
You can also try this code with Online Python Compiler
Run Code

After evaluation, the condition will be either true or false. If the value is true, the if statement will execute the block of statements below it; otherwise, it will not.

Flowchart of if statement

Example:

Here's an example of a basic if statement

x = 70
y = 90
if (x < y):
    print('We’re inside the if condition')
z = 130
print(z)
You can also try this code with Online Python Compiler
Run Code

Output

We’re inside the if condition
130

Because the condition present in the if statement is true. That's why the block below the if statement is executed and the statements just after the if block.

If-else Statement

If a condition is true, then if statement will execute a block of statements; if the condition is false, the if statement will not. But what if the condition is false, and we want to do something else? This is when the else statement comes in. When the condition is false, we may combine the else statement with the if statement to execute a code block.

Syntax

if (condition):
   # Statements to execute if
   # condition is true
else:
   # Statements to execute if
   # condition is false
You can also try this code with Online Python Compiler
Run Code

After evaluation, the condition will be either true or false. If the value is true, the if statement will execute the block of statements below it; otherwise, else code block will execute.

Flowchart of if-else statement

Example:

Here's an example of a basic if-else block

x = 70
y = 90
if (x < y):
    print('x is smaller than y')
    print('in if statement')
else:
    print('y is less than or equals to x')
    print('in else statement')
z = 130
print(z)
You can also try this code with Online Python Compiler
Run Code

Output

x is smaller than y 
in if statement
130

Because the condition present in the if statement is true. So, the code block inside the if statement is executed and the statements just after the if-else block and it’ll not go inside the else block.

You can try it on online python compiler.

Nested if-else

Nested if-else statements mean that an if-else block is inside of another if-else block. 

Syntax

if (condition1):
   # Statements to execute if condition1 is true
   if (condition2):
   # Statements to execute if condition2 is true
   else:
   # Statements to execute if condition2 is false 
else:
   # Statements to execute if condition1 is false
You can also try this code with Online Python Compiler
Run Code

Flowchart of nested if-else block

Example:

Here's an example of nested if-else block

x = 70
y = 90
z = 100
if (x < y):
    print('x is smaller than y')
    if (x < z):
        print('x is smaller than z too')  
else:
    print('y is smaller than or equals to x')
    if (x < z):
        print('x is smaller than z')
You can also try this code with Online Python Compiler
Run Code

Output

x is smaller than y 
x is smaller than z too

If-elif-else ladder

The if-elif statements are executed from the top down. As soon as one of the conditions controlling the if-elif is true, the statement associated with that if-elif is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Syntax

if (condition):
    statements
elif (condition):
    statements
elif (condition):
    statements
 .
 .
 .
 .
else:
    statements
You can also try this code with Online Python Compiler
Run Code

Flowchart of an if-elif-else block

Example:

Here's an example of an if-elif-else block

x = 70
if (x == 10):
    print('x equals to 20')  
elif (x == 20):
    print('x equals to 20')
elif (x == 30):
    print('x equals to 30')
elif (x == 40):
    print('x equals to 40')
else:
    print('x is greater than 40')
You can also try this code with Online Python Compiler
Run Code

Output

x is greater than 40

Shorthand if statement

If there’s an only a single statement in an if block, we can put all of that in one single line

Syntax

if (condition): statement
You can also try this code with Online Python Compiler
Run Code

Example

x = 70
y = 90
if (x < y): print('x is smaller than y')
You can also try this code with Online Python Compiler
Run Code

Shorthand if-else block

If there’s only a single statement in an if-else block, we can put all of that in one single line

Syntax

statement_if_true if (condition) statement_if_true
You can also try this code with Online Python Compiler
Run Code

Example

x = 70
y = 90
print('x > y') if (x < y): print('x < y')
You can also try this code with Online Python Compiler
Run Code

Output

x < y

Must Recommended Topic, Floor Division in Python and Convert String to List Python.

Also Read,  leap year program in python

Frequently Asked Questions

  1. What do you mean by control structure in python?
    Control structures decide which statements in a program are executed and in what order, allowing statements to be skipped or repeated. if, if/else, and if/elif/else statements are all examples of control structures that allow which statements to be skipped or execute conditionally
     
  2. What part of an if statement should be indented?
    All the statements that lie within the if block should be indented and at the same level
     
  3. What is the most popular use case of shorthand syntax?
    It is most popularly used when we just need to return or print something and there’s only one statement in the conditional block.

Key Takeaways

  • Cheers if you reached here! In this blog, we learned about Conditional Statements in Python. 
  • We have covered the syntax, flowchart, and uses of basic if and if-else nested if-else code blocks.
  • We have also seen different examples and use cases of these conditional blocks.
  • Further, we learned about shorthand syntax in python.

On the other hand, learning never ceases, and there is always more to learn. So, keep learning and keep growing, ninjas!

Check out this article - Quicksort PythonFibonacci Series in Python

Check out the Top 100 SQL Problems to get hands-on experience with frequently asked interview questions and land your dream job.

Good luck with your preparation!

Live masterclass