TracksPractical Coding FoundationsWriting Your First Lines of CodeVariables and Simple Values(3 of 11)

Variables and Simple Values

A variable is a named container for data. Instead of using raw values everywhere, you give them names that explain what they represent. This makes code easier to read, understand, and change.

Creating Variables

To create a variable, choose a name and use the equals sign to assign a value:

name = "Alice"
age = 25

Now name holds the text "Alice" and age holds the number 25. The equals sign here means "store this value" – it's assignment, not a math equation.

Using Variables

Once created, use the variable name anywhere you'd use the value itself:

name = "Alice"
print(name)
print("Hello,", name)

The variable acts as a stand-in for its value. Python looks up what's stored and uses that.

Changing Values

Variables can be reassigned – that's why they're called "variable":

name = "Alice"
print(name)

name = "Bob"
print(name)

After reassignment, the old value is gone. The variable now holds the new value.

Naming Conventions

Good variable names make code self-documenting. Python has conventions:

  • Use lowercase letters: name, not Name
  • Separate words with underscores: first_name, not firstName
  • Be descriptive: user_age is clearer than x
  • Avoid single letters except for simple counters
# Good names
user_name = "Alice"
total_price = 99.99
is_logged_in = True

# Poor names
x = "Alice"
tp = 99.99
flag = True

Assignment vs Equality

New programmers sometimes confuse = (assignment) with == (equality comparison). They're different operations:

x = 5      # Assignment: store 5 in x
x == 5     # Comparison: is x equal to 5? (returns True or False)

You'll learn more about comparisons when you study conditional logic.

Why Variables Matter

Variables let you write flexible, reusable code. Instead of hardcoding values throughout your program, you define them once and reference them by name. Need to change a value? Update it in one place.

They also make your intentions clear. total_price * tax_rate tells a story that 99.99 * 0.08 doesn't.

See More

Further Reading

Last updated December 6, 2025

You need to be signed in to leave a comment and join the discussion