Python: Operators

By Xah Lee. Date: . Last updated: .

Arithmetic Operators

Addition

print(3 + 4)  # 7

Substract

print(3 - 4)  # -1
print(3 + -4)  # -1

Multiplication

print(3 * 4)  # 12

Power

print(2**3)  # 8

Divide

print(11 / 5)  # 2.2

Integer Quotient

print(11 // 5)  # 2

Remainder (modulo)

print(11 % 5)  # 1

Quotient and remainder

print(divmod(11, 5))  # (2, 1)

Log

import math

# math.log(arg, base)

# 2**x == 8
# x == math.log(8, 2)

print(math.log(8, 2))
# 3.0

Python, Operators