Nearby lessons

16 of 159

Python - 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

🐍Code Cell
1f = 10.5
2 
3print(f)
4print(type(f))
Output
10.5

Floating-Point Numbers

Float values represent numbers with a decimal point.

Some examples are:

🐍Code Cell
1a = 10.5
2b = 0.25
3c = -15.75
4 
5print(a)
6print(b)
7print(c)
Output
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

🐍Code Cell
1f = 1.2e3
2 
3print(f)
Output
1200.0

Explanation

The value 1.2e3 means:

1.2 × 10³

So the result is 1200.0.

More Examples

🐍Code Cell
1a = 5e2
2b = 3.5E3
3 
4print(a)
5print(b)
Output
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

🐍Code Cell
1population = 7.8e9
2 
3print(population)
Output
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

🐍Code Cell
1f = 0B11.01
Output
SyntaxError

Invalid Octal Float

🐍Code Cell
1f = 0o123.456
Output
SyntaxError

Invalid Hexadecimal Float

🐍Code Cell
1f = 0X123.456
Output
SyntaxError

Valid Float Examples

🐍Code Cell
1a = 10.5
2b = 0.123
3c = -15.75
4d = 1.2e3
5 
6print(a)
7print(b)
8print(c)
9print(d)
Output
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.

🐍Code Cell
1x = 25.5
2 
3print(type(x))
Output
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

🐍Code Cell
1a = 10
2b = 10.5
3 
4print(type(a))
5print(type(b))
Output
No output captured.

Important Points

  • The float data type stores decimal numbers.
  • Float values are immutable.
  • Scientific notation is supported.
  • Both e and E can be used in scientific notation.
  • Binary, octal, and hexadecimal float literals are not allowed.

Key Points

  • The float data 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 Typefloat
PurposeStores decimal numbers
Example10.5
Scientific NotationSupported
Binary FloatNot Allowed
Octal FloatNot Allowed
Hexadecimal FloatNot Allowed
MutableNo (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 Questions
Progress: 0 / 8