Decision making Statement in Python

Decision-making is a fundamental aspect of programming that allows you to control the flow of your Python programs based on certain conditions. In Python, you can achieve this using decision-making statements. This guide is designed for beginners to understand how to use decision-making statements effectively.

In the below PDF we discuss about Decision making Statement in detail in simple language, Hope this will help in better understanding.

Python

1. The If Statement :

The if statement is one of the most basic decision-making statements in Python. It allows you to execute a block of code if a specified condition is True. Here’s a simple example:

age = 18
if age >= 18:
print("You are eligible to vote.")

In this code, the if statement checks if the age variable is greater than or equal to 18. If it is, the program will print the message, “You are eligible to vote.”

2. The If-Else Statement :

The if-else statement extends the if statement by allowing you to execute one block of code if the condition is ‘True’ and another block if it’s ‘False’. Here’s an example:

age = 15
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote yet.")

In this case, if the ‘age’ is 15, it will print “You are not eligible to vote yet.”

3. The If-Elif-Else Statement :

The if-elif-else statement is used when you have multiple conditions to check. It allows you to test conditions one by one until one of them is ‘True’. If none of the conditions are met, the else block is executed. Here’s an example:

score = 75
if score >= 90:
print("You got an A.")
elif score >= 80:
print("You got a B.")
elif score >= 70:
print("You got a C.")
else:
print("You need to improve your score.")

In this code, it will print “You got a C.” because the score is 75, which satisfies the third condition.

4. Nested Decision-Making Statements :

You can also nest decision-making statements within each other to handle more complex scenarios. For instance:

age = 25
income = 50000
if age >= 18:
if income >= 30000:
print("You qualify for the loan.")
else:
print("Your income is too low to qualify.")
else:
print("You are not eligible to apply for a loan.")

This code first checks if the person is above 18 years old and then, if the income is above 30,000, it qualifies them for a loan.

Related Question

The ‘if’ statement in Python is used to execute a block of code if a specified condition is true. It allows you to control the flow of your program based on whether a condition is met.

The if-else statement in Python allows you to execute one block of code if a condition is true (if part) and another block of code if the condition is false (else part). It provides an alternative path for execution based on the condition.

The if-elif-else statement is used when you have multiple conditions to check in sequence. It allows you to test conditions one by one until one of them is true. If none of the conditions are true, the else block is executed.

You can nest decision-making statements by placing one decision statement (e.g., if, if-else, if-elif-else) inside another. This allows you to handle more complex scenarios by checking conditions in a hierarchical manner.

Decision-making statements in programming are used to create logic and control the flow of a program based on specific conditions. They enable programs to make choices and execute different code paths depending on the values of variables or expressions.

Relevant

Introduction to Python Python is

Variables in Python Variables are

Keywords in Python Keywords in

Data Types in Python In

Operators in Python Python is

2 thoughts on “Decision making Statement in Python”

Leave a Comment

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

// Sticky ads
Your Poster