
When working with numerical data in Python, it is necessary to distinguish between integer and floating-point values and write code accordingly. Since Python version 3, it has become less common for issues to arise even if the distinction between integers and floating-point numbers is not clearly made. However, this is a fundamental concept in numerical data handling and is essential to understand when learning other programming languages; therefore, it is crucial to master it thoroughly.
The numeric types that can be handled in Python are integers, floating-point numbers, and complex numbers.
This site does not cover complex numbers (as they are difficult for beginners and rarely used by the general public). If you want to write code that uses complex numbers, please search for keywords such as “Python complex” You will find a wealth of information on how to use them.
Table of Contents(目次)
Integer (int)
An integer is a numerical data type such as “0,” “15,” and “-20.” Roughly speaking, it is a “number that is not a decimal or a fraction.” Python treats zero and negative values as integers.
In Python, integers are represented as “int”.
Floating-Point Number (float)
A floating-point number is a data type that represents numbers such as “0.0,” “15.6,” and “-20.012.” It refers to “numbers that include a decimal point.” Therefore, even if a number can be represented as an integer, such as “0.0” or “15.0,” Python treats it as a floating-point number when a decimal point is added to define the value.
In Python, floating-point numbers are represented as “float.”
As shown in the following code, Python automatically identifies and manages int/float when you define each value.
num_int = 10 # Data is recorded as integers (int).
num_float = 10.0 # Since there is “.0”, the data is recorded as a floating point number (float).
When int and float values are mixed in calculations
In Python 3, calculations can be performed correctly even if int and float values are mixed, so you can generally obtain correct results without worrying too much about data types. However, in Python 2 (major version 2) or other programming languages, calculations may produce incorrect results; therefore, it is essential to be mindful of this when writing code.
Calculations with int and float (Example 1)
For example, when you perform “2 + 2.5,” Python outputs the result “4.5.”
result = 2 + 2.5
print(result) # 4.5 is output
Python can perform calculations not only with expressions containing only int or float, but also with expressions containing a mix of int and float.
When outputting calculation results to TERMINAL, if “.0” is added to the value (e.g., “5.0”), the data is treated as a float type.
For example, if you enter “2.0 + 3” as shown in the following code, the value “5.0” will be output. Python performs calculations by converting integer values to float values when there are float values in the calculation expression. Therefore, the data type of the calculation result is float. (Python does not automatically convert “5.0” to an integer (“5”).)
result = 2.0 + 3
print(result) # Outputs “5.0” (float type),
To strictly check the type of a value, use the “type()” function. Specify the value you want to check inside the parentheses of “type()”, and it will return the type information of that value. You can confirm this information by using “print”.
print(type(result)) # Outputs "<class 'float'>"
(Note: “[type information]” refers to data that contains complex information that cannot be represented as simple numbers or strings. This will be explained in detail in the lecture on “class.”)
Note that you can split the code “print(type(result))” into two lines without any problems. The result will be the same.
data_type = type(result)
print(data_type) # Outputs "<class 'float'>"
Calculations with int and float (Example 2)
Now, let's look at an example of division.
In Python 3, the result of division is always returned as a float type. Even when calculating “integer/integer,” the result is returned as a float type. Even if the numbers are divisible, such as “4 / 2,” the result is returned as the float “2.0.”
(*When performing division in programming, use the slash (“/”) symbol.)
result1 = 5 / 2
print(result1) # “2.5” (float type) is output.
result2 = 4 / 2
print(result2) # “2.0” (float type) is output.
In Python3, if the result of division includes a decimal point, it is output as a float type. However, other programming languages may produce different results, so caution is required. (Note: The behavior is also different in Python2.)
In other programming languages, “integer / integer” may return only integers. For example, “9 / 2” returns “4” instead of “4.5.” Behavior varies by language, so check the specifications for each language. (This was the behavior in Python2.)
In programming languages that exhibit the behavior described above, if you want to divide two integers and obtain a floating-point result, convert either the divisor or the dividend to a floating-point type. In either of the following cases, the result will be returned as a floating-point type.
- int / float
- float / int
- float / float
The process of changing a type is called casting. To convert an int to a float, write “float(9).” This allows the program to recognize calculations such as “9 / 2” as “9.0 / 2,” so you can obtain the result as a float.
numA = 9 # int
numB = 2 # int
result = float(numA) / numB # "Convert to “float/int” format
print(result) # Obtain a float value
※ This is for Python2 and other languages. If you are using Python3, you can use “9 / 2” without any problems. (Even with Python3, always test your program and check what data types you are dealing with.)