And, Or, Not, randint
CSD1010: Week 2B
Wiedemann • Fall 2025
Logical Operator: and
- Evaluates to
True only if both sides are True.
| A | B | A and B |
True | True | True |
True | False | False |
False | True | False |
False | False | False |
if temperature > 70 and is_sunny:
print("Perfect beach weather!")
Logical Operator: or
- Evaluates to
True if either side is True.
| A | B | A or B |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
if is_weekend or is_holiday:
print("No school today!")
Logical Operator: not
- Flips the truth value of a boolean expression.
not True becomes False.
not False becomes True.
| A | not A |
True | False |
False | True |
if not is_raining:
print("Good day for a walk")
Operator Precedence
- Python checks
not first, then and, then or when evaluating compound conditionals.
- In any case, it is safest to use parentheses to make your intentions clear.
# Without parentheses: is this right? (No.)
can_go = has_ticket or has_pass and not is_full
# With parentheses: less error-prone, clearer meaning
can_go = (has_ticket or has_pass) and not is_full
Random Integers
- The
random module provides various functions for generating randomness.
random.randint(a, b) returns a random integer between a and b (inclusive).
import random
# Simulate a dice roll
roll = random.randint(1, 6)
print(f"You rolled a {roll}")