Back to Engineering
5 MAY 2026

What Is a Variable? The Building Block Every Programmer Must Know

CA
Compiled by Cypher Async

What Is a Variable? The Building Block Every Programmer Must Know

If programming were cooking, variables would be your bowls and containers — you put ingredients in them, you label them, and you use them whenever you need. Every program ever written, from a simple calculator to a complex web application, relies on variables at its core.


What Exactly Is a Variable?

A variable is a named storage location in your computer's memory that holds a value. The name acts as a label so you can refer to the value later without knowing the exact memory address where it is stored.

Think of it like labeling a box:

  +------------------+
  |   LABEL: "age"   |
  +------------------+
  |   VALUE:  21     |
  +------------------+

When you write age anywhere in your code, the computer knows to look inside that box and use the number 21.


Declaring a Variable

Different programming languages have slightly different syntax, but the core idea is always the same: give a name, and assign a value.

Python

name = "Riya"
age = 20
is_student = True

JavaScript

let name = "Riya";
let age = 20;
let isStudent = true;

Java

String name = "Riya";
int age = 20;
boolean isStudent = true;

Notice that in some languages (like Java) you must also tell the computer what type of data the variable will hold. In others (like Python), the language figures it out on its own.


The Three Parts of a Variable

Every variable has three core parts:

  +-------------+     +------+     +----------+
  |    NAME     |  =  | TYPE |  +  |  VALUE   |
  +-------------+     +------+     +----------+
  |   "score"   |     |  int |     |    95    |
  +-------------+     +------+     +----------+

| Part | What it means | Example | |-------|----------------------------------------|-------------| | Name | The label you use to refer to it | score | | Type | The kind of data it stores | Integer | | Value | The actual data inside the container | 95 |


Common Data Types

Variables can hold different types of data. Here are the most common ones you will encounter as a beginner:

  DATA TYPES
  ├── Integer (int)     → Whole numbers        → 10, -5, 200
  ├── Float             → Decimal numbers      → 3.14, -0.5
  ├── String (str)      → Text                 → "Hello", "Cypher"
  ├── Boolean (bool)    → True or False        → True, False
  └── None / Null       → No value / empty     → None, null

How Variables Work in Memory — A Visual

When you run your program, the operating system allocates a region of memory called the stack (and sometimes the heap) to store your variables. You do not need to manage this directly — the language does it for you.

  MEMORY (simplified view)
  +----------+-----------+----------+
  | Address  |   Label   |  Value   |
  +----------+-----------+----------+
  | 0x001A   |   name    |  "Riya"  |
  | 0x001B   |   age     |    20    |
  | 0x001C   | is_student|   True   |
  +----------+-----------+----------+

You write name → the computer looks up address 0x001A → finds "Riya" → uses it.


Changing (Reassigning) a Variable

The word "variable" comes from "vary" — meaning it can change. You can update the value stored in a variable at any point:

score = 50
print(score)   # Output: 50

score = 80
print(score)   # Output: 80

The box now holds a new value. The old value is gone (unless you stored it somewhere else).


Variable Naming Rules

Every language has naming conventions, but these rules apply almost everywhere:

  GOOD NAMES              BAD NAMES
  ─────────────────────   ──────────────────────
  student_name            1name       (starts with number)
  totalMarks              total marks (has a space)
  isLoggedIn              class       (reserved keyword)
  courseCount             x           (not descriptive)

Best practices:

  • Use descriptive names (studentAge not sa)
  • Start with a letter or underscore, never a number
  • Avoid reserved words (if, for, class, return)
  • Use camelCase (totalMarks) or snake_case (total_marks) consistently

Constants — Variables That Should Not Change

Sometimes you want a value that should never change, like the value of Pi or the number of days in a week. These are called constants.

# Python — convention: ALL_CAPS for constants
PI = 3.14159
MAX_STUDENTS = 30
// JavaScript — use const keyword
const PI = 3.14159;
const MAX_STUDENTS = 30;

Using constants makes your code easier to read and prevents accidental changes.


A Simple Program Using Variables

Here is a small program that uses everything we covered:

# Student report card
student_name = "Arjun"
subject = "Mathematics"
marks_obtained = 87
total_marks = 100

percentage = (marks_obtained / total_marks) * 100

print(student_name + " scored " + str(percentage) + "% in " + subject)
# Output: Arjun scored 87.0% in Mathematics

Flow of the program:

  START
    │
    ▼
  Declare variables (name, subject, marks, total)
    │
    ▼
  Calculate percentage using the variables
    │
    ▼
  Print the result using the variables
    │
    ▼
  END

Common Beginner Mistakes

| Mistake | Why it is wrong | |-------------------------------|---------------------------------------| | Using a variable before declaring it | The computer does not know what it is | | Typo in variable name | naem is not the same as name | | Wrong data type operations | Adding a number to a string directly | | Using a reserved word as name | for = 5 will cause a syntax error |


Summary

  • A variable is a named container that stores a value in memory
  • It has three parts: name, type, and value
  • Common types include integers, floats, strings, and booleans
  • Variables can be reassigned (their value can change)
  • Use constants for values that should never change
  • Always use descriptive, readable names

Variables are step one of your programming journey. Every loop, every function, every program you will ever write depends on them. Get comfortable with them — they are your best tool.


Published by Cypher Async — Agartala's offline coding school, building real engineers one concept at a time.