In this article we will explore Python numeric data types.

Table of Contents



Introduction

In Python, there are three distinct numeric types: integersfloating point numbers, and complex numbers. And numbers are created by numeric literals or as the result of built-in functions and operators.

Learning data types in each programming language is essential to understand the code and programs.

Numeric data types are widely used in many mathematical, statistical, data science, and machine learning solutions built in Python.


Python int (integer) numeric type

Python int (integer) numeric type represents an integer which is a whole number, it can be positive or negative, and it can be of unlimited length.

The int() constructor can be used to create an integer number in Python.

Consider the following examples:


a = 1
b = -2
c = int(5)
d = int('6')

print(type(a))
print(type(b))
print(type(c))
print(type(d))

print(a, b, c, d)

Output:


class 'int'
class 'int'
class 'int'
class 'int'

1 -2 5 6

Python float (floating point number) numeric type

Python float (floating point number) numeric type represents a number that contains one or more decimals, it can be positive or negative (including infinity).

The float() constructor can be used to create a floating point number in Python.

Consider the following examples:


a = 1.0
b = -1.10
c = float('5.5')
d = float('-inf')

print(type(a))
print(type(b))
print(type(c))
print(type(d))

print(a, b, c, d)

Output:


class 'float'
class 'float'
class 'float'
class 'float'

1.0 -1.10 5.5 -inf

Python complex (complex number) numeric type

Python complex (complex number) numeric type represents a complex number that contains one real and one imaginary parts, and is constructed from two real numbers.

The complex() constructor can be used to create a complex number in Python.

Consider the following examples:


a = 1+3j
b = -2+5j
c = complex(3,-7)
d = complex('1')

print(type(a))
print(type(b))
print(type(c))
print(type(d))

print(a, b, c, d)

Output:


class 'complex'
class 'complex'
class 'complex'
class 'complex'

(1+3j) (-2+5j) (3-7j) (1+0j)

Numeric operations in Python

All numeric types support the following operations:

OperationResultintfloatcomplex
x + ySum of x and y
x - yDifference of x and y
x * yProduct of x and y
x / yQuotient of x and y
x // yFloored quotient of x and y
x % yRemainder of x / y
-xx negated
+xx unchanged
abs(x)Absolute value of magnitude of x
int(x)x converted to integer
float(x)x converted to floating point
complex(re, im)A complex number with real part re and imaginary part im
c.conjugate()Conjugate of the complex number c
divmod(x, y)The pair (x // y, x % y)
pow(x, y)x to the power y
x ** yx to the power y
Source

Examples

Knowing numeric data types and their properties is essential for operations.


Addition operation

Performing addition of integers will result in integer type output:


a = 1
b = 2

c = a+b

print(c)

and you should get:

3

Performing addition of integer and float will result in float type output:


a = 1
b = 2.0

c = a+b

print(c)

and you should get:

3.0

Subtraction operation

Similar to addition operation, performing subtraction of integer from integer will result in integer type output:


a = 5
b = 3

c = a-b

print(c)

and you should get:

2

and performing subtraction of float from integer will result in float type output:


a = 5
b = 3.0

c = a-b

print(c)

and you should get:

2.0

You can also subtract a negative number which will result in an addition operation:


a = 3
b = -6

#Operation: 3 - (-6)
c = a-b

print(c)

and you should get:

9

Multiplication operation

Performing multiplication of integers will result in integer type output:


a = 5
b = 2

c = a*b

print(c)

and you should get:

10

Performing multiplication of integer by float will result in float type output:


a = 5
b = 2.0

c = a*b

print(c)

and you should get:

10.0

Division operation

Performing division of integers will result in float type output:


a = 9
b = 3

c = a/b

print(c)

and you should get:

3.0

Performing division of integer by float will result in float type output:


a = 9
b = 3.0

c = a/b

print(c)

and you should get:

3.0

Conclusion

In this article we explored Python numeric data types including integers, floating point numbers, and complex numbers.

As your next step in learning Python consider reading about Python data structures in the following articles: