Skip to main content

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: Returns True if both operands are true.

    print(True and True)   # Output: True
    print(True and False) # Output: False
  • or: Returns True if 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 is False, the whole expression must be False, so the second operand is not evaluated.
  • For or, if the first operand is True, the whole expression must be True, so the second operand is not evaluated.

🛠️ Section 3: Bitwise Operators

Bitwise operators work on integers, treating them as sequences of binary digits.

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
``OR
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero fill left shiftShifts left by pushing zeros in from the right
>>Signed right shiftShifts 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, not are 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/or with &/|.

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.

Further Reading (Python Resources)