Open In App

Difference Between Integer and Float in Python

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Integers are used to represent whole numbers without any decimal points, floats, or floating-point numbers, accommodate values with decimal places. Understanding the differences between these data types is important for effective programming and data manipulation in Python. In this article, we will explore the differences between integers and floats along with examples.

Integer in Python

In Python, an integer is a numeric data type representing whole numbers without any decimal points. Integers can be positive or negative and are commonly used for counting, indexing, and performing arithmetic operations. Python supports unlimited precision for integers, allowing them to be as large as the available system memory permits.

Example:

In this example, three integers are declared: positive_integer with a value of 42, negative_integer with -17, and large_integer with a significantly large value. The code demonstrates basic arithmetic operations, calculating the sum and product of these integers. Additionally, it showcases Python’s support for unlimited precision by calculating 2 to the power of 1000. Finally, the results are printed, including large values and unlimited precision.

Python3




positive_integer = 42
negative_integer = -17
large_integer = 9876543210123456789012345678901234567890
 
# arithmetic operations
sum_result = positive_integer + negative_integer
product_result = positive_integer * large_integer
 
# unlimited precision
unlimited_precision_result = 2 ** 1000
 
# displaying results
print("Positive Integer:", positive_integer)
print("Negative Integer:", negative_integer)
print("Large Integer:", large_integer)
print("Sum Result:", sum_result)
print("Product Result:", product_result)


Output

Positive Integer: 42
Negative Integer: -17
Large Integer: 9876543210123456789012345678901234567890
Sum Result: 25
Product Result: 414814814825185185138518518513851851851380


Float in Python

In Python, a float is a numeric data type representing decimal numbers. Floats are used when precision is required in mathematical calculations and when dealing with numbers that are not whole. Floats can be positive or negative and can also be expressed in scientific notation. Python supports double-precision floating-point numbers as defined by the IEEE 754 standard.

Example:

In this example, three float variables are declared: positive_float with a value of 3.14, negative_float with -0.5, and large_float with a significantly large decimal value. The code demonstrates basic arithmetic operations, calculating the sum and product of these float variables. Additionally, it showcases Python’s support for floating-point arithmetic and precision by performing arithmetic operations involving large and decimal values. Finally, the results are printed, including the values of the float variables, sum, and product.

Python3




positive_float = 3.14
negative_float = -0.5
large_float = 1234567890.12345678901234567890
 
# arithmetic operations
sum_result = positive_float + negative_float
product_result = positive_float * large_float
 
# displaying results
print("Positive Float:", positive_float)
print("Negative Float:", negative_float)
print("Large Float:", large_float)
print("Sum Result:", sum_result)
print("Product Result:", product_result)


Output

Positive Float: 3.14
Negative Float: -0.5
Large Float: 1234567890.1234567
Sum Result: 2.64
Product Result: 3876543174.987654


Difference Between Integer and Float in Python

The difference between Integer and Float is as follows:

Feature

Integer

Float

Definition

Whole numbers without decimal points

Numbers with decimal points

Declaration

e.g., x = 5

e.g., y = 3.14

Precision

Infinite precision

Limited precision

Operations

Supports arithmetic operations

Supports arithmetic operations

Division

Integer division returns an integer (floor)

Division returns float

Memory

Typically occupies less memory

Typically occupies more memory

Example

“x = 5”

“y = 3.14”

Range

Limited by system memory and resources

Limited by floating-point representation

Representation

Stored as binary numbers without fractions

Stored in IEEE 754 floating-point format

Accuracy

Accurate for counting and discrete values

May have rounding errors and representation issues

Conversion

Can be explicitly converted to float

Can be explicitly converted to an integer, rounding may occur

Use Cases

Ideal for counting, indexing, and whole numbers

Ideal for continuous data, measurements, and calculations

Syntax

Written without a decimal point

May include a decimal point and/or exponent



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads