Python online course

What Are Variables in Python?

Learn how Python variables bind names to values, how to create, read, print, and reassign them, and how to choose valid variable names.

A variable is a name used to refer to a value while a program runs. A value is data that a program works with, such as a number or text. Variables let you give useful names to data so you can reuse, inspect, and update it.

A simple container analogy can help: you might label a container x and associate it with the number 3. Python is more precise than a physical container, however. A variable name is connected to a Python object, the entity that represents a value. This connection is called a binding.

Creating a Variable with Assignment

Python creates a variable when you first assign a value to its name. The basic pattern is:

variable_name = value

The equals sign is the assignment operator when it is used this way. Assignment binds the name on the left to the value or object on the right.

x = 3

Conceptually, Python makes the value 3 available, establishes the name x, and binds x to that value. You can then use x to access the value.

Names, Values, Objects, and Binding

These terms describe different parts of a variable:

  • Variable name: the identifier you write in code to access a variable's value, such as x or student_name.
  • Value: the data, such as 3 or 'Mina'.
  • Object: the Python entity representing a value. Variables refer to objects.
  • Binding: the association between a name and an object.

Thinking in terms of names and objects explains why a variable is not necessarily a fixed physical storage box. A name can be bound to one object and later bound to another object.

Using a Variable

Use a variable name wherever Python expects a value. The built-in print() function displays values:

x = 3
print(x)

Output:

3

When Python evaluates print(x), it looks up the name x, obtains the value to which it refers, and passes that value to print().

You can try the same statements in an interactive Python prompt:

>>> x = 3
>>> print(x)
3

In a script, write the statements without the >>> prompt characters. For help with interactive execution, see Python's interactive prompt and how to run Python code.

Variable Names Versus String Literals

A name without quotation marks is interpreted as a variable reference when it is used in an expression. Text inside quotation marks is a string literal. A string is text data, and the quoted text is used exactly as written.

x = 3
print(x)
print('x')

Output:

3
x

print(x) evaluates the variable named x and displays its value. print('x') does not look up a variable; it displays the one-letter string x.

CodeHow Python interprets itOutput
print(x) after x = 3Evaluate the variable reference x.3
print('x')Display the string literal containing the letter x.x

Reassigning a Variable

Reassignment means assigning a new value to a name that already exists. Later assignment changes what that name refers to.

x = 3
print(x)
x = 5
print(x)

Output:

3
5

The first print(x) runs before reassignment, so it displays 3. After x = 5, the current object associated with x is the value 5, so the second call displays 5.

Choosing Valid Variable Names

Good variable names make code easier to read. Prefer clear, descriptive names that explain the data they refer to. Python examples commonly use lowercase snake_case: words are lowercase and separated by underscores.

student_name = 'Mina'
print(student_name)

Output:

Mina

A variable name is an identifier, a valid name for a variable, function, class, or another Python construct. Core identifier rules include:

  • Use letters, digits, and underscores.
  • Do not begin a name with a digit.
  • Do not include spaces.
  • Do not use a Python keyword, such as class, as a variable name.
  • Use descriptive lowercase snake_case names for ordinary variables.
NameValid?Reason
totalYesUses a clear word and valid characters.
student_nameYesUses lowercase snake_case.
score2YesDigits are allowed when the name does not begin with one.
2scoreNoA variable name cannot begin with a digit.
student nameNoSpaces are not allowed in an identifier.
classNoclass is a Python keyword.

Common Problems

Quoted variable names print letters instead of values

If a letter is printed instead of the value you assigned, you probably placed the variable name in quotation marks.

x = 3
print('x')

Use print(x) when you want Python to evaluate the variable and display 3.

Using a name before assignment

Python reports that a name is not defined when you reference it before assigning it, or when you misspell it.

print(x)
x = 3

Assign the variable first and use the same spelling consistently:

x = 3
print(x)

Invalid identifier syntax

This statement causes a syntax error because the name begins with a digit:

2score = 10

Use a valid alternative such as:

score2 = 10

A newer value is displayed

In this example, the later assignment is why print(x) displays 5:

x = 3
x = 5
print(x)

Python uses the value currently associated with x. If you need the earlier value, print it before reassigning the variable or bind it to another name.

Summary

  • A variable is a name used to refer to a value during program execution.
  • Python assignment uses the = operator: name = value.
  • Assignment creates a binding between a name and a Python object.
  • Use an unquoted variable name to access its value and print() to display it.
  • Quoted text is a string literal, so print('x') prints x rather than the value of x.
  • Reassignment binds an existing name to a new value.
  • Use valid, descriptive names such as student_name.

Next, explore variable data types, Python variable names, and strings in Python.