What is a Variable?
Welcome to the very first step of your journey from zero to understanding the math behind AI. Today we’re going to talk about one of the most fundamental ideas in all of mathematics: the variable.
Think of a variable like a labeled box. You can put different numbers inside the box, and the label tells you what the box represents. For example, imagine you have a box labeled “my_age”. Today you might put the number 25 inside. Next year, you could take out 25 and put in 26. The label stays the same, but the number inside changes. That’s exactly what a variable is: a symbol (usually a letter like x, y, or n) that stands for a number that can change.
Why Do We Need Variables?
In everyday life, we often talk about quantities that aren’t fixed. For instance:
- “The temperature today is __ degrees.” The blank can be filled with different numbers each day.
- “I have __ dollars in my pocket.” That amount varies.
Variables allow us to talk about these changing quantities without having to pick a specific number. In AI, variables are used everywhere—for example, the input to a model (like the pixel values of an image) is a variable because it changes for every new image.
Constants vs. Variables
A constant is a number that does not change. For example, the number of days in a week (7) is a constant. The speed of light in a vacuum (about 299,792,458 meters per second) is a constant. A constant is a fixed value; it’s like a box that is permanently glued shut with a specific number inside.
A variable, on the other hand, is a symbol that can represent different numbers at different times. For example:
- Let x = the number of hours you study today. x could be 0, 1, 2, or any number.
- Let y = the cost of a cup of coffee. y might be $3.50 today and $4.00 tomorrow.
Here’s a simple way to remember: Constants stay the same; variables can vary.
How to Write and Read Variables
Variables are usually written as letters, often from the end of the alphabet: x, y, z. But they can be any letter (like a, b, n) or even words (like “price” or “age”). When we say “Let x = 5”, we are assigning the value 5 to the variable x. After that, whenever we see x, we can replace it with 5.
Substituting Numbers into Expressions
An expression is a combination of numbers, variables, and operations (like +, -, ×, ÷). For example:
- x + 3
- 2y - 1
- 4z + 7
When we substitute a number into a variable, we replace the variable with that number and then calculate the result. Let’s do it step by step.
Example 1: Evaluate x + 3 when x = 5.
- Replace x with 5: 5 + 3
- Add: 5 + 3 = 8 So, x + 3 = 8 when x = 5.
Example 2: Evaluate 2y - 1 when y = 4.
- Replace y with 4: 2×4 - 1
- Multiply: 8 - 1
- Subtract: 7 So, 2y - 1 = 7 when y = 4.
Example 3: Evaluate 4z + 7 when z = 0.
- Replace z with 0: 4×0 + 7
- Multiply: 0 + 7
- Add: 7 So, 4z + 7 = 7 when z = 0.
Notice that if you change the number you substitute, the result changes. That’s the power of variables: they let you compute many different results from the same expression.
Real-World Analogy: The Parking Meter
Imagine a parking meter that costs $2 per hour. The total cost depends on how many hours you park. Let’s let h represent the number of hours. Then the total cost (in dollars) is 2 × h. Here:
- 2 is a constant (the cost per hour).
- h is a variable (the number of hours, which can be 1, 2, 3, etc.).
- 2 × h is an expression.
If you park for 3 hours, substitute h = 3: 2 × 3 = 6 dollars. If you park for 5 hours, substitute h = 5: 2 × 5 = 10 dollars. The expression stays the same, but the result changes with h.
Variables in AI: A Tiny Glimpse
In AI, variables represent everything from the input data (like the pixels of an image) to the internal parameters of the model (like weights). For example, a very simple AI model might have an expression like:
- prediction = weight × input + bias Here, “input” is a variable (it changes for each new data point), “weight” and “bias” are also variables that the model learns from data. By substituting different input numbers, the model produces different predictions. So understanding variables is the first step to understanding how AI models compute things.
Common Mistakes to Avoid
- Confusing variables with their values: A variable is not the number itself; it’s a placeholder. Saying “x is 5” is fine, but remember that x can be other numbers too.
- Forgetting order of operations: When substituting, always follow the correct order: do multiplication and division before addition and subtraction, unless parentheses say otherwise. For example, in 2y - 1, you multiply 2 by y first, then subtract 1.
- Thinking variables are only letters: Variables can be any symbol, but letters are most common.
Practice (Try These on Your Own)
- Evaluate a + 7 when a = 3.
- Evaluate 5b - 2 when b = 4.
- Evaluate 3c + 6 when c = 0.
- Evaluate 10 - d when d = 10.
(Answers: 1. 10, 2. 18, 3. 6, 4. 0)
Key Takeaways
- A variable is a symbol (like x) that stands for a number that can change.
- A constant is a fixed number that does not change.
- To substitute a number into an expression, replace the variable with that number and calculate.
- Variables are the building blocks of all math in AI, from simple formulas to complex models.
You now have the foundational understanding of variables. In the next level, we’ll see how variables are used in functions—machines that take an input and produce an output. Great job!