In Python, it is often necessary to check multiple conditions in an if statement to determine if a particular action should be taken or not.
This tutorial will guide you step by step on how to check multiple conditions in an if statement in Python, allowing your code to make more complex decisions.
You will learn about using logical operators such as and, or, and not to combine multiple conditions in a single if statement.
Step 1: Basic If Statement in Python
Before diving into multiple conditions, let’s review the basic structure of an if statement in Python. An if statement is used to test a specific condition or expression and execute a block of code if the condition is true. The syntax for an if statement is:
1 2 |
if condition: # Your code here |
For example, here’s a simple if statement checks if a number is even:
1 2 3 4 |
number = 4 if number % 2 == 0: print("The number is even.") |
In this code snippet, we are checking if the remainder of the division of the number by 2 is 0. If the condition is true, it prints “The number is even.”
Step 2: Adding Multiple Conditions Using And, Or, and Not Operators
To check multiple conditions within an if statement, you can use the and, or, and not operators.
- and: This operator returns true if both conditions are true.
- or: This operator returns true if at least one of the conditions is true.
- not: This operator returns the opposite of the condition (if the condition is true, it returns false, and vice versa).
Here’s an example of how to use these operators:
1 2 3 4 5 |
age = 25 income = 50000 if age >= 21 and income >= 50000: print("Eligible for loan.") |
In this example, we are checking if a person is eligible for a loan based on their age and income. The loan will only be granted if the person is 21 or older and has an income of at least $50,000.
The and operator ensures that both conditions are met.
Step 3: Using Parentheses to Control the Order of Evaluation
When dealing with more complex expressions, it might be necessary to use parentheses to control the order of evaluation. This ensures the correct logical operators are applied to the intended conditions. Here’s an example:
1 2 3 4 5 |
salary = 60000 years_employed = 3 if (age >= 21 and income >= 50000) or (years_employed >= 5): print("Eligible for loan.") |
In this case, the loan will be granted if the person is 21 or older with an income of at least $50,000 or has been employed for at least 5 years. The parentheses make sure that the and operator is applied to age and income before the or operator is applied to the result.
Step 4: Combining And, Or, and Not Operators
You can combine and, or, and not operators to build even more complex conditions. Here’s an example:
1 2 3 4 |
credit_score = 700 if (age >= 21 and income >= 50000) or (years_employed >= 5) and not credit_score < 650: print("Eligible for loan.") |
In this example, the loan is granted if the person is 21 or older with an income of at least $50,000 or has been employed for at least 5 years, but must not have a credit score below 650.
Full Code Example
1 2 3 4 5 6 7 |
age = 25 income = 50000 years_employed = 3 credit_score = 700 if (age >= 21 and income >= 50000) or (years_employed >= 5) and not credit_score < 650: print("Eligible for loan.") |
Output
Eligible for loan.
Conclusion
In this tutorial, you learned how to check multiple conditions in an if statement in Python using and, or, and not logical operators. By combining these operators and using parentheses to control the order of evaluation, your code can make more nuanced and complex decisions based on various conditions.
Keep practicing, and you’ll soon master the art of crafting intricate if statements in Python.