Logical Operators & Conditional Statements
What are Logical Operators?
AND (and): True if both conditions are True
OR (or): True if at least one condition is True
NOT (not): Reverses the Boolean value (True becomes False)
Truth Table - AND
A B A and B
True True True
True False False
False True False
False False False
Truth Table - OR
A B A or B
True True True
True False True
False True True
False False False
Truth Table - NOT
A not A
True False
False True
Python Code Examples
a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False
Conditional Statements
if condition:
# do something
elif another_condition:
# do something else
else:
# fallback
Example: Age Check
age = 18
if age >= 18:
print('You can vote')
else:
print('You are too young')
Logical Operators in Conditions
is_student = True
has_id = False
if is_student and has_id:
print('You get a discount')
else:
print('No discount')
Practice Questions
1. print(True or False and False)
2. if not (5 > 3): print('Hello') else: print('Bye')
3. Write a program to check if a number is positive and even.
Wrap-Up & Summary
- Logical operators help make decisions with multiple conditions.
- Conditional statements control the program flow based on logic.