programming · level 1

Variables & Assignment

Names pointed at values.

100 XP

Variables & Assignment

A variable is a name pointed at a value. The most fundamental operation in any program is binding a name to a value so you can refer to it later.

The mental model

When you write:

x = 5

You're not saying "x is 5 forever". You're saying "right now, the name x refers to the value 5." Tomorrow x could refer to a string. The next line could rebind x to something else entirely:

x = 5
x = "hello"
x = [1, 2, 3]

In a dynamically-typed language (Python, JavaScript, Ruby), the variable doesn't have a type — only the value does. The name is just a label.

In a statically-typed language (Go, Rust, TypeScript, Java), the variable usually keeps a single type for its whole lifetime. The compiler enforces it:

var x int = 5
x = "hello"   // compile error: cannot use "hello" (string) as int

Assignment is not equality

= in code does not mean "is equal to". It means "store this value in this name". The equality operator is usually == (or === in JavaScript). Mixing them up is the most common bug for first-time programmers:

if x = 5:   # syntax error — you meant ==
    ...

Most modern languages catch this at parse time. C and a few others let you write it and produce a "useful" warning (if (x = 5) always-true) instead.

Multiple assignment

Many languages let you assign several names at once:

a, b, c = 1, 2, 3

This is a single statement, not three. The right-hand side is evaluated first (producing a tuple (1, 2, 3)), then unpacked into the names on the left. That means swap is a one-liner:

a, b = b, a

Without multiple assignment you'd need a temporary:

tmp = a
a = b
b = tmp

Augmented assignment

x += 1 is shorthand for x = x + 1. It exists for every arithmetic operator: -=, *=, /=, %=, **=. Most languages have it; some (like Go) require the long form.

Naming

The single most important thing about a variable name is that it tells the next reader what the value means. n is fine for "the number we're iterating", terrible for "the user's account balance". Conventions:

  • snake_case — Python, Ruby, Rust
  • camelCase — JavaScript, Java, TypeScript
  • PascalCase — types in most languages, all identifiers in some

Consistency inside a project beats personal preference.

Constants

A constant is a variable that's never reassigned. Some languages enforce this with a keyword:

const PI = 3.14159;
PI = 4;  // TypeError: Assignment to constant variable.

Python doesn't have a const keyword, but the convention is ALL_CAPS for module-level constants — a signal to future readers, not the language.

What makes a good variable name

Three quick tests:

  1. Could the name be wrong? (data could be anything; users_who_signed_up_today could not be wrong.)
  2. Will a reader who's never seen this code understand what it holds? If you have to explain it in a comment, the name needs work.
  3. Is the name proportional to its scope? A loop counter named i is fine — its scope is one screen. A module-level config object should not be called c.