Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials Python Numbers
Python Beginner FREE

Numbers

Lesson 2 of 14 Beginner Interactive

Python supports integers (int) and floats (float). Integers have arbitrary precision.

Math Operations

  • + - * basic arithmetic
  • / float division, // floor division
  • % modulo, ** exponentiation

Math & Random Modules

math.sqrt(), math.ceil(), math.floor(), random.randint()

Syntax

PYTHON
import math, random
print(10 / 3)    # 3.333...
print(10 // 3)   # 3
print(10 ** 3)   # 1000
print(math.sqrt(16))   # 4.0
print(math.pi)         # 3.14159...
print(random.randint(1, 10))
Python Numbers and Math
PYTHON
a, b = 10, 3

print("Add:", a + b)
print("Subtract:", a - b)
print("Multiply:", a * b)
print("Divide:", a / b)
print("Floor div:", a // b)
print("Modulus:", a % b)
print("Power:", a ** b)

# Built-ins
print("Round:", round(3.7))
print("Abs:", abs(-5))
print("Min:", min(3, 1, 2))
print("Max:", max(3, 1, 2))
print("Sum:", sum([1, 2, 3]))

# Import math
import math
print("Sqrt:", math.sqrt(16))
print("Pi:", round(math.pi, 4))

Practice

1
Exercise

Calculate triangle area given base=10, height=5.

Answer
print(0.5 * 10 * 5)
2
Exercise

Print 5 random numbers 1-50.

Answer
import random
for _ in range(5): print(random.randint(1,50))

Quick Quiz

1

What is 17 // 5?

Floor division returns 3.

Interview Questions

/ returns float (3.333), // returns floor integer (3).