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. How would we write a Boolean expression to check if someone is at least 16 years old?

age >= 16

To make this show you would run the following commands :

age = 17 # Example age

is_eligible_to_work = age >= 16

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

  • A company offers free shipping for orders of at least $50. Write a Boolean expression to check if the total order amount qualifies for free shipping. ```python (cost1 + cost2 + cost3)/3 >= 50

  • We can also run the following :

order_amount = 55 # Example order amount is_eligible_for_free_shipping = order_amount >= 50 print(is_eligible_for_free_shipping) # Output: True if order amount is 50 or more, False otherwise

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. Write the Boolean expression for this scenario.

(score >= 10 AND lives == 5) OR (score == 50 AND lives > 0)

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

(height1 + height2 + height3) / 3 >= 65

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

(height1 + height2 + height3) / 3 >= 65

    <script src="https://utteranc.es/client.js"
        repo="manas12709/portfolio_2025"
        issue-term="pathname"
        label="utterances"
        theme="github-light"
        crossorigin="anonymous"
        async>
    </script>