5 Top Use Cases to Understand What is Python If-Else Statement

Companies are heavily reliant on algorithmic decision-making because computer programs can process vast amounts of data within a short span. It helps automate decisions using advanced programming features. One such feature that serves as an excellent decision-making tool is conditional if-else Python statements. Let’s understand the conditional statements in Python and analyze some Python if-else statement examples and use cases.
This blog discusses the following:
- What is the Syntax of the If-Else Statement in Python?
- How Does the If-Else Statement Execute in Python?
- What are Some Practical Examples of Using If-Else Statements in Python?
- Can You Nest If-Else Statements in Python?
- How Does the If-Else Statement Differ From the If-Elif-Else Statement in Python?
Data Science Courses
What is the Syntax of Python If-Else Statement?
Conditional statements in Python essentially act as the decision-making tool that allows the system to take a particular action if one condition is true. If the condition is not true, the system will perform another action. For instance, you execute a code in a program asking it to switch on the light during the daytime. If the condition ‘it is daytime’ is true, then the system will run a code and switch on the light. It will, however, turn off the light if the statement is false. These are called ‘if-else’ or conditional statements in Python. In essence, this coding instruction enables the program to make decisions according to specific conditions.
The basic syntax of the if condition in Python is:
if condition:
#statement to execute if the condition is true
Example 1:
Input
a = 12
b = 7
if a > b
print(“a is greater than b.”)
Output
a is greater than b.
Example 2:
Input
student_marks = 78
if student_marks >= 40
result = pass
print(“The student has passed”)
Output
The student has passed
The basic syntax of the else condition in Python is:
if condition:
# code to execute if condition is true
else:
# code to execute if condition is false
Example 1:
Input
order_total = ₹46
if order_total < ₹50:
shipping_cost = ₹5
else:
shipping_cost = 0
print(“Shipping cost: ₹{shipping_cost}”)
Output
Shipping cost: $5
Example 2:
Input
purchase_amount = ₹899
if purchase_amount >= 1000:
discount = 10%
else:
discount = 0
final_amount = purchase_amount – (purchase_amount * discount)
print(“Total price after discount: ₹{final_amount}”)
Output
Total price after discount: ₹899
ALSO READ: Top 18 Python Pattern Programs
How Does the If-Else Statement Execute in Python?
As the name suggests, the Python if-else statement works on two block codes—if and else. When you provide an input, the system evaluates the ‘if’ condition in Python. If the condition is true, the ‘if’ code gets executed. However, in case the condition is false, the program skips the if code and executes the else code block. Here is a flowchart to understand the Python if-else statement execution better:
What are Some Practical Examples of Using If-Else Statements in Python?
Here are some practical use cases of if-else Python statements in various industries:
-
Employee Bonus According to Their Performance
Organizations can use Python if-else statement applications that determine whether an employee is eligible for a quarterly bonus based on the percentage of target achieved.
target_achieved = 80%
bonus_rate = 20%
If target_achieved >= 50%
print(“You get a 20% bonus”);
else:
print(“No Bonus.”)
-
Loan Approval
Python if-else statement is beneficial in the banking and finance industry for loan disbursal. An application can be used to determine loan eligibility criteria based on a person’s credit score. For instance, if the credit score of a person is equal to or more than 650, then they’ll be eligible for getting a loan. In case the credit score is below 650, the loan application will be rejected.
credit_score = 715
if credit_score >= 650:
print(“Your loan application has been approved!”)
else:
print(“Your loan application has been rejected.”)
-
Quality Control in Manufacturing
Another practical use of the if-else Python statement is to ensure quality control during the manufacturing process. If a product meets the quality score, it can be sent to a wholesaler or else it can be discarded.
quality_score = 65
if quality_score >= 50:
print(“Product quality is excellent.”)
else:
print(“Product quality is poor. It needs to be rejected “)
-
Password Security
If else Python statements can also be used to verify passwords by using the following code:
name = input(“Give name:”)
if name == “Kevin”:
password = input(“Give password:”)
if password == “XYZ@456”:
print(“Both inputs are correct!”)
else:
print(“The given password is incorrect.”)
else:
print(“The given name is wrong.”)
-
Medication Dosage
The Python if-else statement can also be used in the healthcare industry to determine medicine dosage as per a person’s age, weight, and symptoms.
patient_weight = 65 (in kilograms)
patient_age = 37
if patient_weight >= 80 or patient_age >= 65:
medicine_dosage = “High”
else:
medicine_dosage = “Standard”
print(f”Medicine dosage: {medicine_dosage}”)
Can You Nest If-Else Statements in Python?
Yes, you can nest a Python if-else statement. Nested statements are conditional statements in Python inside another conditional statement. Simply put, nested statements are used to check multiple conditions. For example, you want to check if the number 15 is greater than 10 and if it is completely divisible by 5.
num = 15
if num >= 10:
if num % 5 == 0:
print(“Number is greater than 10 and completely divisible by 5.”)
else:
print(“Number is greater than 10 but not completely divisible by 5.”)
else:
print(“Number is not greater than 10.”)
ALSO READ: Why is it Important to Learn Python in Data Science?
How Does the If-Else Statement Differ From the If-Elif-Else Statement in Python?
Python if-else statement is commonly used in case there are only two possible outcomes. For example, if a student passes or else fails. However, if there are multiple circumstances such as giving grades to students, an if-elif-else statement is used. It is short for the else if statement.
grade = 78
if grade >= 90:
print(“Your grade is ‘A’ on the exam.”)
elif grade >= 80:
print(“Your grade is ‘B’ on the exam.”)
elif grade >= 70:
print(“Your grade is ‘C’ on the exam.”)
elif grade >= 60:
print(“Your grade is ‘D’ on the exam.”)
else:
print(“Your grade is ‘F’ on the exam.”)
ALSO READ: Which Python Courses are Best for You? A Comprehensive Guide to Help You Decide
According to the October 2023 TIOBE Programming Community Index (a popular index that indicates the popularity of global programming languages), Python is the top programming language. The GitHub Octoverse 2022 report also reveals that Python’s usage on GitHub is increasing by 22.5% every year, making it the second fastest-growing programming language. These figures indicate the importance of learning Python for software developers to boost their careers. Emeritus’ online data science courses help professionals learn the fundamentals of Python, including the Python if-else statement and other popular programming languages. Explore these online data science courses from Emeritus to advance your career!
Write to us at content@emeritus.org