Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 02

float — Floating-Point Data Type

Learn how Python stores real numbers, why 10 + 1.2 becomes a float, and how scientific notation represents extremely large and tiny values.

Beginner15 min

Visual reference

Precision scale — decimals matter for real measurementsPrecision scale — decimals matter for real measurements
Rounding and decimal places in PythonRounding and decimal places in Python
Scientific notation in maths and scienceScientific notation in maths and science

What you will learn

  • What is float? — store real numbers with decimal places
  • Type promotion — why int + float becomes float
  • Scientific notation — 3e2, 100e-2, 5e-66
  • Important rule — binary, octal, and hex float literals are invalid

Big picture: where does float fit?

In the previous lesson, int stored whole numbers. float stores real numbers — values with a fractional (decimal) part.

int vs float — whole numbers versus decimal numbers

Key idea: both are numeric types, but float can represent fractions while int cannot.

What is a floating-point number?

float is a pre-defined class and a fundamental type.

Purpose: store real constant values — numbers with decimal places — such as percentages, CGPA, commission, or DA amounts.

Valid floats Not floats
12.34, 0.5, -7.25, 3e8 10, 100, 0b101 (those are int)

Analogy

  • int counts apples → 5 apples
  • float measures weight → 5.75 kg

A weighing scale needs decimals, so it uses floating-point values.

Counting whole items — intCounting whole items — int
Measuring the body — fractional valuesMeasuring the body — fractional values
Weight scale — float in daily lifeWeight scale — float in daily life

Creating a float

Assign a value with a decimal point (or use scientific notation).

python
price = 12.34

print(price)
print(type(price))
text
12.34
<class 'float'>

Python sees the decimal point and stores the value as float.

Why does 10 + 1.2 become a float?

Type promotion: if any operand is a float, Python promotes the result to float so you do not lose the fractional part.

10 (int) + 1.2 (float) = 11.2 (float)
python
a = 10      # int
b = 1.2     # float
c = a + b

print(c)
print(type(c))
text
11.2
<class 'float'>

Interactive: type promotion

Use the playground at the bottom of this page. Change a and b in the starter code, run again, and check type(c).

Division always returns a float

Even when both operands are int, / produces a float because the answer can have a fractional part.

python
print(22 / 7)
text
3.142857142857143

Compare the two division operators:

Expression Result Type
22 / 7 3.142857... float
22 // 7 3 int
  • / — floating-point division
  • // — integer (floor) division

Scientific notation (exponent form)

General form: mantissa e exponent → value = mantissa × 10exponent.

Advantage: compact storage for very large or very small values (your notes: minimizes memory for big floating-point values).

3e2 means 3 × 10² = 300.0
python
a = 3e2
b = 100e-2

print(a)
print(b)
text
300.0
1.0

Try these mentally, then verify in the playground:

Expression Meaning Value
3e2 3 × 10² 300.0
5e3 5 × 10³ 5000.0
8e-2 8 × 10⁻² 0.08
1.5e3 1.5 × 10³ 1500.0

Change the exponent in expressions like 3e2 in the playground to explore scientific notation interactively.

Why computers use scientific notation

Storing this literal is awkward:

text
0.000000000000000000000000000000000000000000000000000000000000000005

Python displays it compactly:

python
a = 0.000000000000000000000000000000000000000000000000000000000000000005
print(a)
text
5e-66

Common in physics, finance, and machine learning.

Important rule: only decimal float literals

Valid:

python
10.5
0.25
3e8

Not valid (from your notes):

python
# 0b1010.0b1111   # SyntaxError: invalid decimal literal
# 0o23.0b1010
# 0xAC.0o23

Binary, octal, and hexadecimal prefixes belong to int literals. Convert when you need a float:

python
x = float(0b1010)
print(x)
text
10.0

Real-world applications

CGPA example — 8.67CGPA example — 8.67

CGPA — 8.67

Percentage — 12.5%Percentage — 12.5%

Percentage — 12.5%

Weight — 72.35 kgWeight — 72.35 kg

Weight — 72.35 kg

Scientific measurement — 5e-9Scientific measurement — 5e-9

Scientific data — 5e-9

Memory tip: int vs float

Use int when… Use float when…
Counting students Measuring height
Employee ID Salary percentage
Number of products Temperature
Age (whole years) Scientific measurements

Rule of thumb: if the value can have a fractional part, choose float.

Practice exercises

Exercise 1 — store your height as a float:

python
height = 172.5
print(height, type(height))

Exercise 2 — predict, then run:

python
print(1 / 3)

Answer: 0.3333333333333333

Exercise 3 — predict, then run:

python
print(1.5e3)

Answer: 1500.0

Quick knowledge check

  1. Why is 10 + 1.2 a float?
    One operand is already float; Python promotes the result.

  2. What does 3e2 mean?
    3 × 10² = 300.0.

  3. Is 0b101.1 a valid float literal?
    No — only decimal-style float literals; 0b is for integers.

Summary

  • float stores real numbers with decimal places or scientific notation.
  • Type promotion: int + float → float.
  • / returns float; // returns int.
  • Scientific notation (e) compactly represents huge and tiny values.
  • 0b / 0o / 0x cannot be used as float literals — use float(...) on an integer instead.

Next lesson: bool — Boolean data type (True, False, and logical values).

Loading playground…