4 min read•june 24, 2024
logic are fundamental to programming, allowing us to create complex conditions and control program flow. By combining like 'and', '', and '', we can evaluate multiple expressions and make decisions based on their outcomes.
These concepts are crucial for writing efficient and flexible code. Understanding and helps us analyze and simplify logical expressions, leading to clearer and more maintainable programs.
and
operator returns [True](https://www.fiveableKeyTerm:TRUE)
if both operands are True
, otherwise returns [False](https://www.fiveableKeyTerm:FALSE)
()
(x > 0) and (x < 10)
is True
if x
is between 0 and 10 (exclusive)or
operator returns True
if at least one operand is True
, otherwise returns False
()
(x < 0) or (x > 10)
is True
if x
is less than 0 or greater than 10not
operator returns the opposite Boolean value of the operand ()
not (x > 0)
is True
if x
is less than or equal to 0and
operator syntax: expression1 and expression2
True
only if both expression1
and expression2
are True
(age >= 18) and (age <= 65)
checks if age is between 18 and 65 (inclusive)or
operator syntax: expression1 or expression2
True
if at least one of expression1
or expression2
is True
(score < 60) or (absences > 5)
checks if score is below 60 or absences exceed 5not
operator syntax: not expression
expression
not (is_weekend)
is True
if is_weekend
is False
if-else
statements execute code conditionally based on Boolean expressions
if
block runs if the condition is True
, else
block runs if the condition is False
if condition: # Code to execute if condition is True else: # Code to execute if condition is False
if-else
statements
if (temperature > 30) and (humidity > 60): print("It's hot and humid") else: print("The weather is pleasant")
Truth tables visualize all possible combinations of input values and their corresponding output values (truth values)
Truth table for the
and
operator:
A | B | A and B |
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Truth table for the
or
operator:
A | B | A or B |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Truth table for the
not
operator:
A | not A |
True | False |
False | True |
Compound Boolean expressions can be analyzed using truth tables
(A and B) or (not C)
if-elif-else
statements check multiple conditions in sequence
elif
(short for "else if") blocks run if the preceding if
or elif
conditions are False
if
or elif
block with a True
condition is executedif condition1: # Code to execute if condition1 is True elif condition2: # Code to execute if condition1 is False and condition2 is True else: # Code to execute if all preceding conditions are False
if-else
statements create more complex decision-making structures
if-else
statements can be placed inside other if
, elif
, or else
blocksif age >= 18: if has_license: print("You can drive") else: print("You need a license to drive") else: print("You are too young to drive")