Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

4.3: Boolean Operations

( \newcommand{\kernel}{\mathrm{null}\,}\)

Learning Objectives

By the end of this section you should be able to

  • Explain the purpose of logical operators.
  • Describe the truth tables for and, or, and not.
  • Create expressions with logical operators.
  • Interpret if-else statements with conditions using logical operators.

Logical operator: and

Decisions are often based on multiple conditions. Ex: A program printing if a business is open may check that hour >= 9 and hour < 17. A logical operator takes condition operand(s) and produces True or False.

Python has three logical operators: and, or, and not. The and operator takes two condition operands and returns True if both conditions are true.

p q p and q
True
True
True
True
False
False
False
True
False
False
False
False
Table 4.1 Truth table: p and q.
Checkpoint: Example: Museum entry
Concepts in Practice: Using the and operator
1.
Consider the example above. Jaden tries to enter when the capacity is 2500 and there are 2 hours before close. Can Jaden enter?
  1. yes
  • no
  • 2.
    Consider the example above. Darcy tries to enter when the capacity is 3000. For what values of hrs_to_close will Darcy to be able to enter?
    1. hrs_to close > 1.0
    2. no such value
    3.
    Given is_admin = False and is_online = True, what is the value of is_admin and is_online?
    1. True
    2. False
    4.
    Given x = 8 and y = 21, what is the final value of z?
    if (x < 10) and (y > 20):
      z = 5
    else:
      z = 0
    
    1. 0
    2. 5

    Logical operator: or

    Sometimes a decision only requires one condition to be true. Ex: If a student is in the band or choir, they will perform in the spring concert. The or operator takes two condition operands and returns True if either condition is true.

    p q p or q
    True
    True
    True
    True
    False
    True
    False
    True
    True
    False
    False
    False
    Table 4.2 Truth table: p or q.
    Checkpoint: Example: Streaming prompt
    Concepts in Practice: Using the or operator
    5.
    Given days = 21 and is_damaged is False, is the refund processed?
    if (days < 30) or is_damaged:
      # Process refund
    
    1. yes
  • no
  • 6.
    For what values of age is there no discount?
    if (age < 12) or (age > 65):
      # Apply student/senior discount
    
    1. age >= 12
    2. age <= 65
    3. (age >= 12) and (age <= 65)
    7.
    Given a = 9 and b = 10, does the test pass?
    if (a%2 == 0 and b%2 == 1) or (a%2 == 1 and b%2 == 0):
      # Test passed
    else:
      # Test failed
    
    1. yes
    2. no

    Logical operator: not

    If the computer is not on, press the power button. The not operator takes one condition operand and returns True when the operand is false and returns False when the operand is true.

    not is a useful operator that can make a condition more readable and can be used to toggle a Boolean's value. Ex: is_on = not is_on.

    p not p
    True
    False
    False
    True
    Table 4.3 Truth table: not p.
    Checkpoint: Example: Diving warning
    Concepts in Practice: Using the not operator
    8.
    Given x = 13, what is the value of not(x < 10)?
    1. True
  • False
  • 9.
    Given x = 18, is x in the correct range?
    if not(x > 15 and x < 20):
      # x in correct range
    
    1. yes
    2. no
    10.
    Given is_turn = False and timer = 65, what is the final value of is_turn?
    if timer > 60:
      is_turn = not is_turn
    
    1. True
    2. False
    Try It: Speed limits

    Write a program that reads in a car's speed as an integer and checks if the car's speed is within the freeway limits. A car's speed must be at least 45 mph but no greater than 70 mph on the freeway.

    If the speed is within the limits, print "Good driving". Else, print "Follow the speed limits".


    This page titled 4.3: Boolean Operations is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    Support Center

    How can we help?