22
DecDecision Making Statements: If, If..else, Nested If..else and if-elif-else Ladder
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
- If statements in python
- If else statement in python
- Nested if else statement in Python
- 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.
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.
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")
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)
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.