if-elif-else
Statements#
In the world of programming, decision-making is a fundamental concept, and the if-elif-else
statement in Python is a powerful tool for precisely that purpose. These statements allow you to create dynamic, branching logic within your programs. With them, you can instruct your code to take different actions based on specific conditions, making your programs more intelligent and responsive.
What is an if-elif-else Statement?#
An if-elif-else
statement is a control structure in Python that enables your program to make choices. It starts with an if
statement, which checks a particular condition. If that condition is true, a specific block of code is executed. However, if the condition is not met, the program can continue to evaluate other conditions using elif
(short for “else if”) statements. These “elif” clauses allow for multiple conditions to be checked sequentially. Finally, if none of the preceding conditions are true, the else
block of code is executed.
Why is it Used?
The primary purpose of if-elif-else
statements is to introduce decision-making capabilities into your code. They allow your program to adapt and respond to varying situations. For example, you might use if-elif-else
statements to implement logic like:
Game Over Conditions: Determine if a player’s score has reached a winning threshold. If yes, declare a winner; otherwise, continue the game.
player_1_score = 47
if player_1_score >= 100:
print("YOU WIN!")
else:
print(f"You need {100 - player_1_score} more points to win!")
You need 53 more points to win!
User Authentication: Check if a user’s credentials are correct. If they are, grant access; otherwise, deny access.
user_input_username = "john_doe"
user_input_password = "secure_password"
correct_username = "john_doe"
correct_password = "secure_password"
if user_input_username == correct_username and user_input_password == correct_password:
print("Access granted!")
else:
print("Access denied. Please check your username and password.")
Access granted!
Grade Evaluation: Assign grades based on a student’s score.
student_score = 85
if student_score >= 90:
print("A")
elif student_score >= 80:
print("B")
elif student_score >= 70:
print("C")
elif student_score >= 60:
print("D")
else:
print("F")
B
In these examples, “if-elif-else” statements allow the program to take different actions depending on specific conditions. They provide the flexibility to handle a wide range of scenarios and are an essential tool for building responsive and intelligent software.