Close

Python Numbers

[Last Updated: Feb 2, 2022]

Python has three numeric types:

  • int
  • float
  • complex numbers

Example

x = 1  # int
y = 1.2  # float
y2 = 2e3  # int  exponential notation, the e or E can be used
y3 = 1.3E-4  # float
z = 2 + 3j  # complex
z2 = 2.2 + 3e-4j  # complex

Using type()

type() function returns the data type of a variable:

scripts/numeric-types.py

def showtype(num):
    print(f"type of {num} is {type(num)}")


x = 1  # int
y = 1.2  # float
y2 = 2e3  # int
y3 = 1.3E-4  # float
z = 2 + 3j  # complex
z2 = 2.2 + 3e-4j  # complex

showtype(x)
showtype(y)
showtype(y2)
showtype(y3)
showtype(z)
showtype(z2)

Output

type of 1 is <class 'int'>
type of 1.2 is <class 'float'>
type of 2000.0 is <class 'float'>
type of 0.00013 is <class 'float'>
type of (2+3j) is <class 'complex'>
type of (2.2+0.0003j) is <class 'complex'>

Example Project

Dependencies and Technologies Used:

  • Python 3.10.2
Numeric types in Python Select All Download
  • python-numeric-types
    • scripts
      • numeric-types.py

    See Also