Logic Operators

Logic operators are used to combine "truth values", or booleans.

logic_operators.py

# Logic Operators

# and
were_cookies_bought = True
were_chips_bought = True
print(
    "Were both cookies and chips bought? "
    + str(were_cookies_bought and were_chips_bought)
)

# or
was_computer_bought = True
was_bike_bought = False
print("Was a computer or bike bought? " + str(was_computer_bought or was_bike_bought))

# not
is_raining = False
print("Is it NOT raining? " + str(not is_raining))

View on GitHub

Practice

Decisions 1

Write in Python syntax what you would say if you wanted either 5 dollars OR (2 drinks AND 1 snack)

💻 Template code

Solution code

Decisions 2

Write in Python syntax what you would say if you wanted Not a drink OR Not a cookie

💻 Template code

Solution code

Previous Section

3.1 Mathematical Operators

Next Section

3.3 Comparison Operators

<aside> ⚖️ Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License.

If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact [email protected] for inquiries.

</aside>