Python Boolean Connectives: Bitwise or Logical?

I’m looking for some teacher-based feedback here on Python’s bitwise and logical Boolean connectives.

Traditionally, I have taught my students about the existence, meaning, and function of and, or, ^, not.

More recently, I discovered that &, |, ~ were substitutes for and, or, not. Or so I thought.

I’m realizing that the single-symbol bitwise operators usually behave similarly to the logical one, but in the case of precedence of operations the bitwise versions are handled first, followed by the logical/traditional ones.

I believe order of precedence is:
~
&
^
|
not
and
or

My multiple choice question is this: In your professional opinion, what is ideal for an introductory-level Python course?

A) Teaching only the logical not, and, or
B) Teaching only the bitwise ~, &, ^, |
C) Teaching both logical and bitwise
D) Other (please elaborate)

Thanks for all of your professional opinions!

Not a teacher.

A. Bitwise operators are different to logical operators. You might want to take a look at this:

That is a helpful reply, because I do want to be cognizant of the unintended consequences of what I teach.

Whereas my students would see no difference in:

if(answer1 == "y") and (answer2 == "y"):
if(answer1 == "y") & (answer2 == "y"):

it seems you are suggesting it is possible that this could create a misunderstanding later on down the line that has consequences, no?

Thanks for your feedback.

1 Like

I believe these would have the same effect, but it’s important to know where & would be different from and. It could be confusing for complete beginners to Python, though.

Exactly.

I also teach mathematics, and I have to undo the PEMDAS order of operations taught to students to explain that multiplication and division are functionally equivalent and thus share precedence moving left to right.

Teaching them order of operations the first time in the primary grades is made easier by cutting away that abstraction, but we must make the correction later. The kids adapt to that pretty easily overall, but it is still annoying to them that they weren’t taught correctly in the first place.

That’s a classic teacher issue there: simplify for a better early understanding or be fully upfront from the start.

Thank you!

In my opinion, it is better to teach only the logical operators (and, or, not).
The bitwise operators are generally useless unless you are manipulating ints bitwise for some reason, or you are using sets. Python programmers never use the bitwise operators for any boolean stuff (though you could). Even xor ^, with no keyword equivalent, isn’t really used with bools.
So it’s best to not accidentally introduce bad practices, which could sometimes lead to confusing bugs.

See the very useful python documention, including an operator precedence table:

Also, did you know that and and or don’t have to return bools? (a mostly useless piece of information)

print([] or 0.0)  # prints '[]'
1 Like

Bit off-topic, but that’s how PEMDAS works, though?

Where would and not return a bool?

4 posts were split to a new topic: Python “and” doesn’t always return bool

And poor context in my part about PEMDAS. Early learners learn “multiplication, then division” and are never presented with an expression that puts division first so that the issue of reading left to right isn’t presented at that time.

In upper grades, we have to correct that notion to explain that they take equal precedence.

My analogy here is that using & interchangeably with and is a similar bargain. Some hotshot will email me eight years after I have them in class and say that their professor vilified me for doing it wrong :slight_smile: