Back to Blog

Python Tutorial for Beginners #4 - Numbers & Math (All 7 Operators, Math Module, Built-ins)

Sandy LaneSandy Lane

Video: Python Tutorial for Beginners #4 - Numbers & Math (All 7 Operators, Math Module, Built-ins) by Taught by Celeste AI - AI Coding Coach

Watch full page →

Python Numbers & Math: Operators, Math Module, and Built-ins

Understanding numbers and math operations is fundamental in Python programming. This tutorial covers all seven arithmetic operators, explores the math module for advanced functions, and demonstrates useful built-in functions for numerical tasks.

Code

# Arithmetic operators
a = 15
b = 4

print("Addition:", a + b)          # 19
print("Subtraction:", a - b)       # 11
print("Multiplication:", a * b)    # 60
print("Division:", a / b)           # 3.75 (float division)
print("Floor Division:", a // b)   # 3 (integer division)
print("Modulus:", a % b)            # 3 (remainder)
print("Exponentiation:", a ** b)   # 50625 (15 to the power of 4)

# Using the math module
import math

print("Square root of 16:", math.sqrt(16))    # 4.0
print("Ceiling of 4.2:", math.ceil(4.2))      # 5
print("Floor of 4.8:", math.floor(4.8))       # 4
print("Value of pi:", math.pi)                  # 3.141592653589793
print("Value of e:", math.e)                    # 2.718281828459045

# Built-in functions
nums = [3, -7, 2.5, 10, -1]

print("Absolute value of -7:", abs(-7))         # 7
print("Rounded value of 2.567:", round(2.567, 2))  # 2.57
print("Minimum:", min(nums))                     # -7
print("Maximum:", max(nums))                     # 10
print("Sum:", sum(nums))                         # 7.5

# Type conversion
x = 7.9
y = "12"

print("Convert float to int:", int(x))           # 7
print("Convert string to int:", int(y))          # 12
print("Convert int to float:", float(5))         # 5.0

Key Points

  • Python supports seven arithmetic operators for basic math, including floor division (//) and exponentiation (**).
  • The math module provides functions like sqrt, ceil, floor, and constants such as pi and e for advanced calculations.
  • Built-in functions like abs, round, min, max, and sum simplify common numerical operations on values and collections.
  • Type conversion functions int() and float() allow changing between numeric types and parsing strings to numbers.
  • Floor division (//) returns the integer quotient, while regular division (/) returns a float result.