The word comes from the Latin for “changeable,” and that is the whole idea. A variable is a label attached to something whose value is not fixed. The label stays the same while the value behind it can differ from one situation, person, moment or run of a program to the next. A constant is the opposite: a value that never changes, like the number of days in a week. Many fields also use the term loosely. A programmer may use a variable that never actually changes after it is set, and a researcher may hold a variable constant on purpose. Both are still variables by definition, because they could have varied. Throughout this guide we will follow one running example, hours studied, to show how the same idea looks in each field.
These three terms are easy to mix up. The variable is the characteristic itself, such as hours studied. A value is what that variable equals for one case, such as 3.5 hours for one student. Data is the collection of values you record across many cases. The variable stays the same across a data set, while its values differ from case to case and can change over time for the same case.
They give things names. You cannot calculate, code or test something you have not named. They make testing possible. A hypothesis is a claim about how one variable relates to another. They make work repeatable. When variables are defined clearly, someone else can run the same calculation, program or study. They separate cause from coincidence. Knowing which variable you changed and which you only watched is the foundation of fair conclusions.
| Classification | Type | Quick definition | Example |
|---|---|---|---|
| By data | Discrete | Counted in whole units | Number of exams taken |
| By data | Continuous | Measured on a scale; decimals possible | Hours studied: 2.75 |
| By data | Nominal | Categories with no natural order | College major |
| By data | Ordinal | Ordered categories with unequal gaps | Satisfaction rating from 1 to 5 |
| By role | Independent | Presumed cause | Hours studied |
| By role | Dependent | Measured outcome | Exam score |
| By role | Control | Held constant or adjusted for | Time of day of the exam |
| By role | Confounding | Related to both variables and distorts the link | Motivation |
| By role | Moderating | Changes the strength or direction of an effect | Sleep quality |
| By role | Mediating | Explains how an effect happens | Practice problems completed |
| By role | Covariate | Adjusted for to sharpen the estimate | Prior exam score |
An unknown to solve for. In 3x + 2 = 11, x stands for a number you have to find. Here it takes one specific value, x = 3. An input to a function. In y = 5x, you choose any value of x and the function gives you y. Here x really does vary. A placeholder in a general formula. In the area formula for a rectangle, length and width can be any numbers.
Suppose a study plan starts with 40 points and adds 5 points for every hour studied. The expression is 40 + 5h. Substitute h = 2 and you get 50. Substitute h = 3 and you get 55. Now write the equation 40 + 5h = 65. Only one value of h works: h = 5 (learn the method in solving linear equations).
| Expression | Equation | |
|---|---|---|
| Equals sign | No | Yes |
| Goal | Evaluate or simplify | Solve for the unknown |
| Value of the variable | Any value you substitute | Usually one value, or a few |
| Example | 40 + 5h | 40 + 5h = 65 |
| Area of math | Common letters | What they usually stand for |
|---|---|---|
| Algebra | x, y, n | An unknown number in an expression or equation |
| Geometry | s, r, h, l, w | A measurement such as side length, radius, height, length or width |
| Functions | x, f(x) | An input and the output it produces |
| Statistics | x, y | Data values collected from a group |
No spaces in a name. A name cannot start with a digit. A name cannot be a reserved word such as if, for or class
| Instead of | Problem | Use |
|---|---|---|
my score |
Contains a space | my_score or myScore |
2nd_score |
Starts with a digit | score_2 |
for |
Reserved word | loop_count |
h |
Unclear in a long program | hours_studied |
hours_studied = 3
bonus_hours = 2
total_hours = hours_studied + bonus_hours # 5
+ operator, which is called concatenation: name = 'Sam'
print('Student: ' + name) 'Hours: ' + 3 raises an error, so you convert first with str(3). JavaScript quietly converts for you, which is convenient but can hide mistakes.7 / 2 gives 3. In Python, 7 / 2 gives 3.5.if hours_studied >= 5:
print('Great preparation')
else:
print('Consider studying more') def show_hours():
hours_studied = 3 # local variable
print(hours_studied)
show_hours() # prints 3
print(hours_studied) # NameError: not defined outside the function When a value should never change after it is set, many languages let you declare it as a constant (const in JavaScript, final in Java). This protects you from accidental changes and tells the reader the value is fixed.
The two are related but not identical. In math, a variable is an abstract symbol, and x = x + 1 is impossible to satisfy. In code, x = x + 1 is a valid instruction: take the current value, add one and store the result back. The equals sign means “assign,” not “is equal to.”
The first split asks whether a variable records an amount or a group. The second asks whether it is counted or measured, or whether the groups have an order.
Quantitative variables record amounts: hours studied, weight, income. Qualitative (categorical) variables record group membership: major, blood type, favorite genre.
Discrete variables are counted and take separate values, such as the number of exams taken (0, 1, 2…). Continuous variables are measured and can take any value in a range, including decimals, such as 2.75 hours or 68.4 kg.
Nominal categories have no natural order (eye color, city). Ordinal categories have a meaningful order, but the gaps between them are not necessarily equal (education level, a 1–5 satisfaction rating).
Knowing the type tells you which summaries make sense. Averaging eye colors is meaningless, but averaging hours studied is useful. The type also guides which graph and, later, which statistical test fits.
| Variable type | Typical summaries | Common graphs |
|---|---|---|
| Nominal | Counts, percentages, mode | Bar chart, pie chart |
| Ordinal | Counts, median, mode | Bar chart |
| Discrete | Counts, mean, median | Bar chart, dot plot |
| Continuous | Mean, median, standard deviation | Histogram, box plot, scatterplot |
+ operator, which is called concatenation: | Independent variable | Dependent variable | |
|---|---|---|
| Role | Presumed cause | Presumed effect |
| In an experiment | Set or assigned by the researcher | Measured, not controlled |
| Also called | Predictor, explanatory, treatment | Outcome, response |
| Graph axis | x (horizontal) | y (vertical) |
| Example | Hours studied | Exam score |
A memory aid: the dependent variable depends on the other one.
The variable you vary or group by goes on the x-axis. The outcome you measure goes on the y-axis.
A moderator changes the strength or direction of the relationship. Perhaps studying helps more for students who sleep well. Sleep quality moderates the effect. A mediator explains how the effect happens. Perhaps studying raises scores because it increases the number of practice problems completed. Practice problems mediate the effect.
Mediator: explains how the effect happens
Moderator: changes how strong the effect is
A mediator sits inside the path between cause and effect. A moderator sits outside it and changes how strong the path is.
| Context | What a variable is | “Hours studied” looks like |
|---|---|---|
| Math | A letter standing for a number | h in points = 5h |
| Programming | A named storage spot for a value | hours_studied = 3 |
| Statistics | A characteristic recorded for each case | A continuous, ratio-scale quantitative variable |
| Research | A factor with a role in the study design | The independent variable (exam score is the dependent variable) |
| Abstract concept | Operational definition |
|---|---|
| Study effort | Hours of study logged in a diary per week |
| Reading ability | Words read correctly per minute from a standard passage |
| Physical activity | Average daily step count from a wearable over seven days |
| Customer satisfaction | Score on a 1–5 post-purchase survey question |
| Math anxiety | Total on a validated math-anxiety questionnaire |
| Field | Independent | Dependent | Control |
|---|---|---|---|
| Education | Tutoring format (group vs one-on-one) | Quiz score | Prior grades |
| Psychology | Amount of sleep | Reaction time | Time of day |
| Medicine | Drug dose | Blood pressure | Age and weight |
| Agriculture | Fertilizer type | Crop yield | Soil type, water |
| Marketing | Email subject line | Open rate | Send time |
| Computer science | Sorting algorithm | Run time | Hardware, input size |
| Mistake | Why it is a problem | How to avoid it |
|---|---|---|
| Treating correlation as causation | Two variables moving together does not show one causes the other | Use a controlled design or careful causal methods |
| Choosing variables you cannot measure | The study cannot be run or replicated | Operationalize each concept first |
| Ignoring confounders | Results may reflect a hidden third factor | Review the literature, use controls or statistical adjustment |
| Including too many variables | Complexity grows and results get harder to trust | Keep only variables tied to the question |
| Vague definitions written after data collection | Measurements become inconsistent | Define and pilot test beforehand |
| Wrong variable type for the analysis | A test built for one data type misleads on another | Identify type (nominal, ordinal, discrete, continuous) first |
| Confusing math and code meanings | x = x + 1 looks wrong to a math reader |
Remember that = means assignment in code |
| Using a variable before creating it | The program stops with an error such as NameError in Python | Assign a value before the first use |
| Mixing up letter case in names | Score and score are different variables in most languages |
Pick one naming style and stick to it |
| Averaging a categorical variable | The result has no meaning | Use counts and percentages instead |
| Treating a control group as a control variable | The two ideas do different jobs | A group is people; a variable is a factor held steady |
A teacher wants to know whether students who use flashcards for 20 minutes a day score higher on a vocabulary test than students who do not. All students are in the same class and take the same test on the same day. Name the independent variable, the dependent variable, the control variables and one possible confounder.
1. In the equation 3x + 2 = 11, how many values can x take?
2. In the expression 3x + 2, how many values can x take?
3. Is “number of pets in a household” discrete or continuous?
4. Which of these is ordinal: eye color, shirt size (S, M, L, XL) or height in centimeters?
5. Which is a valid variable name in Python: 2nd_score, my score or score_2?
score_2. The others start with a digit or contain a space.
6. Sleep quality changes how strongly study time affects exam scores. What role does sleep quality play?