Decision Making Statements: If, If..else, Nested If..else and if-elif-else Ladder

Decision Making Statements: If, If..else, Nested If..else and if-elif-else Ladder

22 Aug 2024
Beginner
41.3K Views
11 min read

Decision-Making Statements in Python: An Overview

The decision that may be important for you is whether you need a Python certification course after reading this article. In this Python Tutorial, we will discuss how to make use of these statements and how they can help your coding and make it easier to maintain and read.

What are Decision Making Statements in Python?

Programming requires decision-making statements because they let programs decide what to do and run distinct code blocks according to predefined conditions. The if, elif and if-else statements are just a few of the decision-making statements available in Python. Programmers can control how a program executes depending on a variety of conditions by using these statements.

Types of decision-making statements in python

There are four types of decision-making statements in Python language, those are

  1. If statements in python
  2. If else statement in python
  3. Nested if else statement in Python
  4. if-elif-else Ladder in Python

1. If statements in python

  • The simplest structure for making decisions is an if statement, which only permits the execution of a code block if a predetermined condition is met.
  • The if keyword is followed by an indented block of statements that are executed if the condition is assessed and found to be true.
  • If not, the code skips the if block.

If Statement in Python

Syntax of If Statement in Python

if expression:
 statement(s)

Example of If Statement in Python Editor

          num = 8
if num > 0:
  print("The number is positive.")

This Python code prints "The number is positive" if the value of num is greater than zero, and initializes a variable called num with the value 8.

Output

The number is positive.

Read More - 50 Python Interview Questions and Answers

2. If else statement in python

  • If the condition in the if block evaluates to false, the if-else statement executes an alternate block of code.
  • This block of alternatives is introduced by the else keyword.

If else statement in python

Syntax of If Else Statement in Python

if expression:
 statement(s)
else:
 statement(s)

Example of If Else Statement in Python

        num = 11
if num % 2 == 0:
  print("The number is even.")
else:
  print("The number is odd.")

This code sets the Python variable num to 11 and prints "The number is even" if it is divisible by two; else, it prints "The number is odd."

Output

The number is odd.

Read More - Python Developer Salary in India

3. Nested if else statement in Python

  • Python enables the nesting of decision-making statements, which facilitates the development of more complex decision logic.
  • A hierarchy of conditions can be created by an if or if-else block within another if or if-else block.

Syntax of Nested If Else Statement in Python

if expression1:
 statement(s)
 if expression2:
   statement(s)
 elif expression3:
   statement(s)
 elif expression4:
   statement(s)
 else:
   statement(s)
else:
 statement(s)

Example of Nested If Else Statement in Python Compiler

        num = 3
if num > 0:
  if num % 2 == 0:
    print("The number is positive and even.")
  else:
    print("The number is positive but odd.")
else:
  print("The number is not positive.")

This code assigns the python variable num a value of 3 and outputs "The number is positive and even" if it is both positive and even, "The number is positive but odd" if it is positive but odd, and "The number is not positive" if it is not larger than 0.

Output

The number is positive but odd.

4. Python if-elif-else Ladder

  • An if-elif-else ladder is an order of if statements connected by elif statements.
  • This enables you to check for numerous conditions and run separate code blocks based on which condition is met.

Syntax of Python if-elif-else Ladder

if condition1: # code block 1
elif condition2:
 # code block 2
elif condition3:
 # code block 3
else:
 # default code block

Example of Python if-elif-else ladder

        x = 11
if x == 2:
 print("x is equal to 2")
elif x == 3:
 print("x is equal to 3")
elif x == 4:
 print("x is equal to 4")
else:
 print("x is not equal to 2, 3, or 4")

The variable x is given the value of x in this example. The if-elif-else ladder is then used to determine x's value. If x equals 2, the code block after the if expression is performed. If x is 3 or 4, the code block that follows the matching elif expression is executed. If x is not equal to two, three, or four, the code block after the else statement is run.

Output

x is not equal to 2, 3, or 4

Short Hand if Statement

In Python, a shorthand if statement is an easy way to write an if statement with a single expression. It is very useful when you have a basic condition to verify and only one action to take if that condition is met.

Syntax of Short Hand if Statement

expression if condition else None

Example of Short Hand if Statement

       i = 21
if i > 15: print("i is greater than 15")

This code in the Python Online Compiler line gives the variable i the value 21. Then, an if statement is used to determine whether the value of i is greater than 15. If it is, the if block statement is run, which writes the string "i is greater than 15" to the console.

Output

i is greater than 15

Short Hand if-else statement

If-else statements in Python are written in a more clear manner using short-hand if-else statements. They are useful when there is just one expression to evaluate for each branch of the if-else statement.

Syntax of Short Hand if-else statement

expression if condition else alternative_expression

Example of Short Hand if-else statement

          
age = 12
message = "You can vote" if age >= 18 else "You cannot vote yet"
print(message)
This code snippet creates a variable called age and assigns the value 12 to it. Then, it use a conditional phrase known as a shorthand if-else statement to assess whether the age value is higher than or equal to 18. If it is, the variable message is set to "You can vote," otherwise it is set to "You cannot vote yet." Finally, the message value is printed to the console.

Output

You cannot vote yet
Summary

By understanding how to use if statements, if else statements and nested if else statements in Python programming you can give your applications the ability to make basic decisions. These Decision-Making Statements are an important part of Python certification training and will help you write more sophisticated programs.

FAQs

The if statement in Python (and other programming languages) is the most fundamental type of decision-making.

Decision boxes are used in flowcharts to help in decision-making processes. Specifically, they test conditions and provide two outputs: if the corresponding condition is true, the statement in the true block of the flowchart is executed; if not, the statement in the false block is executed.

When you want to execute one piece of code if a condition is true and another bit of code if the condition is false, use an if...else statement.

Yes, many if statements can be used in a row. This is known as an if-elif ladder.
Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this