Video Pending

Created by Zed A. Shaw Updated 2024-02-17 04:54:36
 

29: Boolean Practice

The logic combinations you learned from the last exercise are called "Boolean" logic expressions. Boolean logic is used everywhere in programming. It is a fundamental part of computation, and knowing them very well is akin to knowing your scales in music.

In this exercise you will take the logic exercises you memorized and start trying them out in Python. Take each of these logic problems and write what you think the answer will be. In each case it will be either True or False. Once you have the answers written down, you will start Python in your terminal and type each logic problem in to confirm your answers.

  1. True and True
  2. False and True
  3. 1 == 1 and 2 == 1
  4. "test" == "test"
  5. 1 == 1 or 2 != 1
  6. True and 1 == 1
  7. False and 0 != 0
  8. True or 1 == 1
  9. "test" == "testing"
  10. 1 != 0 and 2 == 1
  11. "test" != "testing"
  12. "test" == 1
  13. not (True and False)
  14. not (1 == 1 and 0 != 1)
  15. not (10 == 1 or 1000 == 1000)
  16. not (1 != 10 or 3 == 4)
  17. not ("testing" == "testing" and "Zed" == "Cool Guy")
  18. 1 == 1 and (not ("testing" == 1 or 1 == 0))
  19. "chunky" == "bacon" and (not (3 == 4 or 3 == 3))
  20. 3 != 3 and (not ("testing" == "testing" or "Python" == "Fun"))

I will also give you a trick to help you figure out the more complicated ones toward the end.

Whenever you see these Boolean logic statements, you can solve them easily by this simple process:

  1. Find an equality test (== or !=) and replace it with its truth.
  2. Find each and/or inside parentheses and solve those first.
  3. Find each not and invert it.
  4. Find any remaining and/or and solve it.
  5. When you are done you should have True or False.

I will demonstrate with a variation on #20:

3 != 4 and not ("testing" != "test" or "Python" == "Python")

Here's me going through each of the steps and showing you the translation until I've boiled it down to a single result:

  1. Solve each equality test:

    • 3 != 4 is True, so replace that with True to get True and not ("testing" != "test" or "Python" == "Python")

    • "testing" != "test" is True so replace that with True to get True and not (True or "Python" == "Python")

    • "Python" == "Python" is True so replace that with True and we have True and not (True or True)

  2. Find each and/or in parentheses ():

    • (True or True) is True so replace that to get True and not (True)
  3. Find each not and invert it:

    • not (True) is False so replace that and we have True and False
  4. Find any remaining and/or and solve them:

    • True and False is False and you're done.

With that we're done and know the result is False.

The more complicated ones may seem very hard at first. You should be able to take a good first stab at solving them, but do not get discouraged. I'm just getting you primed for more of these "logic gymnastics" so that later cool stuff is much easier. Just stick with it, and keep track of what you get wrong, but do not worry that it's not getting in your head quite yet. It'll come.
Previous Lesson Next Lesson

Register for Learn Python the Hard Way, 5th Edition (2023-2024)

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.