Python Numbers

Last Update : 21 Aug, 2022 Python, Programming

In this tutorial, You will learn about the different types of numbers used in Python.

There are three numeric types supported in Python such as integers, floating-point numbers, and complex numbers. Python numeric types are defined as int, float, and complex classes.

You can create various numeric types when assigning a value to variables (defining variables).

For example -:

x = 5    # int
y = 2.3  # float
z = 5j   # complex

You can use the type() function to verify the type of any object in Python. Also, can use the isinstance() function to check whether the variable belongs to a particular class.

For example -:

x = 5
y = 2.3
z = 5j

# check the variable type.
print(type(x))
print(type(y))
print(type(z))


# ckeck the variable belongs to a particular class.
# with the correct class
print(isinstance(x, int))
print(isinstance(y, float))
print(isinstance(z, complex))

# with the incorrect class
print(isinstance(x, complex))

This program produces the following result -:

<class 'int'>
<class 'float'>
<class 'complex'>
True
True
True
False

 

Integer Numbers in Python (int)

In Python, Integer (int) is a positive or negative whole number without decimals of unlimited length.

The following example shows the valid integer literals in Python.

w = 0
x = 1
y = 356562355487711
z = -3255872

print(type(w)) # <class 'int'>
print(type(x)) # <class 'int'>
print(type(y)) # <class 'int'>
print(type(z)) # <class 'int'>

binary, octal, and hexadecimal values are also valid integer literals in Python.

x = 0b11011000 # binary
y = 0o12 # octal
z = 0x12 # hexadecimal

print(type(x)) # <class 'int'>
print(type(y)) # <class 'int'>
print(type(z)) # <class 'int'>

 

Floating Point Numbers in Python (float)

In Python, Floating point number (float) is the positive and negative real number containing one or more decimals.

The decimal part is denoted by the decimal symbol " . " or the scientific notation like "E" or "e".

For example -:

x = 1.2
y = 2e400
z = -32.534

print(type(x)) # <class 'float'>
print(type(y)) # <class 'float'>
print(type(z)) # <class 'float'>

There is a maximum size for floating point numbers which depends on your system. The floating point numbers beyond their maximum size are referred to as "inf", "Inf", "INFINITY", or "infinity".

2e400 will be considered as the infinity of floating point numbers for most systems.

 

Complex Numbers in Python

Complex numbers are numbers with real and imaginary parts. The imaginary part is written with the j.

For example -:

x = 3+5j
y = 5j
z = -5j

print(type(x)) # <class 'complex'>
print(type(y)) # <class 'complex'>
print(type(z)) # <class 'complex'>

 

Python Numbers Type Conversion

Type conversion is converting the value of one data type to another data type. You can use the int(), float(), and complex() methods to convert from one type to another.

For example -:

#convert int to float:
x = float(1)

#convert float to int:
y = int(2.83)

#convert int to complex:
z = complex(1)

print(x)
print(y)
print(z)

print(type(x))
print(type(y))
print(type(z))

This program produces the following result -:

1.0
2
(1+0j)
<class 'float'>
<class 'int'>
<class 'complex'>

 

Note: You do not allow to convert complex numbers into another number type in Python.

 

Summary

  • Three numeric types are supported in Python such as integers, floating-point numbers, and complex numbers.
  • Use the type() function to verify the type of any object in Python.
  • int is a positive or negative whole number without decimals.
  • float is the positive and negative real number containing decimals.
  • complex is a number with real and imaginary parts.

 

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.