Mathematical Operations & The Math Module
Turn Python into a powerful calculator. Learn basic arithmetic operators and explore the built-in math module for advanced calculations.
Basic Arithmetic
Python handles math beautifully. Here are the tools you'll use every day:
- Addition & Subtraction:
+and- - Multiplication & Division:
*and/(Division always returns a Float!) - Floor Division (
//): Divides and chops off the decimal, returning a whole integer. - Modulus (
%): Returns the remainder of a division. Super useful for finding even/odd numbers (e.g.,x % 2 == 0). - Exponentiation (
**): To the power of.2 ** 3is 8.
The Math Module
When you need more than basic arithmetic, Python has a built-in module full of mathematical goodies. You just have to import it.
import math
# Find the square root
result = math.sqrt(144) # 12.0
# Get the value of Pi
pi_value = math.pi
# Round a number UP to the nearest integer
ceil_val = math.ceil(4.2) # 5