Nearby lessons
16 of 108Python - float Data Type
Introduction
The float data type is used to store floating-point numbers (decimal numbers).
A float value always contains a decimal point.
Python supports both positive and negative decimal values.
Example
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10.5
Floating-Point Numbers
Float values represent numbers with a decimal point.
Some examples are:
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10.5 0.25 -15.75
Scientific Notation (Exponential Form)
Python also allows float values to be written in scientific notation.
You can use either e or E.
Syntax:
numberePower
or
numberEPower
Example of Scientific Notation
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1200.0
Explanation
The value 1.2e3 means:
1.2 × 10³
So the result is 1200.0.
More Examples
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
500.0 3500.0
Advantages of Scientific Notation
- Represents very large numbers easily.
- Makes code easier to read.
- Useful in scientific calculations.
- Commonly used in engineering and data science.
Example
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
7800000000.0
Float Values Support Only Decimal Form
Unlike integers, float values can only be written in decimal form.
Float values cannot be represented using binary, octal, or hexadecimal literals.
Invalid Binary Float
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
SyntaxError
Invalid Octal Float
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
SyntaxError
Invalid Hexadecimal Float
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
SyntaxError
Valid Float Examples
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10.5 0.123 -15.75 1200.0
Checking the Data Type
The type() function is used to check the data type of a value.
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.
Difference Between int and float
| int | float |
|---|---|
| Stores whole numbers. | Stores decimal numbers. |
| No decimal point. | Contains a decimal point. |
| Example: 10 | Example: 10.5 |
Example
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.
Important Points
- The
floatdata type stores decimal numbers. - Float values are immutable.
- Scientific notation is supported.
- Both
eandEcan be used in scientific notation. - Binary, octal, and hexadecimal float literals are not allowed.
Key Points
- The
floatdata type represents decimal numbers. - Float values always contain a decimal point.
- Scientific notation can be used for very large or very small values.
- Use
type()to check whether a value is a float. - Float values support only decimal representation.
Quick Summary
| Feature | Description |
|---|---|
| Data Type | float |
| Purpose | Stores decimal numbers |
| Example | 10.5 |
| Scientific Notation | Supported |
| Binary Float | Not Allowed |
| Octal Float | Not Allowed |
| Hexadecimal Float | Not Allowed |
| Mutable | No (Immutable) |
🧠 Test Your Knowledge
8 QuestionsProgress: 0 / 8
Keep Going!Python - complex Data Type