float()

Python · Variables & Data Types

Theory

float() builds a floating-point number from another value. Integers become their decimal version (float(7) is 7.0), and strings are parsed if they look like a number: '19.99', ' 3.5 ' with spaces, scientific notation like '1e3' (which is 1000.0), and even the special words 'inf' and 'nan'. Text that is not numeric, such as 'abc' or '1,200' with a comma, raises ValueError.

Remember what a float really is: a 64-bit binary approximation. float('19.99') stores the closest binary value to 19.99, which is why arithmetic can show tiny errors and why == comparisons on floats are risky. When converting prices from text, clean the string first (remove commas and the rupee sign with replace()) and decide where rounding should happen.

Why we use it

Prices, percentages, ratings and measurements usually have decimals and often arrive as text from CSV files or web forms. float() turns that text into a number you can sum, average and compare.

When to use it

Use float() for measurement-like values where small rounding is acceptable. For exact currency, use decimal.Decimal('19.99'). Strip thousands separators before converting: float('1,200'.replace(',', '')).

How it works

  1. float(value) is called
  2. Is value already an int? Yes: Return the same number as a float, 7 -> 7.0. No: Treat it as text.
  3. Strip surrounding whitespace from the text
  4. Does it look like a number, e.g. 19.99, 1e3, inf? Yes: Convert to the nearest binary float. No: Raise ValueError.
  5. e.g. float('19.99') returns 19.99 of type float

Syntax

float(value)

Example

price = float("19.99")
print(price, type(price))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic