Python int and float

Introduction

In Python, numbers are one of the most commonly used data types. Two important numeric data types are:

  • int → Used for whole numbers
  • float → Used for decimal numbers

These data types are essential in programming because they are used in calculations, data processing, machine learning, statistics, finance applications, AI systems, and almost every software application.

Integer (int)

What is an Integer?

An integer (int) is a whole number without any decimal point.

Examples

x = 10
y = -25
z = 0

In the above examples:

  • 10 is a positive integer
  • -25 is a negative integer
  • 0 is also an integer

Characteristics of int

  • Stores whole numbers only
  • Can store positive and negative values
  • Does not contain decimal points
  • Supports arithmetic operations
  • Memory efficient for whole number calculations

Checking Integer Type

You can use the type() function to check the data type.

num = 50
print(type(num))

Output

<class 'int'>

Arithmetic Operations with Integers

Addition

a = 10
b = 5
print(a + b)

Subtraction

print(a - b)

Multiplication

print(a * b)

Division

print(a / b)

Floor Division

print(a // b)

Modulus

print(a % b)

Exponent

print(a ** b)

Float (float)

What is a Float?

A float (float) is a number that contains a decimal point.

Examples

price = 99.99
height = 5.8
pi = 3.14159

All these values are floating-point numbers.

Characteristics of float

  • Stores decimal numbers
  • Supports fractional values
  • Used in scientific calculations
  • More precise for measurements and mathematical operations
  • Can represent very large and very small numbers

Checking Float Type

value = 10.5
print(type(value))

Output

<class 'float'>

Arithmetic Operations with Floats

Addition

x = 5.5
y = 2.5
print(x + y)

Subtraction

print(x - y)

Multiplication

print(x * y)

Division

print(x / y)

Difference Between int and float

Integer Example

a = 10
print(type(a))

Output

<class 'int'>

Float Example

b = 10.0
print(type(b))

Output

<class 'float'>

Even though both represent the value ten, Python treats them differently because one has a decimal point.

Type Conversion

Python allows conversion between int and float.

Converting int to float

num = 25
converted = float(num)

print(converted)
print(type(converted))

Output

25.0
<class 'float'>

Converting float to int

value = 9.8
converted = int(value)

print(converted)
print(type(converted))

Output

9
<class 'int'>

Important Note

When converting float to int, Python removes the decimal part instead of rounding.

User Input and Numeric Types

By default, the input() function returns data as a string.

Integer Input

age = int(input("Enter your age: "))
print(age)

Float Input

salary = float(input("Enter your salary: "))
print(salary)

Practical Examples

Example 1 — Total Marks

math = 80
science = 75
english = 90

average = (math + science + english) / 3

print(average)

Since division produces a decimal value, the result becomes a float.

Example 2 — Shopping Bill

item1 = 99.99
item2 = 149.50

bill = item1 + item2

print(bill)

Float Precision

Sometimes floating-point calculations may produce unexpected results because of computer memory representation.

Example

print(0.1 + 0.2)

Output

0.30000000000000004

This happens because computers store floating-point numbers in binary format.

Useful Functions

round()

Used to round float values.

value = 5.6789
print(round(value, 2))

Output

5.68

abs()

Returns the absolute value.

print(abs(-10))

Output

10

max() and min()

print(max(10, 20, 30))
print(min(10, 20, 30))

Real-World Usage

Integer Applications

  • Counting students
  • Number of products
  • Age values
  • Quantity tracking
  • Roll numbers

Float Applications

  • Temperature measurements
  • AI and machine learning calculations
  • Scientific data
  • Financial systems
  • Percentage calculations
  • Height and weight measurements

Common Mistakes

Mistake 1 — Using String Instead of Integer

age = "25"
print(age + 5)

This will produce an error because strings and integers cannot be added directly.

Correct Way

age = int("25")
print(age + 5)

Mistake 2 — Expecting Rounded Conversion

print(int(9.9))

Output

9

int() truncates the decimal part.

Summary

  • int stores whole numbers
  • float stores decimal numbers
  • Both support mathematical operations
  • type() checks the datatype
  • int() and float() perform type conversion
  • Floats are important for precision calculations
  • Integers are useful for counting and indexing

Understanding int and float is fundamental in Python programming because almost every application uses numeric operations in some form.