AlgoMaster Logo

Numbers (int, float, complex)

Last Updated: January 3, 2026

6 min read

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.

Integer Basics

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.

Creating Integers

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.

Integer Operations

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.

Real-World Applications

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.

Floating-Point Numbers

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.

Creating Floats

You can create a float by including a decimal point:

Float Operations

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.

Practical Float Example

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.

Complex Numbers

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.

Creating Complex Numbers

You can create a complex number using the complex() function or by direct assignment:

Operations with Complex Numbers

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.

Real-World Applications

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.

Type Promotion and Casting

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.

Casting Between Number Types

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.

Edge Cases and Nuances

Finally, let’s touch on some edge cases that can trip up even seasoned developers.

Integer Overflow

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.

Precision of Floats

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.

Complex Number Limitations

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.