Operators in Python - KeithChamberlain/PythonJourney GitHub Wiki

Python Operators

Operators in python are a bit tricky when coming from just about any other language or environment, such as R or Excel. The usual suspects:

  • add +,
  • subtracts/negation -,
  • gets/equals =,
  • multiplication *,
  • division / and
  • modulo %

are ok to deal with and transfer from other languages just fine. However:

  • integer division //, also called floor division, is introduced, and
  • exponentiation ** takes on an atypical operator that matches what is used in SAS.

Different from R, the binary operators are whole words in python, just like they are in Excel. Only different from Excel, the binary operators in python are reserved words and are not function calls. See bellow:

  • and rather than & in R or AND() in Excel
  • or rather than | in R or OR() in Excel
  • not rather than ! (bang) in R or NOT() in Excel

I felt disappointed that other binary operators are not included in python, such as exclusive or. To be sure, xor is implemented as a bitwise operator ^. To get it to work as xor on strings, one needs to call bool(str) first.

Assignment Operators

Assignment operators are just AWESOME in python. There's all sorts of modifications on gets = to simplify coding, inspired by C.

  • +=, -=, /=, *=, %=, //=, **=
  • Also the bitwise operators gets.

The bitwise operators I don't plan to get into just yet.

Comparison Operators

  • ==, !=, <=, >=, <, >
⚠️ **GitHub.com Fallback** ⚠️