Last Updated: January 3, 2026
Whether you're calculating values, processing data, or working with scientific computations, numbers play a crucial role.
In this chapter, we'll dive deep into Python's number types: integers, floats, and complex numbers.
Let's start with integers, or int types. An integer is simply a whole number, which can be positive, negative, or zero. Integers in Python can be of arbitrary size, meaning you don't have to worry about overflow like in some other languages.
You can create an integer by simply assigning a whole number to a variable:
Here, x is a positive integer, y is a negative integer, and z is zero.
Integers support basic arithmetic operations like addition, subtraction, multiplication, and division. Let's look at some examples:
The division operator (/) gives you a float, while floor division (//) returns an integer. This distinction is essential when you're working in situations where integer values are required.
Integers can be used in various scenarios, such as counting items, indexing lists, or managing loop iterations. For instance, if you are processing a list of customer orders, you might use integers to represent the number of orders:
Here, we're using the sum() function, which is a common operation when dealing with integers.
Next, let’s explore floating-point numbers, or float. A float represents real numbers and can contain a decimal point. This makes them ideal for representing quantities that require precision, such as measurements or financial calculations.
You can create a float by including a decimal point:
Just like integers, floats support arithmetic operations. However, be mindful of floating-point precision issues, which can lead to unexpected results:
This behavior stems from how floating-point numbers are represented in binary, leading to minor inaccuracies.
Floats are widely used in financial applications where precision is critical. For example, calculating interest rates can often involve floats:
In this example, we calculated the total amount after applying simple interest to the principal.
Now, let’s dive into complex numbers, represented by complex in Python. A complex number has a real part and an imaginary part, typically written in the form a + bj, where a is the real part and b is the imaginary part.
You can create a complex number using the complex() function or by direct assignment:
Complex numbers also support arithmetic operations, but with some unique behaviors:
When multiplying complex numbers, remember that \(j^2 = -1\), which leads to the unique results you'd expect from complex multiplication.
Complex numbers are frequently used in fields like engineering, physics, and signal processing. For instance, they are integral in electrical engineering for analyzing AC circuits:
Here, the cmath module provides functions to work with complex numbers, allowing you to compute magnitude and phase angles.
When performing operations, Python automatically promotes integers to floats if a float is involved. This is known as type promotion. Let's look at an example:
You might expect an integer result, but Python returns a float due to promotion. This behavior helps prevent data loss during calculations.
Sometimes, you might need to explicitly convert between different number types. You can use functions like int(), float(), and complex() for this purpose:
This explicit casting is useful when you need to ensure a specific type in your calculations.
Finally, let’s touch on some edge cases that can trip up even seasoned developers.
While Python handles large integers well, keep in mind that operations involving very large numbers can still lead to performance issues. Always consider the implications of working with large datasets or calculations.
As mentioned before, floating-point arithmetic can sometimes lead to unexpected results due to precision limitations. Always test your calculations, especially in financial applications where errors can be costly.
While complex numbers are powerful, they are sometimes misunderstood. Remember that they are not commonly used for everyday programming tasks. Use them judiciously, and always clarify their purpose when sharing code with others.
In the next chapter, we will look at how to effectively convert between different data types, enhancing your ability to manipulate and process data seamlessly.