Bitwise and Comparison Operators

The Ancient Bitwise Operators (Yet really useful)

Bitwise Operators directly manipulate bits. There really is no speed advantage to bitwise operators in Python. The main advantage of bitwise operators is for scripting, compression, encryption, communication over sockets (checksums, stop bits, control algorithms, etc).

Bitwise Operators:

  • x << y #returns x left-shifted y places​

  • x >> y #returns x right-shifted y places​

  • x & y #bitwise and​

  • x | y #bitwise or​

  • x ^ y #bitwisexor​

  • ~x #returns the ones complement

Comparison Operators

  • x == y #if x equals y, return True​

  • x != y #if x is not y, return True​

  • x <> y #same as != (deprecated)​

  • x > y #if x is greater than y, return True​

  • x < y #if x is less than y, return True​

  • x >= y #if x is greater or equal to y, return True​

  • x <= y #if x is less or equal to y, return True

Membership Operators

  • in #Evaluates to true if it finds a variable in the checked sequence
  • not in #Evaluates to true if it does not find a variable in the checked sequence

Identity Operators

  • is #Evaluates to true if variables on either side of the operator point to the same object
  • is not #Evaluates to true if the variables on either side of the operator does not point to the same object

Boolean Operators

  • and #Evaluates true if expressions on both sides of the operator are true.
  • or #Evaluates true if expressions on either side of the operator are true.

Lab 3A: Operators

Open up the Python Interpreter and experiment with different types and operators. Here are some suggestions:

  • Compare string numeric values vs number values
  • Create lists, tuples and dicts... experiment with the in/is operators

results matching ""

    No results matching ""