Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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
  2. 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
  2. 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
  2. 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".

Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.