Blog Post #9: The Building Blocks: Working with Numbers (Integers and Floats)

In Post #8, we learned that variables are like labeled boxes for storing data. We stored a number (100) and some text ("Alex"), hinting that Python treats different kinds of data differently. These different kinds are called data types. A data type simply tells Python what sort of data a variable is holding and what kinds of operations can be performed on it.

In this post, we’ll explore the two most common numerical data types in Python: integers and floating-point numbers. Understanding them is the first step to performing calculations and working with numerical data in your programs.

Integers: The World of Whole Numbers

An integer is a whole number. It can be positive, negative, or zero, but it cannot have a decimal part. If you are counting things that cannot be split into fractions, you are dealing with an integer.

In Python, this data type is called int (short for integer).

Here are a few examples of integers stored in variables:

# Examples of integers
player_count = 4
current_score = 1250
level_modifier = -5
initial_items = 0

Integers are perfect for things you can count: the number of users online, a player’s score in a game, the number of items in a shopping cart, or a simple counter in a loop.

Floats: Handling the Decimals

A floating-point number, or a float for short, is a number that contains a decimal point. Floats are used to represent numbers that can have a fractional part, like measurements or the result of a division. They are Python’s version of what you might call real numbers in mathematics.

The Python name for this data type is float.

Here are some examples of floats:

# Examples of floats
pi_approx = 3.14
account_balance = 42.95
player_speed = 2.5

You would use floats for any value that requires precision: the price of an item, a scientific measurement, or a user’s GPA.

The Key Difference: The Decimal Point

So, what really separates an int from a float in Python’s eyes? It’s simple: the presence of a decimal point.

Even if a number has a mathematical value that could be a whole number, if it is written with a decimal point, Python will treat it as a float.

Consider this example:

whole_number = 10
decimal_number = 10.0

Here, whole_number is an int, but decimal_number is a float. To Python, they are fundamentally different types of data.

We can prove this with a handy built-in function called type(), which tells us the data type of any variable. Don’t worry about understanding functions in detail yet; just see this as a useful tool for inspecting our data.

number_a = 50
number_b = 50.0

print(type(number_a))
print(type(number_b))

Running this script will produce the following output:

<class 'int'>
<class 'float'>

This confirms that Python sees 50 as an int and 50.0 as a float.

What’s Next?

You’ve now met Python’s two main numerical types: int for whole numbers and float for numbers with decimal points. Knowing the difference is crucial for performing accurate calculations, which we will start doing in upcoming posts.

Numbers are only one part of the story when it comes to basic data. The other major part is text. In Post #10, we will take a deep dive into the string data type, learning how to create, manage, and work with textual data in Python.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment