0
0
Pythonprogramming~5 mins

Numeric values (int and float behavior) in Python

Choose your learning style9 modes available
Introduction

Numbers help us count and measure things in programs. Integers (int) are whole numbers, and floats are numbers with decimals.

When you need to count items like apples or books (use int).
When you measure something like weight or height that can have decimals (use float).
When you want to do math like adding, subtracting, multiplying, or dividing numbers.
When you want to store and use numbers in your program for calculations.
When you want to convert between whole numbers and decimal numbers.
Syntax
Python
integer_number = 10
float_number = 3.14

Integers are written without a decimal point.

Floats have a decimal point or use scientific notation like 1e3 (which means 1000).

Examples
This shows how Python treats numbers as int or float based on their form.
Python
a = 5
b = 2.0
print(type(a))  # <class 'int'>
print(type(b))  # <class 'float'>
You can convert an integer to a float using float().
Python
x = 7
y = float(x)
print(y)  # 7.0
Converting a float to int cuts off the decimal part (does not round).
Python
z = 4.5
w = int(z)
print(w)  # 4
Sample Program

This program shows adding and multiplying int and float numbers, and converting between them.

Python
num1 = 8
num2 = 3.5
sum_result = num1 + num2
product = num1 * num2
int_conversion = int(num2)
float_conversion = float(num1)

print(f"Sum: {sum_result}")
print(f"Product: {product}")
print(f"Integer conversion of {num2}: {int_conversion}")
print(f"Float conversion of {num1}: {float_conversion}")
OutputSuccess
Important Notes

Adding an int and a float results in a float.

Converting float to int removes decimals without rounding.

Use float when you need decimals, int when you want whole numbers only.

Summary

Integers are whole numbers without decimals.

Floats are numbers with decimals.

You can convert between int and float using int() and float().