Boolean Python

  • A Boolean value can only be true or false.
  • A Boolean expression, when evaluated, results in a Boolean value of either true or false.

Relational operators!

  • Used to test the relationship between 2 variables, expressions, or values. These relational operators are used for comparisons and they evaluate to a Boolean value (true or false).

Ex. a > b is only true if a is greater than b otherwise it is false

  • a == b (equals)
  • a != b (not equal to)
  • a > b (greater than)
  • a < b (less than)
  • a >= b (greater than or equal to)
  • a <= b (less than or equal to)
# Example: The legal age to drive in California is 16 years old.
# Boolean expression to check if someone is at least 16 years old

age = 17  # Example age

# Check if the person is eligible to drive
is_eligible_to_drive = age >= 16

print(is_eligible_to_drive)  # Output: True if age is 16 or more, False otherwise

True
# A company offers free shipping for orders of at least $50.
# Boolean expression to check if the total order amount qualifies for free shipping

# Example order costs
cost1 = 40
cost2 = 60
cost3 = 50

# Check if the average cost is at least $50
average_cost = (cost1 + cost2 + cost3) / 3
is_eligible_for_free_shipping_average = average_cost >= 50
print("Average cost qualifies for free shipping:", is_eligible_for_free_shipping_average)

# We can also check a specific order amount
order_amount = 55  # Example order amount
is_eligible_for_free_shipping = order_amount >= 50
print("Order amount qualifies for free shipping:", is_eligible_for_free_shipping)

Average cost qualifies for free shipping: True
Order amount qualifies for free shipping: True

Logical operators:

Used to evaluate multiple conditions to produce a single Boolean value.

  • NOT evaluates to true if condition is false, otherwise evaluates to false
  • AND evaluates to true if both conditions are true, otherwise evaluates to false
  • OR evaluates to true if either condition is true or if both conditions are true, otherwise evaluates to false
# Example: You win the game if you score at least 10 points and have 5 lives left 
# OR if you score at least 50 points and have more than 0 lives left.

score = 12  # Example score
lives = 5   # Example lives

# Boolean expression to check if you win the game
is_winner = (score >= 10 and lives == 5) or (score >= 50 and lives > 0)

print("Player wins:", is_winner)

Player wins: True
# Example: Write a Boolean expression to check if the average of height1, height2, and height3 is at least 65 inches.

height1 = 64  # Example height in inches
height2 = 66  # Example height in inches
height3 = 67  # Example height in inches

# Calculate the average height
average_height = (height1 + height2 + height3) / 3

# Boolean expression to check if the average height is at least 65 inches
is_tall_enough = average_height >= 65

print("Average height is at least 65 inches:", is_tall_enough)

Average height is at least 65 inches: True
# Example: Write a Boolean expression to check if the average of height1, height2, and height3 is at least 65 inches.

height1 = 63  # Example height in inches
height2 = 67  # Example height in inches
height3 = 66  # Example height in inches

# Boolean expression to check if the average height is at least 65 inches
average_height = (height1 + height2 + height3) / 3
is_tall_enough = average_height >= 65

print(is_tall_enough)  # Output: True if the average height is at least 65 inches, False otherwise

True