What are the numeric data types in Python?

Introduction

Numeric data types handle numerical values used in mathematical computations and for other purposes which use numeric values.

Python is a dynamically typed language and therefore data type is set when you assign a value to a variable. For instance, if you are assigning an integer value, the data type will be automatically set to an integer type. Similarly, if you are assigning a decimal value, the data type will be automatically set to float. Hence, programmers need not have to explicitly specify its type whether it is an integer, float, or a string like in other programming languages.

There are three distinct numeric data types in Python:

  1. integers
  2. floating-point numbers
  3. complex numbers.

(The standard library includes the additional numeric types of fractions. Fraction, for rational, and decimal. Decimal, for floating-point numbers with user-definable precision.)

Previous post: simple data types

Integer

Int, or integer, is a whole number, positive or negative, without decimals of unlimited length.

Example:

x = 1
y = 35656
z = -56

Floating-point number

The float or floating-point number is a number, positive or negative, containing one or more decimals.

Example:

x = 2.5
y = 1.05
z = -67.78

Float can also be scientific numbers with an “e” to indicate the power of 10.

Example:

x = 56e4
y = 2E4
z = -45.56e45

Complex number

Complex numbers have real and imaginary parts, which are each floating-point number. The imaginary part is written with the letter ‘j’.

Example:

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

To extract real and imaginary parts from a complex number, use variable_name.real and variable_name.imag.

>>> print(F"Real number of x is {x.real}")
Real number of x is 3.0
>>> print(F"Imaginary number of x is {x.imag}")
Imaginary number of x is 5.0

Checking the data type

In python, data types are set dynamically based on the assigned value. So, to check the data type of any object or variable, we can see using the built-in type() function. It will return the type of data.

For example, let’s declare a variable x and assign integer value 5;

>>>x = 5
>>>print(f"Data type of x {type(x)}")
Data type of x <class 'int'>

In the above example, it returned <class ‘int’>. This means that the data type of x is an integer. Similarly, you can check for other types of data;

PI = 3.14
x = 5 + 6j
print(f"The Data type of PI {type(PI)}")
print(f"The Data type of x {type(x)}")

Output:

The data type of PI <class 'float'>
The Data type of x <class 'complex'>

Finding the memory address of the data

Whenever we are assigning some values to the variables, they will be automatically stored inside the memory with some address. The built-in id() function returns the memory address where the value referenced by the variable is stored. Or simply said, it returns the address of the variable in the memory.

For example, let me declare a variable num and assign a float value of 20.0;

num = 20.0
print(f"Memory address of num {id(num)}")

Output:

Memory address of num 1318753283600

It returned some number which is actually a memory address of value 20.0 stored inside the variable num.

In python, memory allocation is done by the Python memory manager and the user has no control over it. Let me explain with the example;

num = 10
add = 5 + 5
print(f"Memory address of num {id(num)}")
print(f"Memory address of add {id(add)}")

Output:

Memory address of num 140715664791488
Memory address of add 140715664791488

When we run the above program, both the values are stored inside the same memory address (140715664791488); this is because both variables hold the same value but in a different way. This is how python allocates the memory address dynamically by the interpreter.  

Underscores in number

When you’re writing long numbers, you can group digits using underscores to make large numbers more readable:

Example:

annual_income = 5_00_000
print(annual_income)

When you print a number that was defined using underscores, Python prints only the digits:

500000

Python ignores underscores when storing these kinds of values. Even if you don’t group the digits in threes, the value will still be unaffected.

To Python, 5000 is the same as 5_000, which is the same as 50_00. This feature works for integers and floats, but it’s only available in Python 3.6 and later.

Numeric data types are immutable

Numeric data types are immutable which means that their value cannot be changed once declared. For instance, I am declaring a variable num and assign an integer value of 10; and again in the same variable num, I am assigning a value of 20;

Example:

num = 10
print(id(num))

num = 20
print(id(num))

So, when I am printing the memory address of the variable num, it is stored in a different memory address. This is because numeric data types are immutable and will be overwritten by the current value.

140715384231872
140715384232192

Summary

There are three numeric built-in data types in python; int, float, and complex number.

Python is a dynamically typed language. Data types are not typed explicitly.

The built-in id() method is used to check the memory address of the value referenced by the variable.

The built-in type() method is used to check the data type of any object or variable.

Numeric data types are immutable.

When you’re writing long numbers, you can group digits using underscores to make large numbers more readable.