Sitemap

Python Variables and Identifiers

8 min readSep 12, 2026

--

Press enter or click to view image in full size

In the previous article, we explored the basics of programming and Python. The next step is to understand one of the fundamental building blocks of Python: variables.

Almost every program needs to work with information. It could be a person’s name, an age, a price, a location, a score, or some other value. Instead of repeatedly writing these values throughout a program, Python allows us to assign them to names and use those names whenever they are needed.

This is where variables come in.

What Is a Variable?

A variable is a name that is used to refer to a value.

For example:

age = 24

Here, age is the variable and 24 is the value assigned to it.

Variables can be used to store different kinds of information:

name = "Riya"
city = "Pune"
age = 24

In this example:

  • name refers to "Riya"
  • city refers to "Pune"
  • age refers to 24

These variables can then be used elsewhere in the program:

print(name)
print(city)
print(age)

Output:

Riya
Pune
24

Instead of repeatedly writing the actual values, the variable names can be used whenever those values are required.

This makes code easier to organise, read, and maintain.

Creating Variables in Python

Creating a variable in Python is straightforward. The assignment operator = is used to assign a value to a name.

For example:

score = 85
temperature = 27.5
student_name = "Aarav"

Here, three different variables have been created.

Python determines the type of value based on what is assigned to the variable. For example, 85 is an integer, 27.5 is a floating-point number, and "Aarav" is a string.

The type of a value can be checked using the type() function:

score = 85

print(type(score))

Output:

<class 'int'>

The different data types available in Python will be explored in more detail in the next article.

For now, the important idea is that a variable gives us a name through which we can work with a value.

What Is an Identifier?

Another important term associated with variables is identifier.

An identifier is a name used to identify something in Python. Variable names, function names, class names, and other named elements in a Python program can be identifiers.

For example:

student_name = "Aarav"

Here, student_name is an identifier.

Identifiers allow us to give names to elements in our program so that we can refer to them when needed.

Choosing meaningful identifiers is therefore an important part of writing readable code.

Consider these two examples:

x = 72

and:

exam_score = 72

Both are valid Python code. However, exam_score immediately provides some information about what the value 72 represents.

This becomes increasingly important as programs become larger and more complex.

Rules for Naming Variables

Python has specific rules for creating valid variable names. Understanding these rules helps prevent syntax errors and makes code easier to read.

1. Variable names can contain letters

Letters can be used when creating a variable name.

For example:

name = "Meera"

Variable names can contain uppercase and lowercase letters, although Python treats them differently, which we will look at shortly.

2. Numbers can be used in variable names

Numbers can also be included in a variable name, but the name cannot begin with a number.

For example:

student2 = "Kabir"

is valid.

However:

2student = "Kabir"

is invalid because the variable name begins with a number.

A number can appear later in the name, but not at the beginning.

3. Underscores can be used

Python allows underscores _ in variable names.

For example:

student_name = "Meera"

Underscores are particularly useful when a variable name contains multiple words.

Compare:

studentname = "Meera"

with:

student_name = "Meera"

Both can be valid, but student_name is easier to read because the two words are clearly separated.

This naming style is commonly used in Python.

4. Spaces cannot be used

Spaces are not allowed within variable names.

For example:

student name = "Meera"

is invalid.

If a variable name contains multiple words, an underscore can be used instead:

student_name = "Meera"

5. Variable names cannot start with a number

A variable name must begin with a letter or an underscore, not a number.

For example:

5marks = 90

is invalid.

Instead, we could write:

marks5 = 90

or, more simply:

marks = 90

Python Is Case-Sensitive

One of the important characteristics of Python is that it is case-sensitive.

This means that uppercase and lowercase letters are treated as different.

For example:

age = 20
Age = 30

Python considers age and Age to be two different names.

We can see this by printing both variables:

age = 20
Age = 30

print(age)
print(Age)

Output:

20
30

The only difference between the two variable names is the capital A, but Python treats them as separate variables.

This is why consistency in naming is important. Accidentally changing the capitalization of a variable can result in unexpected behaviour or errors.

Valid and Invalid Variable Names

Let’s bring the naming rules together with a few examples.

Valid variable names

name = "Isha"
age = 21
student_name = "Isha"
marks2 = 88
_total = 500

These follow Python’s basic naming rules.

Invalid variable names

2name = "Isha"
student name = "Isha"

The first begins with a number, while the second contains a space.

Another example is:

student-name = "Isha"

A hyphen should not be used as a separator in a normal Python variable name.

Get Mrunal Shrikumar’s stories in your inbox

Join Medium for free to get updates from this writer.

Instead, write:

student_name = "Isha"

Using an underscore makes the name valid and easier to read.

Why Meaningful Variable Names Matter

Python gives us flexibility when choosing variable names. This means it can be tempting to use short names such as x, y, or a.

For example:

x = 50000

This code is valid, but it doesn’t tell us what 50000 represents.

It could be a salary, a price, a population, or something completely different.

Now consider:

monthly_salary = 50000

The purpose of the value is immediately clearer.

The same applies to other examples.

Instead of:

x = "Delhi"

we can write:

city = "Delhi"

The second example communicates the meaning of the value without requiring additional explanation.

Meaningful variable names become especially useful as programs grow. Clear names make code easier to read, understand, maintain, and modify.

Variables Can Be Reassigned

A variable does not necessarily keep the same value throughout a program.

A variable can be assigned a new value later.

For example:

count = 10
print(count)
count = 25
print(count)

Output:

10
25

Initially, count refers to 10.

Later, 25 is assigned to count.

When the variable is printed again, the newer value is displayed.

This is known as reassignment.

Reassignment is useful because the information being handled by a program often changes while the program is running.

Assigning Multiple Variables

Python also provides a convenient way to assign values to multiple variables in a single statement.

For example:

first_name, age, city = "Anaya", 23, "Nagpur"

Each variable receives its corresponding value.

We can check the values using:

print(first_name)
print(age)
print(city)

Output:

Anaya
23
Nagpur

This is a useful feature of Python’s syntax and can make assignments more concise.

Variables and Values

At this point, it is useful to think of a variable as a name associated with a value.

For example:

price = 450

Here, price is the name used in the program, and 450 is the value associated with it.

When we write:

print(price)

Python uses the variable and displays its value:

450

This relationship between names and values is an important concept in Python.

As we move further into Python, it becomes especially relevant when working with data types, lists, objects, and mutable and immutable values.

A Simple Example

Let’s combine some of the concepts discussed so far:

student_name = "Kavya"
student_age = 22
exam_marks = 91

print(student_name)
print(student_age)
print(exam_marks)

Output:

Kavya
22
91

Each variable has a meaningful name:

  • student_name represents the student's name.
  • student_age represents the student's age.
  • exam_marks represents the marks.

Although this is a simple example, the same principles are used when creating variables in larger programs.

Variables and Memory

When learning about variables, the concepts of memory and references may also appear.

A simple way to visualise a variable is to imagine it as a name connected to a value. However, Python is better understood as using names that refer to objects rather than thinking of variables as physical boxes that contain values.

For example:

number = 100

Here, number is a name associated with the value 100.

This idea becomes more important when working with mutable and immutable objects and data structures such as lists.

For now, the key idea is:

A variable name provides a way to refer to a value in a Python program.

The deeper details of how Python handles objects and references can be explored once the basic concepts are clear.

Common Mistakes When Naming Variables

Understanding the rules is useful, but seeing common mistakes can make them easier to remember.

Starting with a number

1name = "Sam"

❌ Invalid

A variable name cannot begin with a number.

Using spaces

first name = "Sam"

❌ Invalid

Use an underscore instead:

first_name = "Sam"

Forgetting that Python is case-sensitive

Name = "Sam"
name = "Rohan"

These are two different variables.

Choosing unclear names

x = 500

This is valid, but its purpose isn’t obvious.

A clearer alternative would be:

account_balance = 500

The second version makes the meaning of the value much easier to understand.

Key Takeaways

Variables and identifiers are fundamental concepts in Python.

A variable provides a name that can be used to refer to a value, while identifiers are names used to identify elements within a Python program.

The key points to remember are:

  • A variable is a name used to refer to a value.
  • Variables are created using the assignment operator =.
  • Identifiers are names used to identify elements in Python.
  • Variable names can contain letters, numbers, and underscores.
  • A variable name cannot start with a number.
  • Spaces cannot be used in variable names.
  • Python is case-sensitive.
  • Variables can be reassigned to new values.
  • Multiple variables can be assigned in a single statement.
  • Meaningful variable names make code easier to understand.
  • Python variables are best understood as names that refer to values or objects.

These concepts may seem simple, but they form an important foundation for understanding more advanced Python concepts.

What’s Next?

Now that variables and identifiers are understood, the next question is:

What kinds of values can be stored in variables?

A variable can refer to an integer, a decimal number, text, a Boolean value, or several other types of data.

This brings us to the next fundamental concept: Python data types.

The next article will explore the basic data types in Python, including integers, floating-point numbers, strings, Boolean values, None, and complex numbers.

One concept at a time, one step at a time.

--

--