Numbers are everywhere. From managing finances to analyzing data, we constantly interact with them. But what exactly are numbers?
In this post, we’ll explore the beautiful journey of numbers through number theory, and bring it all together with a Python program that showcases each type of number in action.
The Evolution of Numbers
Natural Numbers
Natural numbers are the first we learn: 1, 2, 3…
1 2 3 4 |
natural_numbers = list(range(1, 6)) print("Natural Numbers:", natural_numbers) |
0️⃣ Whole Numbers
Add zero to natural numbers and you get whole numbers.
1 2 3 4 |
whole_numbers = list(range(0, 6)) print("Whole Numbers:", whole_numbers) |
➕➖ Integers
Integers include negative numbers, zero, and positive numbers.
1 2 3 4 |
integers = list(range(-3, 4)) print("Integers:", integers) |
Rational and Irrational Numbers
➗ Rational Numbers
Rational numbers can be expressed as fractions or finite decimals.
1 2 3 4 5 6 |
from fractions import Fraction rational_numbers = [Fraction(1, 2), Fraction(3, 4), 1.25, 2] print("Rational Numbers:", rational_numbers) |
Irrational Numbers
These cannot be expressed exactly as fractions.
1 2 3 |
2 * (3 + 2)**2 / 5 - 4 |
Step-by-step breakdown:
-
Parentheses:
(3 + 2)
→5
-
Exponents:
5**2
→25
-
Multiplication:
2 * 25
→50
-
Division:
50 / 5
→10.0
-
Subtraction:
10.0 - 4
→6.0
✅ Python Code Example 1:
1 2 3 4 |
my_value = 2 * (3 + 2)**2 / 5 - 4 print(my_value) # Output: 6.0 |
Use Parentheses for Clarity
Even if Python follows PEMDAS, using extra parentheses makes your code easier to read and maintain.
✅ Python Code Example 2 (clearer):
1 2 3 4 |
my_value = 2 * ((3 + 2)**2 / 5) - 4 print(my_value) # Output: 6.0 |
Pro Tip: When in doubt, use parentheses to group parts of your expression clearly!
What Are Variables?
A variable is a name that stores a value. It could be a number, a word, or even a list. In math, we often use letters like x
, y
, or z
. In Python, you can name your variables almost anything—just follow the rules!
✅ Example: Getting user input and using a variable
1 2 3 4 5 |
x = int(input("Please input a number: ")) product = 3 * x print("3 times your number is:", product) |
If you input 5
, Python multiplies it by 3 and prints:
1 2 3 |
3 times your number is: 15 |
Naming Variables in Python
Some math variables have Greek names like θ (theta) or β (beta). Since Python doesn’t support Greek characters easily, we name them like this:
✅ Example:
1 2 3 4 5 6 |
theta = 30.0 beta = 1.75 print("Theta is:", theta) print("Beta is:", beta) |
Subscripted Variables (x₁, x₂, x₃)
In math, you might see variables with subscripts (e.g. x₁
, x₂
, x₃
). In Python, you can use underscores for that:
✅ Example:
1 2 3 4 5 6 7 |
x1 = 3 # or x_1 x2 = 10 # or x_2 x3 = 44 # or x_3 print("x1 + x2 + x3 =", x1 + x2 + x3) |
Output:
1 2 3 |
x1 + x2 + x3 = 57 |
Wrapping Up
Understanding order of operations and how to use variables are foundational in both math and Python. Here’s the combined final example with everything we discussed:
✅ Final All-in-One Python Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import math # Order of operations value = 2 * ((3 + 2)**2 / 5) - 4 print("Final value (PEMDAS example):", value) # Variables from input x = int(input("Please input a number: ")) product = 3 * x print("3 times your number is:", product) # Greek-style variables beta = 1.75 theta = 30.0 print("Beta:", beta) print("Theta:", theta) # Subscripted variables x1, x2, x3 = 3, 10, 44 print("x1 + x2 + x3 =", x1 + x2 + x3) |
What is a Function?
A function defines a relationship between input variables (independent variables) and an output variable (dependent variable).
Mathematically, this is often written as:
Working with Functions in Python
✅ Linear Function Example
1 2 3 4 5 6 7 8 |
def f(x): return 2 * x + 1 x_values = [0, 1, 2, 3] for x in x_values: print(f(x)) |
This prints:
1 2 3 4 5 6 |
1 3 5 7 |
Continuous Functions
Unlike using only whole numbers (0, 1, 2, …), real functions allow infinitely many inputs (e.g., 0.1, 0.01, 0.001), making the function continuous.
For example:
x | y = 2x + 1 |
---|---|
0.0 | 1 |
0.5 | 2 |
1.0 | 3 |
1.5 | 4 |
2.0 | 5 |
️ Visualizing Functions (Graphing)
We can use SymPy, a symbolic math library in Python, to plot functions.
Linear Function
1 2 3 4 5 6 |
from sympy import * x = symbols('x') f = 2*x + 1 plot(f) |
Exponential Function (Parabola)
1 2 3 4 5 6 |
from sympy import * x = symbols('x') f = x**2 + 1 plot(f) |
This creates a curve, not a straight line. Curves like these are called curvilinear functions.
Functions with Two Variables
You can define functions of two independent variables, like:
This requires 3D plotting:
1 2 3 4 5 6 7 8 |
from sympy import * from sympy.plotting import plot3d x, y = symbols('x y') f = 2*x + 3*y plot3d(f) |
Key Concepts
Concept | Meaning |
---|---|
Function | A rule that maps input(s) to a single output. |
Independent variable | Input variable (like `x`) |
Dependent variable | Output (like `y` or `f(x)`) |
Continuous function | A function where you can plug in infinitely small steps. |
Curvilinear function | A continuous but non-linear function (e.g., parabolas) |
SymPy | A Python library for symbolic math and graphing. |
What Is a Summation?
A summation is a mathematical operation represented by the Greek letter sigma (Σ). It tells you to add a bunch of terms together.
Basic Summation Using a Loop in Python
Let’s say you want to multiply each number from 1 to 5 by 2, and add all the results:
1 2 3 4 |
summation = sum(2 * i for i in range(1, 6)) print(summation) # Output: 30 |
-
range(1, 6)
gives numbers 1 through 5 (6 is not included). -
Each number
i
is multiplied by2
. -
sum()
adds them all together.
Summing List Values After Multiplying Each by 10
Suppose you have a list of numbers:
1 2 3 |
x = [1, 4, 6, 2] |
You want to multiply each number by 10
, then sum the result:
Understanding Logarithms in Python: A Powerful Math Function for Data and Beyond
Logarithms may sound like a topic reserved for dusty math books, but they are incredibly useful—especially in fields like data science, machine learning, cryptography, and even audio processing.
Let’s explore logarithms step by step, including how to use them in Python, and how they relate to exponents.
What Is a Logarithm?
A logarithm answers the question:
“To what power must we raise a number (base) to get another number?”
For example:
What power of 2 gives us 8?
Mathematically:
1 2 3 |
2^x = 8 |
To solve for x
, we rewrite this using logarithm notation:
1 2 3 |
log₂(8) = x |
Since 2³ = 8, the answer is:
1 2 3 |
x = 3 |
General Form of a Logarithm
1 2 3 |
If a^x = b, then logₐ(b) = x |
Where:
-
a
is the base -
b
is the result -
x
is the exponent
Calculating Logarithms in Python
Let’s see how to compute logs in Python:
✅ Example 1: Base 2 Logarithm
1 2 3 4 5 6 7 |
from math import log # What power of 2 gives 8? x = log(8, 2) print(x) # Output: 3.0 |
✅ Example 2: Natural Logarithm (base e
)
1 2 3 4 5 6 |
from math import log # Natural logarithm (base e ≈ 2.718) print(log(10)) # Output: 2.302585092994046 |
If no base is provided, Python defaults to base e, known as Euler’s number, common in scientific and statistical calculations.
Key Properties of Logarithms
Understanding how logarithms behave is important for simplifying expressions.
Multiplication (Product Rule) $x^m \cdot x^n = x^{m+n}$ $\log_b(ab) = \log_b(a) + \log_b(b)$
Division (Quotient Rule) $\frac{x^m}{x^n} = x^{m-n}$ $\log_b\left(\frac{a}{b}\right) = \log_b(a) – \log_b(b)$
Power of Power Rule $(x^m)^n = x^{mn}$ $\log_b(a^n) = n \cdot \log_b(a)$
Zero Power Rule $x^0 = 1$ $\log_b(1) = 0$
Negative Exponent (Inverse) $x^{-1} = \frac{1}{x}$ $\log_b\left(\frac{1}{x}\right) = -\log_b(x)$
Using SymPy for Log Simplification
The sympy
library is great for algebraic manipulations.
✅ Example: Algebraic simplification
1 2 3 4 5 6 7 8 |
from sympy import symbols, log, simplify x, y = symbols('x y') expr = log(x * y) simplified = simplify(expr) print(simplified) # Output: log(x) + log(y) |
Bonus: Logarithms with Irrational Exponents
What if you wanted to compute something like 8^\pi \approx 687.2913?
1 2 3 4 5 6 7 8 9 |
import math # Approximate value of pi pi = math.pi result = 8 ** pi print(result) # Output: ≈ 687.2913 |
Internally, Python uses rational approximations for irrational numbers to compute results like this.
Logarithms Undo Exponents
Logarithms are the inverse of exponentiation.
For example:
1 2 3 4 5 6 |
# If 2^x = 8 # Then x = log2(8) print(log(8, 2)) # 3.0 |
Summary
-
Logarithms answer the question: “What power of a base gives a number?”
-
Python uses
math.log()
for both base-e
and base-n
logs. -
The
sympy
library can symbolically simplify log expressions. -
Logarithms obey similar rules as exponents: they simplify multiplication, division, and powers.
Real-Life Applications of Logarithms
-
Earthquake magnitude (Richter scale)
-
Sound decibel levels
-
Machine learning models like logistic regression
-
Data transformation (log-normal distributions)
-
Finance for calculating compound interest
Coming Up Next
In the next post, we’ll explore Euler’s number (e) and how it’s used in natural logarithms and exponential growth in machine learning and data science.