Python is the programming language of choice for many data scientists because of its rich expanse of libraries and easy-to-understand syntax. Regardless, it still requires time and investment to learn. Everyone who has taken a language course knows that becoming fluent requires a solid foundation. And just like you can't form English sentences without knowing individual words, learning data types is crucial to chaining codes together. That's why, without a doubt, if you are new to Python programming, it's important to understand the different types, such as number (int and float), string, and boolean.
This tutorial will help you get started by introducing you to the int and a float in Python and demonstrating how to perform data type conversion using NumPy.
Table of Contents
- What Are Float and Int in Python?
- How to Convert Float to Int in Python?
- How to Convert Float to Int in Python Using NumPy?
- Float to Int in Python: Next Steps
What Are Float and Int in Python?
Data can be represented by numeric values such as:
- Monetary value
- Age
- Counts
In Python, int and float are two widely used data types for numeric values. Int represents integers – either a positive or negative whole number like 3 or 5. Meanwhile, floats (or floating points) are real numbers, such as 4.37 or 55.87. The difference between the 2 is simply the presence of a decimal point.
You can find out what your variable is by applying type()in Python:
>>> a = 1 # int type
>>> type(a) # get type from variable
<class 'int'>
>>> type(1) # get type from value, int type
<class 'int'>
>>> type(1.0) # get type from value, float type
<class 'float'>
How to Convert Float to Int in Python
In some cases, we do not need the results of mathematical calculations with a decimal place precision. That means we do not need our variable to be a float. So, what do we do?
We can perform something called “casting a floating-point number to an integer” to convert our data types. Essentially, it’s transforming a float to an int in Python.
To carry out this operation, you need to apply int() around a value or variable:
>>> a = 1.0 # float type
>>> b = int(a) # cast to int type
>>> type(a)
<class 'float'>
>>> type(b)
<class 'int'>
However, you should use this technique with caution, as all floating-point numbers are rounded down to the nearest integer by default:
>>> int(1.1)
1
>>> int(1.9)
1
Rounding Up and Down using NumPy
Instead of using casting to convert float to int, we can apply round() to a value or variable and it automatically rounds up or down the number:
>>> round(1.1)
1
>>> round(1.9)
2
>>> round(1.5) # values in center are rounded up
2
This is useful for mathematical operations as you would not want to obtain the incorrect answer just because of rounding errors.
How to Convert Float to Int in Python Using NumPy
You can control which way Python rounds your floating points. Strictly speaking, this method is for mathematical equations that take the floor (rounded down) or ceiling (rounded up) version of numbers. For example, if a problem asks for the maximum number of items you can buy with a given amount of money, the answer must be rounded down to the nearest integer. Why? Because you cannot purchase a fraction of an item, nor can you round up and buy more than you can afford.
To apply a default command, you need the NumPy library. First, you need to import the package to Python, then set your desired outcome:
>>> import numpy as np
>>> int(np.floor(1.8)) # rounds down
1
>>> int(np.ceil(1.2)) # rounds up
2
Important note: NumPy does not perform casting automatically, so you still have to apply int() to the result.
Type conversions are a useful skill in Python when you want to control the representation of numerical data. It is easy to perform type conversion, but tricky as it can lead to rounding errors if used incorrectly.
Float to Int in Python: Next Steps
Fluency in a programming language like Python is essential for anyone looking to land a job as a data scientist in the field. Often, employers will set you tasks during the interview, so make sure you know your way around the different data types. It’s generally good practice to brush up on your coding and data skills now to ensure a successful data science career tomorrow. Thankfully, you've come to the right place
The 365 Data Science Program offers self-paced courses led by renowned industry experts. Starting from the very basics all the way to advanced specialization, you will learn by doing with a myriad of practical exercises and real-world business cases. If you want to see how the training works, start with a selection of free lessons by signing up below.