Working with Numbers in Python

In this post I explain the main number types in Python and the basic arithmetic operators.
TopicsPython

Working with Numbers in Python

Python has several basic types for working with numbers:

  • int for integers, for example 5 and -10
  • float for floating-point numbers, for example 3.14 and -0.001
  • complex for complex numbers, for example 2 + 3j

Examples of Number Literals

10        # int
3.5       # float
2 + 3j    # complex

Basic Arithmetic Operators

  • + for addition
  • - for subtraction
  • * for multiplication
  • / for division, which always returns a float
  • // for floor division
  • % for remainder
  • ** for exponentiation
7 + 2    # =   9 addition
7 - 2    # =   5 subtraction
7 * 2    # =  14 multiplication
7 / 2    # = 3.5 division (float)
7 // 2   # =   3 floor division
7 % 2    # =   1 remainder
7 ** 2   # =  49 exponentiation