SyntaxStudy
Sign Up
Python Beginner 7 min read

Number Operations

Number Operations in Python

Python's math module provides advanced mathematical functions.

import math

print(math.sqrt(16))    # 4.0
print(math.ceil(4.2))   # 5
print(math.floor(4.9))  # 4
print(math.pi)          # 3.14159...
print(math.pow(2, 10))  # 1024.0
print(abs(-5))          # 5
print(round(3.7))       # 4
print(max(1, 5, 3))     # 5
print(min(1, 5, 3))     # 1

Random Numbers

import random
print(random.randint(1, 100))
print(random.choice([1, 2, 3, 4, 5]))
Pro Tip

Use int(), float(), str() to cast between types.