Math and Statistics

Back

Loading concept...

Python’s Math Toolbox: Your Calculator Superpowers! 🧮

Imagine you have a magical calculator that never makes mistakes. But wait—your regular calculator can only do basic stuff like adding and subtracting. What if you need to find the square root of 144? Or figure out the average score of your class?

That’s where Python’s math module and statistics module come in. They’re like upgrading from a basic calculator to a super-powered scientific one!


The Math Module: Your Scientific Calculator

Think of the math module as a box full of special tools. Before you can use these tools, you need to open the box:

import math

Now you have access to incredible powers!

Square Roots Made Easy

Remember trying to figure out what number times itself equals 25? That’s a square root!

import math

result = math.sqrt(25)
print(result)  # 5.0

big_root = math.sqrt(144)
print(big_root)  # 12.0

Powers and Exponents

What’s 2 raised to the power of 10? The math module knows!

import math

power = math.pow(2, 10)
print(power)  # 1024.0

The Famous Pi (Ď€)

Pi is that special number (3.14159…) used for circles. No need to memorize it!

import math

print(math.pi)  # 3.141592653589793

# Circle area = π × radius²
radius = 5
area = math.pi * radius ** 2
print(area)  # 78.53981633974483

Rounding Numbers Your Way

import math

# Floor: rounds DOWN to nearest integer
print(math.floor(3.7))  # 3
print(math.floor(3.2))  # 3

# Ceiling: rounds UP to nearest integer
print(math.ceil(3.2))   # 4
print(math.ceil(3.7))   # 4

Think of it like stairs:

  • floor = step DOWN to the lower stair
  • ceil = step UP to the higher stair

The Statistics Module: Finding Patterns in Numbers

Imagine you’re a detective looking at a bunch of numbers, trying to find clues. The statistics module helps you discover patterns!

import statistics

Mean (Average)

You have 5 friends with different amounts of candies. How many would each person have if you shared equally?

import statistics

candies = [10, 20, 15, 25, 30]
average = statistics.mean(candies)
print(average)  # 20

Median (The Middle Value)

Line up all your numbers from smallest to biggest. The one in the middle is the median!

import statistics

scores = [85, 90, 78, 95, 88]
middle = statistics.median(scores)
print(middle)  # 88

Mode (Most Common Value)

What number appears the most often?

import statistics

votes = [1, 2, 2, 3, 2, 4, 2]
most_popular = statistics.mode(votes)
print(most_popular)  # 2

Standard Deviation

This tells you how spread out your numbers are. Are they all bunched together or scattered everywhere?

import statistics

close_scores = [98, 99, 100, 99, 98]
spread_scores = [50, 100, 75, 25, 100]

print(statistics.stdev(close_scores))
# Small number = scores are similar

print(statistics.stdev(spread_scores))
# Bigger number = scores vary a lot

Decimal Precision: When Every Digit Matters

Regular Python math can sometimes give weird results:

print(0.1 + 0.2)  # 0.30000000000000004
# Wait, shouldn't that be exactly 0.3?

This happens because computers store decimals in a tricky way. When you need exact precision (like counting money!), use the Decimal class:

from decimal import Decimal

price1 = Decimal('0.1')
price2 = Decimal('0.2')
total = price1 + price2
print(total)  # 0.3 (exactly!)

Setting Precision

Control exactly how many decimal places you want:

from decimal import Decimal, getcontext

getcontext().prec = 4  # 4 significant digits

result = Decimal('1') / Decimal('3')
print(result)  # 0.3333

Number Literals: Different Ways to Write Numbers

Python lets you write numbers in cool different ways!

Regular Integers

age = 25
population = 7900000000

Making Big Numbers Readable

Use underscores like commas:

# These are the same number!
big_number = 1000000
readable = 1_000_000

print(big_number == readable)  # True

Floating Point (Decimals)

temperature = 98.6
pi_approx = 3.14
tiny = 0.001

Scientific Notation

For really big or really tiny numbers:

# 3 Ă— 10^8 (speed of light!)
speed_of_light = 3e8
print(speed_of_light)  # 300000000.0

# 1.5 Ă— 10^-6 (very tiny)
tiny = 1.5e-6
print(tiny)  # 0.0000015

Binary, Octal, and Hexadecimal

Computers think in different number systems:

# Binary (base 2) - starts with 0b
binary = 0b1010
print(binary)  # 10

# Octal (base 8) - starts with 0o
octal = 0o17
print(octal)  # 15

# Hexadecimal (base 16) - starts with 0x
hex_num = 0xFF
print(hex_num)  # 255

Quick Reference Diagram

graph LR A["Python Math Tools"] --> B["math Module"] A --> C["statistics Module"] A --> D["Decimal Module"] A --> E["Number Literals"] B --> B1["sqrt, pow"] B --> B2["pi, e"] B --> B3["floor, ceil"] C --> C1["mean"] C --> C2["median"] C --> C3["mode"] C --> C4["stdev"] D --> D1["Exact Precision"] D --> D2["No Rounding Errors"] E --> E1["Integers: 42"] E --> E2["Floats: 3.14"] E --> E3["Scientific: 3e8"] E --> E4["Binary: 0b1010"]

The Big Picture

Tool When to Use It Example
math Scientific calculations math.sqrt(16) → 4.0
statistics Analyze data patterns mean([1,2,3]) → 2
Decimal Money, exact math Decimal('0.1')
Literals Write numbers clearly 1_000_000

You’ve Got This!

You just learned how to:

  • Use math for square roots, powers, and pi
  • Find averages, medians, and modes with statistics
  • Get exact decimal precision when it matters
  • Write numbers in multiple formats (binary, hex, scientific)

These tools transform Python into a powerful calculator that never makes mistakes. Go forth and calculate with confidence!

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.