Python 3: Operators

By Xah Lee. Date: . Last updated: .

Arithmetic Operators

Arithmetic
purposesyntaxfunction
additiona + badd(a, b)
Subtractiona - bsub(a, b)
Negation- aneg(a)
Multiplicationa * bmul(a, b)
Divisiona / btruediv(a, b)
Divisiona // bfloordiv(a, b)
Moduloa % bmod(a, b)
Exponentiationa ** bpow(a, b)

The “function” column is the function from the “operator” module. You can use it after you import operator. example:

# -*- coding: utf-8 -*-
# Python 3

import operator

print(operator.add(3,4))  # prints 7

Assignment Operators

# -*- coding: utf-8 -*-
# Python 3

# add and assign
c = 0
c += 1
print(c)    # 1

# substract and assign
c = 0
c -= 2
print(c)    # -2

# multiply and assign
c = 2
c *= 3
print(c)    # 6

# exponent and assign
c = 3
c **= 2
print(c)    # 9

# divide and assign
c = 7
c /= 2
print(c)    # 3.5

# modulus (remainder) and assign
c = 13
c %= 5
print(c)    # 3

# quotient and assign
c = 13
c //= 5
print(c)    # 2

Note: Python doesn't support ++ or --.

Warning: ++i may not generate any error, but it doesn't do anything.

Bit Operators

Bit Operators
purposesyntaxfunction
anda & band_(a, b)
ora | bor_(a, b)
xora ^ bxor(a, b)
invert~ ainvert(a)
Right Shifta >> brshift(a, b)
Left Shifta << blshift(a, b)
# -*- coding: utf-8 -*-
# Python 3

# 0b101 would mean binary 101
# the "{:2b}" means print in binary, and use 2 slots.

# bitwise And
print( "{:2b}".format(0b11 & 0b10) )   # binary 10


# bitwise Or
print( "{:2b}".format(0b11 | 0b10) )   # binary 11

# xor. true if both sides differ
print( "{:2b}".format(0b00 ^ 0b10) )   # binary 10

# binary invert
print( "{:2b}".format(~ 0b1) )   # binary -10  (odd result!)

Sequence Operators

Sequence Operators
purposesyntaxfunction
get itemobj[k]getitem(obj, k)
assign itemobj[k] = vsetitem(obj, k, v)
delete itemdel obj[k]delitem(obj, k)
joinseq1 + seq2concat(seq1, seq2)
if existobj in seqcontains(seq, obj)
format strings % objmod(s, obj)

Boolean Operators

Boolean Operators
purposesyntaxfunction
to booleanobjtruth(obj)
notnot anot_(a)
equala == beq(a, b)
not equala != bne(a, b)
less thana < blt(a, b)
less equala <= ble(a, b)
greatera > bgt(a, b)
greater equala >= bge(a, b)
same objecta is bis_(a, b)
not same objecta is not bis_not(a, b)

If you have a question, put $5 at patreon and message me.

Python by Example

  1. Python Basics
  2. Print Version String
  3. Builtin Help
  4. Quote String
  5. String Operations
  6. String Methods
  7. Format String
  8. True, False
  9. if then else
  10. for, while, Loops
  11. List Basics
  12. Loop Thru List
  13. Map Function to List
  14. List Comprehension
  15. List Methods
  16. Dictionary
  17. Loop Thru Dict
  18. Dict Methods
  19. Function
  20. Class
  21. List Modules
  22. Write a Module
  23. Unicode 🐍

Regex

  1. Regex Basics
  2. Regex Reference

Text Processing

  1. Read/Write File
  2. Traverse Directory
  3. Manipulate Path
  4. Process Unicode
  5. Convert File Encoding
  6. Find Replace in dir
  7. Find Replace by Regex
  8. Count Word Frequency

Web

  1. Send Email
  2. GET Web Page
  3. Web Crawler
  4. HTTP POST

Misc

  1. JSON
  2. Find Script Path
  3. Get Env Var
  4. System Call
  5. Decompress Gzip
  6. Complex Numbers
  7. Sort
  8. Copy Nested List
  9. Tuple vs List
  10. Sets, Union, Intersection
  11. Closure in Python 2
  12. Decorator
  13. Append String in Loop
  14. Timing f timeit
  15. Keyword Arg Default Value Unstable
  16. Check Page Load Size
  17. Thumbnail Generation

Python 3

  1. Python 3 Basics
  2. Print Version String
  3. Quoting String
  4. String Operations
  5. Format String
  6. Operators
  7. Object, ID, Type
  8. Traverse Directory
  9. Sort List, Matrix, Object
  10. Python 3: Map with Side Effect Doesn't Work If Result is Not Used
  11. Python 3 Closure
  12. Python 2 and 3 Difference