Python Math

Python · Working with Data

Theory

The built-in math module adds mathematical functions beyond the basic + - * / operators. Common ones are sqrt() for square root, pow() for powers, ceil() to round up to the next whole number, floor() to round down, trunc() to drop the decimal part, log() and exp() for logarithms and exponentials, and sin()/cos() for trigonometry. It also provides the constants math.pi and math.e.

math.sqrt(16) returns 4.0 (always a float), math.ceil(4.2) returns 5 and math.floor(4.2) returns 4. With negatives the difference matters: ceil(-4.2) is -4 and floor(-4.2) is -5, while trunc(-4.2) is -4. math works on single numbers; for whole arrays of numbers, NumPy's versions (np.sqrt) are much faster.

Why we use it

Analytics and finance code constantly needs rounding rules, square roots, logs and growth calculations. math gives tested, precise implementations so you do not have to write them yourself.

When to use it

Use math for scalar calculations in plain Python. For lists or columns of numbers use NumPy or pandas, and for money that must not suffer float rounding, use the decimal module.

How it works

  1. import math
  2. Pick the function: sqrt, ceil, floor, log ...
  3. Pass a number
  4. Is the input valid for the function? Yes: Return the result. No: Raise ValueError (e.g. sqrt(-1)).
  5. Use the result in your calculation

Syntax

import math
math.sqrt(x)

Example

math.sqrt(16)  # 4.0
math.ceil(4.2)  # 5

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