Nearby lessons
16 of 159Python - float Data Type
📌 What You Will Learn
- Define the float data type and the values it stores
- Write float values in scientific notation using e or E
- Identify why binary, octal, and hexadecimal float literals are invalid
- Compare int and float data types
- Use the type() function to check whether a value is a float
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
Floating-Point Numbers
Float values represent numbers with a decimal point.
Some examples are:
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
Explanation
The value 1.2e3 means:
1.2 × 10³
So the result is 1200.0.
More Examples
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
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
Invalid Octal Float
Invalid Hexadecimal Float
Valid Float Examples
Checking the Data Type
The type() function is used to check the data type of a value.
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
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) |
📝 Key Takeaways
- The float data type stores decimal numbers that contain a decimal point
- Scientific notation uses e or E, for example 1.2e3 equals 1200.0
- Float values support only decimal representation
- Binary, octal, and hexadecimal float literals are not allowed in Python
- int stores whole numbers, while float stores decimal numbers
🧠 Test Your Knowledge
8 QuestionsProgress: 0 / 8