Mathematical Operations

Python · NumPy Library

Theory

NumPy's universal functions (ufuncs) apply a maths function to every element and return a new array. Common ones are np.sqrt, np.exp, np.log (natural log), np.log10, np.abs, np.round(arr, 2), np.floor and np.ceil. Two-array ufuncs work position by position: np.maximum(a, b) keeps the larger value at each position and np.power(a, 2) squares.

np.clip(arr, low, high) caps values into a range, so np.clip(marks, 0, 100) turns 110 into 100. Ufuncs follow maths rules: np.sqrt of a negative number gives nan and np.log(0) gives -inf, each with a RuntimeWarning rather than an error. Because they run in compiled code they are much faster than calling math.sqrt in a loop, and they also accept pandas Series and return results with the same index.

Why we use it

Data work needs transformations like log for skewed amounts, square roots for distances, rounding for display and clipping to remove impossible values. Ufuncs do these for a whole array in one call, quickly and readably.

When to use it

Use ufuncs instead of Python's math module whenever the input is an array. Use np.log1p for data that contains zeros, np.clip to cap outliers, and np.round only for display, not before further calculations.

How it works

  1. Numeric array
  2. Pick a ufunc: np.sqrt, np.log, np.round, np.clip ...
  3. Can any value be outside the function's domain (negative, zero)? Yes: Clean or use np.log1p first. No: Apply directly.
  4. Compiled loop computes the function for every element
  5. New array with the same shape

Syntax

np.sqrt(a)  np.exp(a)  np.log(a)  np.log10(a)
np.abs(a)  np.round(a, 2)  np.floor(a)  np.ceil(a)
np.clip(a, low, high)  np.maximum(a, b)

Example

x = np.array([16.0, 49.0, 110.0])
print(np.sqrt(x))
print(np.clip(x, 0, 100))

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