HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
abs()round()min() / max()sum()pow()math.sqrt()
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›REFERENCE›Numbers & Math
🔢

Numbers & Math

Numeric operations, rounding and math functions.

CommandDescription
abs()Gives the absolute (unsigned) value of a number.
round()Rounds a number to the given number of decimal places.
min() / max()Finds the smallest / largest value.
sum()Adds up the numbers in a sequence.
pow()Raises to a power (base to the power of exponent).
math.sqrt()Takes the square root (math module).

abs()

Try it yourself →

Gives the absolute (unsigned) value of a number.

Syntax

abs(number)

Description

Makes it positive if negative, leaves it unchanged if positive. Works on integers and floats.

Returns

The absolute value of the number.

Example

print(abs(-7))
print(abs(3.5))

Expected output

7
3.5

When to use?

Measuring the difference between two values without a sign (e.g. |target - actual|).

round()

Try it yourself →

Rounds a number to the given number of decimal places.

Syntax

round(number, digits)

Description

If the second argument isn't given, it rounds to the nearest integer. If given, it rounds to that many decimal places.

Parameters

numberThe number to round.
digits(optional)How many decimal places to keep (default 0).

Returns

The rounded number.

Example

print(round(3.14159, 2))
print(round(2.7))

Expected output

3.14
3

When to use?

For making values like money, averages and percentages readable.

Common mistakes

  • ⚠️ round(2.5) gives 2 (banker's rounding); it doesn't always round up.

min() / max()

Try it yourself →

Finds the smallest / largest value.

Syntax

min(a, b, ...) · max(arr)

Description

Provide multiple arguments or an arr (items, etc.); returns the smallest (min) or largest (max).

Returns

The smallest or largest element.

Example

print(min(4, 2, 9))
print(max([3, 7, 1]))

Expected output

2
7

When to use?

For finding the highest score or the lowest price in a list.

sum()

Try it yourself →

Adds up the numbers in a sequence.

Syntax

sum(arr, start=0)

Description

Adds up all the numbers in a sequence like a list/tuple. An optional starting value can be added.

Returns

The total value.

Example

print(sum([10, 20, 30]))
print(sum([1, 2, 3], 100))

Expected output

60
106

When to use?

When computing a cart total, a score total, or an average (sum/len).

pow()

Try it yourself →

Raises to a power (base to the power of exponent).

Syntax

pow(base, exponent)

Description

Same as base ** exponent. If a third argument is given, it takes the modulus: pow(a, b, m).

Returns

The exponentiated result.

Example

print(pow(2, 10))
print(2 ** 3)

Expected output

1024
8

When to use?

Exponential growth, squaring/cubing, cryptographic modular operations.

math.sqrt()

Try it yourself →

Takes the square root (math module).

Syntax

import math
math.sqrt(number)

Description

Operations like square root, pi, and rounding up/down are in the math module. You must write import math before using them.

Returns

The square root (float).

Example

import math
print(math.sqrt(144))
print(round(math.pi, 4))

Expected output

12.0
3.1416

When to use?

Geometry, distance (Pythagoras), statistical calculations.

Common mistakes

  • ⚠️ Forgetting to write import math: gives a 'math is not defined' error.
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code