Numeric data types store numerical values in a programming language, they can be divided into Integer data types, which  store whole numbers;  floating data types, which store floating-point values i.e values with decimal points, and lastly we have the complex data types , which store complex numbers.

Python offers builtin support for the  three numeric data types , the int type is used to represent the integer values, the float type represents the floating point values and the complex type represents complex numbers. Boolean values, represented by the bool type, are a subtype of the integer data type.

The three numeric types are immutable, meaning that once they are created, their value cannot be changed. You can check on Mutable vs Immutable Data Types  to see more details.

In this article we will explore the three numeric data types , their features and applications in Python programming.

Integers

Integers represent whole numbers and are the most commonly used numeric data type in Python programming.

Examples of integers:

1, 2, 3, 5, 9, 78, 10000, 178900 

Some languages like C++ and Java  have limits on the size of integers due to how numbers are represented internally in the memory. And in such languages, there are different types of integers as characterized by their size, for example an integer variable  will only hold integers in the range  -2147483648 to 2147483647 , trying to assign a value outside that range will raise an error unless you use other integer types such as the long int type.

In Python ,there is only one integer type represented by type int which is abstracted  in a way that it can hold integers of unlimited size. These means that you can efficiently use integers like 2^10000, which has over 3000 digits, a thing which is impossible in some languages like C++ or Java.

The int type  defines some attributes and methods  for integer instances, which allows easy manipulation and conversions, these  features allow us to perform operations such as arithmetic, logical  comparison , boolean logic e.tc on integers.

Arithmetic Operations on Integers

The most commonly performed operations on integers and other numeric types are arithmetic operations,  these are just the basic operations from mathematics such as addition, subtraction, multiplication and division. Briefly, the operators used for arithmetic operations are:

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
** power
// Floor Division
% Modulo 

You can check on arithmetic operators to see a detailed discussion on these operators and operations.

The only operations which may be a little unfamiliar is the Floor Division and the Modulo operation, all the rest are just as used  in Mathematics.

The Floor division is just like normal division but returns the floor value of the division result ,  the floor value of a number is the largest  integer  which is less than or  equal to the number. For example the floor value of 5.5 is 5 .

The modulo operation returns the remainder of a division, for example 10 % 3 is because 10 divided by 3 is 3   remainder 1. While this operation  is not used much in elementary mathematics, it has alot of use when it comes to programming, for example if you divide any number by two, the remainder is always 1 or 0, if the remainder is  1, the number is odd, if 0, the number is even:

a = 7
match a % 2:
    case 1:
        print("Odd")
    case 0:
        print("Even")

//Odd

Example of arithmetic operations on integers interactively:

67 - 7
//60
67 + 7
//74
67 * 7
//469
67 / 7
//9.571428571428571
67 // 7 
//9
67 % 7
//4
30 // 10
//3
30 % 10
//0
3 ** 5
//143
3 ** -2
//0.1111111111111111

What you note from the above examples is that most arithmetic operations where both operands are integers evaluates to an integer. The only case where this does not apply is in exact division i.e division using the ( / ) operator,  which always evaluates to a float; and  in the case of exponentiation/power where the second operand, the exponent, is negative.

Each instance of the int class has a method which is responsible for performing each arithmetic operations, in fact, when you use the arithmetic operators, you are simply calling the method  which performs the actual operation. These methods are called magic methods or dunder methods(dunder is acronym for double underscore).

for example you can also add integers using the addition magic method which is __add__() as shown below:

a = int(9).__add__(7)
print(a)

16

Relational and comparison Operations on Integers

Performing logical comparison on integers and other numeric values is almost like how we do it in mathematics, the operators used are almost  the same except for the equality operator, In math the  ( = ) operator is used for equality, but in Python and most other languages this operator is reserved for assignment, the  (==) operator  is used for equality. This is a very common pitfall for beginners and even some experienced programmers, where you use a single equal sign (=) for equality.

The other operation which is not very common in mathematics but it is very common in programming is not equal to  , in Python the operator used for this operation is (!=) .

The comparison operators always evaluates to boolean values i.e either True or False. Briefly, the operators are:

operator semantics
< less than
> greater than 
== equal to
<= less than or equal to
>= greater than or equal to
!= not equal to

Check on relational operators to see more about these operators.

1 < 2
//True
1 > 2
//False
1 == 2
//False
1 <= 2
//True
1 >= 2
//False
1 != 2
//True
Turning other types to int type

We can use the built-in int() function to turn to integers values from other types which have an equivalent integer value. Some types which can be casted to integers are:

  1. float: Floats can be casted to integers, with the decimal point truncated.
  2. bool: True is casted to 1, and False is casted to 0.
  3. str: Strings that represent valid integer literals.
  4. bytes and bytearray: These can be casted to integers if their length is exactly 1. The integer value is the integer representation of the byte.
  5.  Other objects: Other objects can be casted to integers if they define the __int__() method, which returns an integer representation of the object. For example, datetime objects can be casted to integers using their timestamp values.
int("6")
//6
int("-100")
//-100
int(34.7)
//34
int(True)
//1
int(False)
//0

If called without any argument, the int() function returns 0:

int()
//0

 

The bool type

The bool type as we said earlier is a subtype of int , it has  only two values which is True and False. In fact, these two values are simply  1 and 0 respectively. Python offers a builtin function issubclass() which we can use to confirm that the bool type is a subtype of int:

issubclass(bool, int)
//True

Performing arithmetic operations on boolean values also evaluates to integers as follows:

True + True
//2
False + False
//0
True + False
//1

Boolean values are the return values of  the following  operators:

  1. Relational and Comparison  operators e.g 1 < 2 returns True
  2. Logical Operators e.g (2 < 1) and (6 > 4) returns False
  3. Membership operators e.g 2 in [ 1, 2, 3, 4 ]  returns True
  4. Identity Operators e.g a is b
  5. Conditional expressions 

Every object which has value can be casted into a boolean type, in a nutshell, the boolean values of the builtin types  are as follows:

non-zero number e.g 1,  -7,  20.0 True
Zero e.g 0,  0.0, 0j False
non-empty String  True
empty string False

nonempty sequence e.g (0, 1, 2),  [1, 2, 3], {"a": "apple "}

True
empty sequence e.g (), [], {} False
any other instance of Python type except Nonetype e.g class, function, types

True 

None False

We can use the builtin  bool() function to cast values into their boolean values , Examples:

bool(1)
//True
bool(0)
//False
bool(None)
//False
bool(7.3)
//True
bool([1, 3, 5])
//True
bool([])
//False
bool("r")
//True
bool("")
//False

when the bool() function is called without an argument it returns False:

bool()
//False

Floating Point Numbers

Floating point numbers are numerical representations that approximate real numbers with a decimal point. Examples of floating point numbers include 1.0, 0.0, 10.4, 7.6, 10.01, and 1000.0

We can also use the scientific notation for floating point values . In this notation a float value is represented in the form of e+x or e-x, where e is the letter “e”, and x is the power of 10, which is a positive or negative integer. For example, the number one million can be represented in scientific notation as 1e+6, and the number one tenth can be represented as 1e-1. This notation is especially useful when we are dealing with very large or small numbers.

1e+6
//1000000.0
1e-4
//0.0001
type(1e+6)
//<class 'float'>

Real numbers are not always accurately represented in memory compared to integers. For example, in some older versions of Python, performing the operation 1.0 / 10 will yield the answer 0.10000000000000001, when the correct answer is just 0.1. Even though , the previous equation will yield the correct answer  in latter versions such as  Python 3.11.2, the expression 1.0 / 10 == 0.10000000000000001 will evaluate to True, even though the two values are not equal.

Example in python 3.11.2

1.0 / 10
//0.1
1.0 / 10 == 0.10000000000000001
//True
0.1 + 0.2
//0.30000000000000004

These inaccuracies have nothing to do with Python, it is purely due to how floating point values are represented, for example  it is  impossible to represent 0.1 accurately in the memory so  it gets approximated to the nearest representable value.

Operations on Floats

Since operations on floats and integers do not have much difference, we will not discuss the operators again. The  thing that you should keep in mind is that an operation which involves an operand of  int type and another  of float type, will always yield a floating point value

Examples:

2.4 + 6.3
//8.7
2.5 * 2.5
//6.25
7.0// 7
//1.0
9 // 3.0
//3.0
10 ** 2.0
//100.0

We can use the builtin float() function to  cast compatible values from other types to float type as follows:

float(1)
//1.0
float("4.7")
//4.7
float("5")
//5.0
float(True)
//1.0
float(False)
//0.0

When the float() function is called without any argument, it returns 0.0:

float()
//0.0

Complex numbers

Compex numbers are numbers of the form a + bj, where a and b are real numbers and j is the imaginary unit, defined as the square root of -1. In Python, complex numbers are represented using the built-in complex data type.

Examples:

type( 3 + 4j )
//<class 'complex'>

You can also use the builtin complex() function with two arguments, to create  a complex number.

z = complex(3, 4)
print(z)
//(3+4j)

If no argument is given, the  complex() function returns 0j

complex()
//0j
Accessing the real and the imaginary parts of a complex number.
z = 3 + 4j
print(z.real)
//3.0
print(z.imag)
//4.0