Back to Blog
Python

Python Variables: Your Friendly Guide to Labeling Data

9/9/2025
5 min read
Python Variables: Your Friendly Guide to Labeling Data

New to Python? Learn what variables are in a simple, human way. This friendly guide explains how to use them to store and manage your data, with clear examples and zero jargon.

Python Variables: Your Friendly Guide to Labeling Data

Python Variables: Your Friendly Guide to Labeling Data


Python Variables: Your Friendly Guide to Labeling Data

Ever walked into a massive warehouse looking for one specific box? It would be impossible without those little labels telling you what’s inside: “Christmas Decorations,” “Old Photo Albums,” “Important Tax Docs.”

Think of Python variables as those exact same labels, but for your computer’s memory.

Instead of rummaging through a chaotic digital warehouse, you just slap a clear, human-readable name on a piece of data. You tell Python, “Hey, see this number 42? Let’s call it answer from now on.” And just like that, your code becomes easier to write and a million times easier to understand.

Let’s break it down, friend-to-friend.

What Exactly Is a Variable? (The Coffee Mug Analogy)

I like to think of a variable as a coffee mug.

The mug itself has a name written on it—let’s say it’s my favorite mug, labeled my_mug. The mug can hold different things: coffee, tea, water, or even pencils. What it holds is the value.

In Python, this is exactly how it works:

  • The variable name (my_mug) is the label.

  • The value ("coffee") is the content inside.

You can change what's in the mug anytime you want. Yesterday it held "tea", today it holds "coffee". The label my_mug stays the same, but the value inside can change. That’s why they’re called “variables”!

How to Create a Variable: It’s Just an Assignment

In Python, creating a variable is called assignment. You assign a value to a name. And it’s beautifully simple. You just use the equals sign =.

Think of the = not as "equals" but as "is assigned to." You're pointing the label at the value.

python

# Let's assign some values to some labels!
name = "Alice"
age = 30
is_learning = True
favorite_pi = 3.14159

print(name)
print(age)

When you run this, Python will happily show you:

text

Alice
30

You’ve just told Python to remember that:

  • name points to the string "Alice"

  • age points to the number 30

  • is_learning points to the Boolean value True

  • favorite_pi points to the decimal number 3.14159

The best part? You can change a variable's value later in your code, and Python will just go with the flow.

python

age = 30
print("Age last year:", age)

age = 31 # It's your birthday!
print("Age this year:", age)

Rules for Naming Your Variables (The Etiquette)

While Python is flexible, there are a few rules and best practices for naming your variables to keep your code clean and error-free.

The Strict Rules (Non-negotiable):

  1. No Spaces: Use underscores instead: my_variable, not my variable.

  2. No Starting with a Number: 1st_place is invalid. first_place is perfect.

  3. Avoid Special Symbols: Stick to letters, numbers, and underscores. price_$ is bad. price_in_dollars is good.

  4. Don't Use Python Keywords: Words like print, if, for, and while are reserved for Python itself. Don't use them as labels.

The Best Practices (The Polite Thing to Do):The Art of Naming: How to Choose Great Python Variable Names

Let’s be honest. When we first start coding, we all name variables like this:

python

x = 10
n = "Alice"
a = [1, 2, 3]

It’s quick, it’s easy, and the computer doesn’t care. x is just as valid to Python as user_count.

But here’s the secret: code is read far more often than it is written. And the person reading it most is Future You.

Future You will open this file in six months, squint at the screen, and have absolutely no idea what a was supposed to represent. Was it a list of apples? Accounts? Astronomical units?

This is why choosing good variable names isn't just a technicality—it's a core part of writing clean, understandable, and friendly code. It's the difference between leaving a cryptic sticky note and writing a clear instruction manual.

Let's talk about the rules and the art of naming things in Python.

The Hard Rules (The Non-Negotiables)

First, Python has a few strict rules. Break them, and your code will simply not run.

  1. No Spaces: You can't have spaces in a variable name. Use underscores (_) to separate words. This is called snake_case.

    • Yes: first_name, total_cost

    • No: first name, total cost

  2. No Starting with a Number: A variable name can contain numbers, but it can't start with one.

    • Yes: player2_score, item4_price

    • No: 2nd_place, 4th_attempt

  3. Avoid Special Characters: Stick to letters (A-z), numbers (0-9), and underscores (_). Don't use symbols like @, $, or %.

    • Yes: user_id, discount_rate

    • No: user@id, discount%

  4. Don't Use Python's Keywords: Words that Python reserves for its own use (like if, for, while, class, print) are off-limits. Trying to use them will cause major confusion.

The Soft Rules (The Hallmarks of a Pro)

These are the conventions and best practices that separate beginner code from clean, professional code. They are about being kind to your reader (which is often you!).

  • Use snake_case: This is the universal standard for Python variable names (and function names). It's clean and easy to read.

    • number_of_players instead of numberOfPlayers (that's camelCase, common in JavaScript) or numberofplayers (hard to read).

  • Be Descriptive, But Concise: Find the sweet spot between being clear and being overly long.

    • Too Vague: n, data, tmp

    • Too Long: the_number_of_active_users_in_the_database

    • Just Right: active_user_count, username, is_valid

  • Choose Names That Mean Something:

    • Bad: list_1 = [10, 20, 30] (What does this list contain?)

    • Good: item_prices = [10, 20, 30] (Ah, prices!)

  • Use Boolean Names That Ask a True/False Question: A great trick for Boolean (True/False) variables is to prefix them with is_, has_, can_, or should_. This makes their purpose instantly clear.

    • is_logged_in = True

    • has_permission = False

    • can_edit = True

  • A Note on Single Letters: It's okay to use single-letter variables in very specific, short contexts, like loop counters.

    python

    # It's perfectly fine to use 'i' here; its meaning is clear.
    for i in range(10):
        print(i)

    But if your loop gets complex, a more descriptive name might be better (e.g., for index, row in dataframe.iterrows():).

Putting It All Together: A Before and After

Let's see the transformative power of good naming.

The "What on Earth Does This Do?" Version:

python

def p(d):
    t = 0
    for i in d:
        t += i
    a = t / len(d)
    return a

data = [1, 2, 3, 4, 5]
result = p(data)
print(result)

The "Oh, It Calculates an Average!" Version:

python


  • Be Descriptive: user_email is better than e or email_address is better than addr. Your future self will thank you when reading the code in a month.

  • Use Lowercase: The standard is to use lowercase letters with underscores for multi-word names. This is called snake_case. first_name, total_amount, is_valid_user.

  • https://codercrafter.in

Let's Get Practical: A Mini-Project

Let’s use variables to tell a short story. This shows how they make code dynamic and reusable.

python

# Assign some initial values
character = "Sam"
activity = "bake a cake"
number_of_cakes = 2

# Our story
story = f"{character} is going to {activity}. They want to make {number_of_cakes} cakes!"
print(story)

# Let's change the story!
activity = "write a poem"
number_of_cakes = 1 # Decided to make just one

new_story = f"Actually, {character} changed their mind. They will {activity} and only make {number_of_cakes} cake."
print(new_story)

See how we changed the activity and number_of_cakes variables? We didn't have to rewrite the whole sentence structure. The variables act as placeholders, making our code flexible and efficient.

The Art of Naming: How to Choose Great Python Variable Names

Let’s be honest. When we first start coding, we all name variables like this:

python

x = 10
n = "Alice"
a = [1, 2, 3]

It’s quick, it’s easy, and the computer doesn’t care. x is just as valid to Python as user_count.

But here’s the secret: code is read far more often than it is written. And the person reading it most is Future You.

Future You will open this file in six months, squint at the screen, and have absolutely no idea what a was supposed to represent. Was it a list of apples? Accounts? Astronomical units?

This is why choosing good variable names isn't just a technicality—it's a core part of writing clean, understandable, and friendly code. It's the difference between leaving a cryptic sticky note and writing a clear instruction manual.

Let's talk about the rules and the art of naming things in Python.

The Hard Rules (The Non-Negotiables)

First, Python has a few strict rules. Break them, and your code will simply not run.

  1. No Spaces: You can't have spaces in a variable name. Use underscores (_) to separate words. This is called snake_case.

    • Yes: first_name, total_cost

    • No: first name, total cost

  2. No Starting with a Number: A variable name can contain numbers, but it can't start with one.

    • Yes: player2_score, item4_price

    • No: 2nd_place, 4th_attempt

  3. Avoid Special Characters: Stick to letters (A-z), numbers (0-9), and underscores (_). Don't use symbols like @, $, or %.

    • Yes: user_id, discount_rate

    • No: user@id, discount%

  4. Don't Use Python's Keywords: Words that Python reserves for its own use (like if, for, while, class, print) are off-limits. Trying to use them will cause major confusion.

The Soft Rules (The Hallmarks of a Pro)

These are the conventions and best practices that separate beginner code from clean, professional code. They are about being kind to your reader (which is often you!).

  • Use snake_case: This is the universal standard for Python variable names (and function names). It's clean and easy to read.

    • number_of_players instead of numberOfPlayers (that's camelCase, common in JavaScript) or numberofplayers (hard to read).

  • Be Descriptive, But Concise: Find the sweet spot between being clear and being overly long.

    • Too Vague: n, data, tmp

    • Too Long: the_number_of_active_users_in_the_database

    • Just Right: active_user_count, username, is_valid

  • Choose Names That Mean Something:

    • Bad: list_1 = [10, 20, 30] (What does this list contain?)

    • Good: item_prices = [10, 20, 30] (Ah, prices!)

  • Use Boolean Names That Ask a True/False Question: A great trick for Boolean (True/False) variables is to prefix them with is_, has_, can_, or should_. This makes their purpose instantly clear.

    • is_logged_in = True

    • has_permission = False

    • can_edit = True

  • A Note on Single Letters: It's okay to use single-letter variables in very specific, short contexts, like loop counters.

    python

    # It's perfectly fine to use 'i' here; its meaning is clear.
    for i in range(10):
        print(i)

    But if your loop gets complex, a more descriptive name might be better (e.g., for index, row in dataframe.iterrows():).

Putting It All Together: A Before and After

Let's see the transformative power of good naming.

The "What on Earth Does This Do?" Version:

python

def p(d):
    t = 0
    for i in d:
        t += i
    a = t / len(d)
    return a

data = [1, 2, 3, 4, 5]
result = p(data)
print(result)

The "Oh, It Calculates an Average!" Version:

python



Related Articles