2.2. Operators

2.2.1. Logic Operators

  • x < y - Less than

  • x <= y - Less or equal

  • x > y - Greater than

  • x >= y - Greater or equal

  • x == y - Equals

  • x != y - Not Equals

  • x is None - Object has value

  • x is not None - Object don't have value

10 < 2              # False
10 <= 2             # False
10 > 2              # True
10 >= 2             # True
10 == 2             # False
10 != 2             # True
10 is None          # False
10 is not None      # True

2.2.2. Arithmetic Operators

  • + - Addition

  • - - Subtraction

  • * - Multiplication

  • / - Division

10 + 2              # 12
10 - 2              # 8
10 * 2              # 20
10 / 2              # 5

2.2.3. Multiplication, Power and Root

  • ** - Power

Listing 8. n-th power of the number
10 ** 2             # 100
2 ** -1             # 0.5
1.337 ** 3          # 2.389979753

pow(10, 2)          # 100
pow(2, -1)          # 0.5
pow(1.337, 3)       # 2.389979753
Listing 9. n-th root of the number
4 ** 0.5            # 2.0
2 ** 0.5            # 1.4142135623730951

pow(4, 0.5)         # 2.0
pow(2, 0.5)         # 1.4142135623730951

2.2.4. Divisions

  • / - Division

  • // - True division

  • % - Modulo division (reminder)

10 / 2              # 5
10 / 4              # 2.5

10 // 2             # 5
10 // 4             # 2

10 % 2              # 0
10 % 4              # 2
Listing 10. Even vs odd
10 % 2 == 0         # True
11 % 2 == 0         # False

2.2.5. Increment Operators

  • += - Incremental addition

  • -= - Incremental subtraction

  • *= - Incremental multiplication

  • /= - Incremental division

value = 10
value += 1

print(value)
# 11
value = 10
value -= 1

print(value)
# 9
value = 10
value *= 2

print(value)
# 20
value = 10
value /= 2

print(value)
# 5