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.
Visual reference
What you will learn
- What is
float? — store real numbers with decimal places - Type promotion — why
int + floatbecomesfloat - 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.
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
intcounts apples →5 applesfloatmeasures weight →5.75 kg
A weighing scale needs decimals, so it uses floating-point values.
Creating a float
Assign a value with a decimal point (or use scientific notation).
price = 12.34
print(price)
print(type(price))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.
a = 10 # int
b = 1.2 # float
c = a + b
print(c)
print(type(c))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.
print(22 / 7)3.142857142857143Compare 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).
a = 3e2
b = 100e-2
print(a)
print(b)300.0
1.0Try 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:
0.000000000000000000000000000000000000000000000000000000000000000005Python displays it compactly:
a = 0.000000000000000000000000000000000000000000000000000000000000000005
print(a)5e-66Common in physics, finance, and machine learning.
Important rule: only decimal float literals
Valid:
10.5
0.25
3e8Not valid (from your notes):
# 0b1010.0b1111 # SyntaxError: invalid decimal literal
# 0o23.0b1010
# 0xAC.0o23Binary, octal, and hexadecimal prefixes belong to int literals. Convert when you need a float:
x = float(0b1010)
print(x)10.0Real-world applications
CGPA — 8.67
Percentage — 12.5%
Weight — 72.35 kg
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:
height = 172.5
print(height, type(height))Exercise 2 — predict, then run:
print(1 / 3)Answer: 0.3333333333333333
Exercise 3 — predict, then run:
print(1.5e3)Answer: 1500.0
Quick knowledge check
-
Why is
10 + 1.2a float?
One operand is alreadyfloat; Python promotes the result. -
What does
3e2mean?
3 × 10² =300.0. -
Is
0b101.1a valid float literal?
No — only decimal-style float literals;0bis for integers.
Summary
floatstores real numbers with decimal places or scientific notation.- Type promotion:
int + float→float. /returnsfloat;//returnsint.- Scientific notation (
e) compactly represents huge and tiny values. 0b/0o/0xcannot be used as float literals — usefloat(...)on an integer instead.
Next lesson: bool — Boolean data type (True, False, and logical values).
Loading playground…