The logical operators and
and or
in Python are used to combine multiple conditions in if-else
statements. These operators allow you to create complex conditional logic by combining simpler Boolean expressions.
and
Operator:and
operator returns True
if both conditions are True
. Otherwise, it returns False
.condition1 and condition2
or
Operator:or
operator returns True
if at least one of the conditions is True
. If both conditions are False
, it returns False
.condition1 or condition2
and
in if-else
StatementsThe and
operator is used when you want to check if multiple conditions are all True
.
age = 25
has_license = True
if age >= 18 and has_license:
print("You are allowed to drive.")
else:
print("You are not allowed to drive.")
if
statement checks if age
is greater than or equal to 18 and if has_license
is True
.True
, it prints “You are allowed to drive.”False
, it prints “You are not allowed to drive.”math_score = 85
science_score = 90
if math_score >= 75 and science_score >= 75:
print("You passed both subjects.")
else:
print("You did not pass both subjects.")
if
statement checks if both math_score
and science_score
are greater than or equal to 75.True
, it prints “You passed both subjects.”False
, it prints “You did not pass both subjects.”or
in if-else
StatementsThe or
operator is used when you want to check if at least one of the conditions is True
.
is_student = True
is_senior = False
if is_student or is_senior:
print("You are eligible for a discount.")
else:
print("You are not eligible for a discount.")
if
statement checks if is_student
is True
or if is_senior
is True
.True
, it prints “You are eligible for a discount.”False
, it prints “You are not eligible for a discount.”has_admin_rights = False
has_editor_rights = True
if has_admin_rights or has_editor_rights:
print("You have access to edit the content.")
else:
print("You do not have access to edit the content.")
if
statement checks if has_admin_rights
is True
or if has_editor_rights
is True
.True
, it prints “You have access to edit the content.”False
, it prints “You do not have access to edit the content.”and
and or
You can combine and
and or
operators to create more complex conditions. Use parentheses ()
to ensure the correct order of evaluation.
age = 20
has_permission = True
is_vip = False
if (age >= 18 and has_permission) or is_vip:
print("Access granted.")
else:
print("Access denied.")
if
statement first checks if (age >= 18 and has_permission)
is True
. If both conditions inside the parentheses are True
, it evaluates to True
.False
, it then checks if is_vip
is True
.(age >= 18 and has_permission)
is True
or is_vip
is True
, it prints “Access granted.”False
, it prints “Access denied.”Using the and
and or
logical operators in if-else
statements allows you to handle complex logical conditions efficiently. By understanding how these operators work, you can create more readable and maintainable code. Always remember to use parentheses ()
to clarify the order of evaluation when combining multiple conditions.