Previous Lesson: Lesson 13. (Part II) Methods and Functions on Lists in Python

Let’s memorize the Boolean logic in Python. You’ll have a practice for this one. After your practice, you’ll know how you can check your answers on Python.

In this lesson, you won’t be learning any complex theories that academics love to study. Instead, you’ll learn just the simple basic logic that make real programs work and that real programmers need every day.

Boolean Logic Terms

In Python we have the following terms (characters and phrases) for determining if something is True or False. Additionally, logic on a computer is all about seeing if some combination of these characters and some variables is True at that point in the program. We’ve already talked about this but there’s a lot to learn about them.

  • and
  • or
  • not
  • != (not equal)
  • == (equal)
  • >= (greater-than-equal)
  • <= (less-than-equal)
  • True
  • False

On the following below, these are the some tables that I want you to memorize:

You can have your own methods to memorize these. As a suggestion, you can write a table in an index card (so there would be seven index cards). Also, I’ll give you a week to memorize all of them. Remember though, there is no failing in this lesson, just trying as hard as you can each day, and then a little bit more.

If you’ve memorize all these tables after using a whole week, or even without using a whole week, then, go practice!

Practice for Boolean Logic!

Only do this once you’ve convinced yourself that you really memorized every table without even looking for once. Afterwards, write each of these logic problems below on a separate piece of paper:

  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”))

Once you’re done writing these all, write what you think would be the answer for these logics. In each case, it will be either True or False.

Simple Technique in Memorizing Boolean Logic

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. For example, once you encounter the symbol ==, then change it into “equal to”.
  2. Look for 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.

Start Python for Boolean Logic Checking

Once you have the answers written down, you will start Python in your Terminal. You’ll do this to check each logic problem in to confirm your answers. To start Python, type in ‘python3’ in your Terminal like this:

Once you’ve press ENTER, it will look something like this. Don’t worry if yours doesn’t look exactly like mine:

Now, you can type in the problems I’ve given you above from numbers 1-20 to confirm your answers, one by one.

Note: Type the exact capitalization, words, and characters from the given problem. Saying ‘true and true‘ will be invalid. It should be ‘True and True‘.

Tip: Any and expression that has a False is immediately False. Any or expression that has a True is immediately True.

Next Lesson: Lesson 15: Range in Python

2 Replies to “Lesson 14. Boolean Logic in Python”

Leave a Reply

Your email address will not be published.