Skip to main content

Logical Operators

To join conditional statements together, use logical operators:

Logical Operators Table

main.py
# Operator | Description                                             | Example
# and      | Returns True if both statements are true                | x < 5 and x < 10
# or       | Returns True if one of the statements is true           | x < 5 or x < 4
# not      | Reverse the result, returns False if the result is true | not(x < 5 and x < 10)

Examples

Example

See whether a number is above 0 and below 10:

Prompt
Write a Python program that sets x = 5 and uses the "and" operator to test if x is greater than 0 and less than 10. Print the result.
Output: complete Python program

Example

See whether a number is below 5 or above 10:

Prompt
Write a Python program that sets x = 5 and uses the "or" operator to test if x is less than 5 or greater than 10. Print the result.
Output: complete Python program

Example

Flip the outcome using the not operator:

Prompt
Write a Python program that sets x = 5 and uses the "not" operator to reverse the result of (x > 3 and x < 10). Print the result.
Output: complete Python program