Operators: Logical and Bitwise Operators
Following our exploration of Operators: Comparison Operators, this article dives into two more categories of operators: logical operators and bitwise operators.
📚 Prerequisites
A basic understanding of Python variables and boolean data types.
🎯 Article Outline: What You'll Master
In this article, you will learn:
- ✅ How to use logical operators (
and,or,not) to combine boolean expressions. - ✅ The concept of short-circuit evaluation.
- ✅ What bitwise operators are and how they work.
- ✅ The difference between logical and bitwise operators.
🧠 Section 1: Logical Operators
Logical operators are used to combine and modify boolean expressions.
-
and: ReturnsTrueif both operands are true.print(True and True) # Output: True
print(True and False) # Output: False -
or: ReturnsTrueif at least one operand is true.print(True or False) # Output: True
print(False or False) # Output: False -
not: Reverses the boolean value.print(not True) # Output: False
print(not False) # Output: True
💻 Section 2: Short-Circuit Evaluation
Python's logical operators use short-circuit evaluation. This means that the second operand is only evaluated if the first one is not enough to determine the result.
- For
and, if the first operand isFalse, the whole expression must beFalse, so the second operand is not evaluated. - For
or, if the first operand isTrue, the whole expression must beTrue, so the second operand is not evaluated.
🛠️ Section 3: Bitwise Operators
Bitwise operators work on integers, treating them as sequences of binary digits.
| Operator | Name | Description |
|---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| ` | ` | OR |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shifts left by pushing zeros in from the right |
>> | Signed right shift | Shifts right by pushing copies of the leftmost bit in from the left |
🔬 Section 4: Logical vs. Bitwise Operators
It's important not to confuse logical operators (and, or) with bitwise operators (&, |).
- Logical operators work on boolean values (
True,False) and are used for controlling the flow of your program. - Bitwise operators work on integers and are used for low-level manipulation of bits.
💡 Conclusion & Key Takeaways
You've now learned about logical and bitwise operators in Python.
Let's summarize the key takeaways:
- Logical Operators:
and,or,notare used to combine boolean expressions. - Short-Circuit Evaluation: Python's logical operators are efficient.
- Bitwise Operators:
&,|,^,~,<<,>>are used for low-level bit manipulation. - Logical vs. Bitwise: Don't confuse
and/orwith&/|.
Challenge Yourself: Write a script that takes a number as input and checks if it's both positive and even.
➡️ Next Steps
In the next article, we'll learn about "Operator Precedence and Associativity".
Happy coding!
Glossary (Python Terms)
- Logical Operator: An operator that combines or modifies boolean expressions.
- Bitwise Operator: An operator that works on the binary representation of integers.
- Short-Circuit Evaluation: A strategy where the second argument of a boolean operator is executed only if the first argument does not suffice to determine the value of the expression.