Programming Fundamentals
Boolean Operators
Common Boolean Operators
Boolean operators are used to create logical statements and control the flow of a program based on conditions. The three most common Boolean operators are AND, OR, and NOT.
AND Operator
- Definition: Returns true if both operands are true.
- Practical Use: Used when multiple conditions must be met.
- Example:
if condition1 and condition2: print("this prints when both conditions are true")
OR Operator
- Definition: Returns true if at least one of the operands is true.
- Practical Use: Used when at least one condition needs to be met.
- Example:
if condition1 or condition2:
print("this prints if at least one condition is true")
NOT Operator
- Definition: Returns true if the operand is false, and false if the operand is true.
- Practical Use: Used to reverse the logical state of its operand.
- Example:
if not condition:
print("this will only print if the condition is false")
Boolean operators are fundamental in controlling program flow and making decisions based on multiple conditions.