Last Updated : 23 Jul, 2025
In Python, if
statement is a conditional statement that allows us to run certain code only if a specific condition is true. By combining it with the AND
operator, we can check if all conditions are true, giving us more control over the flow of our program.
Example : This program checks are you eligible to vote or not .
Python
a = 20 # age
b = True # Citizen status
if a >= 18 and b:
print("Eligible")
else:
print("Ineligible")
We can use the if with AND operator in many ways. Let's understand each one, step by step.
To validate user's inputThe most common use case of an if
statement with the and
operator is to check if the data is correct and meets certain criteria.
Example :
Python
a = 23 # age
b = "yes" # permission
if a >= 18 and b == "yes":
print("Granted")
else:
print("Denied")
Explanation:
a
is 18 or older and b
is "yes".Python if with and operator is mostly use to check a password is strong and enough to keep accounts secure.
Example :
Python
p = "securePass123" #password
if len(p) >= 8 and any(char.isdigit() for char in p):
print("Valid")
else:
print("Invalid")
Explantion:
p
is at least 8 characters and contains a digit.Python if
with the and
operator is commonly used to check multiple conditions such as health and status.
Example :
Python
a = 50 # health
b = True # has_weapon
if a > 0 and b:
print("Fight")
else:
print("No Fight")
Explanation:
a
is positive and b
is True
.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4