Aral Code

Variables & Data Lesson 7 of 23

Python Numbers and Math

Arithmetic in Python — +, -, *, /, //, % and **, order of operations, round(), abs(), min(), max(), sum() and the math module.

Python is an excellent calculator. It handles whole numbers, decimals and huge numbers, and it has built-in functions for rounding, finding the largest value and more.

Arithmetic operators

OperatorMeaningExampleResult
+Add25 + 1540
-Subtract100 - 4555
*Multiply12 * 336
/Divide (always float)7 / 23.5
//Floor division (whole part)7 // 23
%Remainder (modulo)7 % 21
**Power2 ** 101024

// and % together

Floor division and remainder are a perfect pair for "how many groups, and how many left over?":

students = 43
per_group = 5
print("Groups:", students // per_group)
print("Left over:", students % per_group)

minutes = 135
print(f"{minutes // 60} hours and {minutes % 60} minutes")

Output

Groups: 8
Left over: 3
2 hours and 15 minutes

Order of operations

Python follows PEMDAS: parentheses, exponents, multiplication/division, addition/subtraction.

print(2 + 3 * 4)
print((2 + 3) * 4)
print((88 + 91 + 85) / 3)

Output

14
20
88.0

Useful built-in functions

grades = [88, 95, 79, 91]

print(round(88.456, 2))     # round to 2 decimals
print(round(89.5))          # round to whole number
print(abs(-15))             # absolute value
print(max(grades))          # largest
print(min(grades))          # smallest
print(sum(grades))          # total
print(sum(grades) / len(grades))   # average

Output

88.46
90
15
95
79
353
88.25

The math module

For more functions, import the math module at the top of your program:

import math

print(math.sqrt(144))       # square root
print(math.pi)              # π
print(math.ceil(41 / 5))    # round up: tricycles needed for 41 people
print(math.floor(9.99))     # round down
print(math.pow(2, 3))       # 2 to the power of 3, as a float

Output

12.0
3.141592653589793
9
9
8.0

Example: area and circumference

import math

radius = float(input("Radius of the garden (m): "))
area = math.pi * radius ** 2
circumference = 2 * math.pi * radius

print(f"Area: {area:.2f} m²")
print(f"Fence needed: {circumference:.2f} m")

Output

Radius of the garden (m): 3
Area: 28.27 m²
Fence needed: 18.85 m

Formatting numbers

price = 1234567.891
print(f"{price:.2f}")       # 2 decimals
print(f"{price:,.2f}")      # with commas
print(f"₱{price:,.2f}")
print(f"{0.875:.1%}")       # as a percentage

Output

1234567.89
1,234,567.89
₱1,234,567.89
87.5%

Decimal surprises

print(0.1 + 0.2)

Output

0.30000000000000004

Computers store decimals in binary, so tiny errors appear. This happens in almost every language. For display, use f-string formatting like :.2f. For serious money calculations, Python has a decimal module that stores exact decimal values.

Big numbers

print(2 ** 100)
print(1_000_000 * 3)   # underscores make big numbers readable

Output

1267650600228229401496703205376
3000000

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What is 17 // 5?
2. What is 2 ** 4?
3. What do you need before using math.sqrt()?
4. What does f"{1500:,}" show?

By Sir Reyel · Last updated 2026-12-04