Python Number Data Types Tutorial

In this tutorial we learn integer, floating point, and complex numbers in Python, and how to convert between (cast) them.

We also cover common mathematical and trigonometric functions.

Number Data Types

Python supports three different numerical types.

  • int is a positive or negative whole number with unlimited size.
  • float , or floating point number, is a number with decimal points.
  • complex is a number with imaginary parts.

The int type

An int is a whole number. It can be positive or negative but it shouldn’t have a decimal point.

Example: int
 print(10)

The float type

A float is a floating point number, that’s to say it’s a number with a decimal point.

Example: float with decimal symbol
 print(3.14)

A float can also be a scientific number with an float to indicate the power of 10.

Example: float with scientific notation
 print(-78e3)

The complex type

A complex number is a float number and a square root of -1. The square root is represented as an imaginary number by the lowercase or uppercase letter J .

Example: complex
 print(619j)

How to see which type() a number is

If we want to see which type a number is, we pass the number to the built-in type() function.

Example: type() function
print( type(20) )
print( type(3.14) )
print( type(-78e3) )
print( type(619j) )

In the example above we check the type of an int , two float numbers and a complex number.

Output: type() function
<class 'int'>
<class 'float'>
<class 'float'>
<class 'complex'>

The output shows us that the numbers belong to one of the numeric data types.

How to explicitly convert a number type to another number type

In most cases Python will convert numbers internally, but sometimes we may want to convert one type of number to another explicitly. To do this we use specific conversion functions.

How to convert a number to int

To convert a number to an int we use the int() function.

Example: convert a number to an int
 print( int(3.14) )

note When the conversion happened, it simply cut away everything after the decimal point.

How to convert a number to float

To convert a number to a float we use the float() function.

Example: convert a number to a float
 print( float(10) )

How to convert a number to complex

To convert a number to a complex number we use the complex() function with one or two parameters.

Example: convert a number to a complex, imaginary part specified
 print( complex(10) )

In the example above we used only one number. It will use the 10 as the float part of the complex number and the imaginary part will be 0.

Example: int
 print( complex(10, 5) )

In the example above we used two numbers separated by a comma. It will convert the 10 as the float part of the complex number and use the 5 as the imaginary part of the complex number.

List of common mathematical functions

The following table lists some of the commonly used math functions:

FunctionDescriptionExampleReturns
abs(x)Positive (absolute) value integerabs(-10)10
ceil(x)Round to closest integer topceil(3.14)4
exp(x)Exponential of xexp(1.5)4.481
fabs(x)Positive (absolute) value floatfabs(-10)10.0
floor(x)Round to closest integer bottomfloor(3.14)3
pow(x, y)Power of. x**ypow(2, 2)4
round(x)Round to closest top or bottomround(3.14)3
sqrt(x)Square root of xsqrt(5)2.236
max(x, y, …)Largest number of the setmax(1, 3, 5)5
min(x, y, …)Smallest number of the setmin(1, 3, 5)1

List of common trigonometric functions

The following table lists some of the commonly used trigonometric functions:

FunctionDescription
acos(x)Return the arc cosine of x, in radians
asin(x)Return the arc sine of x, in radians
atan(x)Return the arc tangent of x, in radians
cos(x)Return the cosine of x radians
sin(x)Return the sine of x radians
tan(x)Return the tangent of x radians
degrees(x)Converts angle x from radians to degrees
radians(x)Converts angle x from degrees to radians

Summary: Points to remember

  • An int is a positive or negative whole number.
  • A float is a number with a fractional part in which the fractional component is denoted by a decimal symbol or scientific notation.
  • A complex is a number with real and imaginary parts.
  • We can check the type of number by passing it to the built-in type() function.
  • We can convert one number type to another by using the int() , float() or complex() functions.