Introduction
Logical operators help us combine multiple true or false checks to make decisions in our programs.
Jump into concepts and practice - no test required
if condition1 and condition2: # do something if condition1 or condition2: # do something if not condition: # do something
age = 20 if age >= 18 and age <= 30: print("You are a young adult.")
day = "Saturday" if day == "Saturday" or day == "Sunday": print("It's the weekend!")
is_raining = False if not is_raining: print("You don't need an umbrella.")
temperature = 25 is_sunny = True if temperature > 20 and is_sunny: print("It's a nice day for a walk.") if temperature < 10 or not is_sunny: print("Better stay inside.")
and operator returns true only if both conditions are true.or needs only one true condition, not reverses truth, and xor is not a Python keyword.x is NOT equal to 10 and variable y is greater than 5?!= is the correct 'not equal' operator in Python; <> is invalid syntax.and correctly combines two conditions; & is a bitwise operator and or changes logic.age = 20
has_id = False
if age >= 18 and has_id:
print('Allowed')
else:
print('Denied')age >= 18 is True since 20 >= 18, but has_id is False.else block runs.if not x > 10 or y < 5
print('Check passed')not and or is correct; indentation is not shown as wrong here.n is either less than 0 or greater than 100, but NOT equal to -10. Which condition correctly uses logical operators?and n != -10. if n < 0 or (n > 100 and n != -10): excludes -10 only when >100, not <0 (e.g., n=-10 is true). if not (n < 0 or n > 100) and n == -10: reverses logic and checks for equal -10. if n < 0 and n > 100 or n != -10: mixes and/or incorrectly.