Python numbers

  1. Python knows about several types of number
    • integer
    • float
    • complex
    • binary, octal, hex
  2. Here is how to to write numbers of these types:

    123           # integer
    -456          # negative
    +789          # positive
    1_000_000     # underscores may be used for grouping
    0O77          # octal
    0O666         # octal
    0b101010101   # binary
    0xbadcafe     # hexadecimal
    3.1415926     # float
    10.           # float
    .001          # float
    1e100         # float
    3.14e-10      # float
    0e0           # float
    0123e012      # float, but careful! Not octal
    3j            # imaginary
    1+2j          # complex
    
  3. Python's has a range of arithmetic operators with the familiar infix syntax.

    Name Operator
    Negation - a
    Positive + a
    Addition a + b
    Subtraction a - b
    Multiplication a * b
    Division a / b
    Division with floor a // b
    Modulo a % b
    Exponentiation a ** b
    Equality a == b
    Difference a != b
    Ordering a < b
    Ordering a <= b
    Ordering a >= b
    Ordering a > b
  4. Division in Python returns a float (/). The integer division operator (//) returns an int.

    8/2
    9/2
    2/8
    2/9
    6.7/3
    6.7//3
    
  5. Even more math functions are available in the math library.

Author: Breanndán Ó Nualláin <o@uva.nl>

Date: 2025-02-10 Mon 14:45