And, Or, Not, randint

CSD1010: Week 2B
Wiedemann • Fall 2025

Logical Operator: and

  • Evaluates to True only if both sides are True.
ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
if temperature > 70 and is_sunny:
    print("Perfect beach weather!")

Logical Operator: or

  • Evaluates to True if either side is True.
ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse
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.
Anot A
TrueFalse
FalseTrue
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}")