Learn What Variables Are in Python: Absolute Beginners Tutorial

Variables are the starting point for every Python program you will write. They give you a way to store data, label it with a meaningful name, and reference it whenever you need it. This tutorial walks you through everything an absolute beginner needs to know about Python variables, from creating your first one to understanding naming rules, dynamic typing, and common mistakes to avoid.

When you first open a Python file or the interactive interpreter, one of the earliest things you will do is store a piece of information so you can use it later. That is exactly what a variable does. Whether you are saving a player's score, a user's name, or the result of a calculation, variables are the mechanism that makes it possible. By the end of this tutorial, you will know how to create them, name them properly, and understand the rules Python enforces behind the scenes.

What Is a Variable in Python?

A variable is a name that points to a value stored somewhere in your computer's memory. Think of it as a label you attach to a piece of data. The data sits in memory, and the variable gives you a way to access it by name instead of by some arbitrary memory address.

In Python, you create a variable by writing a name, then the assignment operator (=), then the value you want to store. There is no separate declaration step, and you never need to specify a data type. The moment you write the assignment, the variable exists.

python
player_name = "Ada"
score = 100
is_active = True

print(player_name)   # Ada
print(score)         # 100
print(is_active)     # True

In the example above, three variables are created in three lines. player_name holds a string, score holds an integer, and is_active holds a boolean. Python figured out the type of each variable on its own based on the value you assigned.

Note

A common beginner misconception is to think of variables as boxes that contain data. A more accurate mental model in Python is to think of them as name tags. The data exists as an object in memory, and the variable is a label you stick onto that object. You can move the label to a different object at any time.

You can reassign a variable to a new value whenever you want. When you do, the name simply stops pointing to the old value and starts pointing to the new one.

python
score = 100
print(score)   # 100

score = 250
print(score)   # 250

Python also supports multiple assignment, where you create several variables in a single line. You can assign different values to different names, or assign one value to several names at once.

python
# Assign different values
x, y, z = 10, 20, 30

# Assign the same value
a = b = c = 0
code builder click a token to place it

Build a valid Python statement that creates a variable called greeting and assigns it the string "hello":

your code will appear here...
== "hello" greeting var = hello

Naming Rules and Conventions

Python enforces strict rules about what you can and cannot name a variable. The interpreter will raise a SyntaxError if you break these rules, and your program will not run. Beyond the enforced rules, the Python community follows naming conventions described in PEP 8, the official style guide. Following these conventions makes your code easier for others to read.

The enforced rules are straightforward. A variable name must start with a letter (a through z, uppercase or lowercase) or an underscore. After the first character, the name can include letters, digits (0 through 9), and underscores. Names are case-sensitive, which means Score and score are two entirely different variables. You cannot use Python reserved keywords like if, for, class, or return as variable names.

Valid
name, _private, _count, Score
Invalid
1name, 2nd_place, 99bottles
Valid
player_score, item2, total_count_3
Invalid
player-score, item@2, total.count
Case-sensitive
age and Age are two different variables
Reserved keywords
if, for, while, class, return, def, import, True, False, None
Pro Tip

Use snake_case for variable names. This means all lowercase letters with words separated by underscores: player_health, total_items, file_path. This is the convention recommended by PEP 8 and used throughout the Python standard library.

spot the bug click the line that contains the bug

This code tries to create three variables and print them. One line breaks a naming rule. Click the line you think is wrong, then hit check.

1 city = "Houston"
2 2nd_place = "silver"
3 score = 95
4 print(city, score)
The fix: Variable names cannot start with a digit. Rename 2nd_place to something like second_place. Python raises a SyntaxError when it encounters a name that begins with a number because the interpreter reads leading digits as the start of a numeric literal, not an identifier.

Dynamic Typing and Data Types

Python is a dynamically typed language. This means you never write a type declaration when creating a variable. The interpreter looks at the value on the right side of the equals sign and determines the type automatically. If you assign the number 42, Python knows it is an integer. If you assign "hello", it knows it is a string.

Because of dynamic typing, a single variable can hold different types at different points in your program. You might assign an integer to result on one line and then assign a string to the same name on the next line. Python does not complain. The variable simply starts pointing to the new object.

python
result = 42
print(type(result))   # <class 'int'>

result = "finished"
print(type(result))   # <class 'str'>

result = 3.14
print(type(result))   # <class 'float'>

The built-in type() function is your go-to tool for checking what type a variable currently holds. As a beginner, you will encounter these common types: int for whole numbers, float for decimal numbers, str for text, bool for True/False values, and list for ordered collections.

Common Mistake

If you try to use a variable before assigning it, Python raises a NameError. Unlike some languages that give uninitialized variables a default value, Python requires every variable to have a value before it can be referenced.

Understanding how the type() function works and recognizing the common data types will help you debug errors early. A frequent beginner mistake is trying to concatenate a string and an integer with the + operator, which raises a TypeError. Checking types with type() helps you catch these issues quickly.

variable memory match round 1 of 4

Click cards to find matching pairs. Match each Python expression with its result or description.

All rounds complete

You matched every pair across all four rounds. These connections between Python expressions and their results are the foundation of working with variables.

How to Create and Use Variables in Python

Follow these three steps any time you need to create and work with a variable in Python. No imports, no special syntax, and no type declarations are required.

  1. Choose a descriptive name

    Pick a name that describes what the variable holds. Use lowercase letters and underscores to separate words, following the snake_case convention. Avoid starting with a number or using Python reserved keywords.

  2. Use the assignment operator to store a value

    Write the variable name, followed by a single equals sign (=), followed by the value you want to store. For example: player_score = 0. Python creates the variable and assigns the value in one step with no type declaration needed.

  3. Use and update the variable in your code

    Reference the variable by name anywhere after the assignment. You can print it, pass it to functions, use it in calculations, or reassign it to a new value at any time. Use the type() function to check what type of data a variable currently holds.

Python Learning Summary Points

  1. A variable in Python is a name that references a value in memory. You create it with the assignment operator (=) and no type declaration is needed.
  2. Variable names must start with a letter or underscore, can contain letters, digits, and underscores, are case-sensitive, and must not be reserved keywords. Follow the snake_case convention from PEP 8.
  3. Python uses dynamic typing, so a variable's type is determined by the value assigned to it and can change through reassignment. Use type() to inspect a variable's current type.
  4. You can assign multiple variables in a single line, reassign variables freely, and use the print() function to display their values during development and debugging.
  5. Attempting to use a variable before it has been assigned raises a NameError. Always assign before you reference.

Variables are the building blocks of every Python program. Once you are comfortable creating them, naming them, and understanding how Python determines their type, you have a strong foundation for learning functions, conditionals, loops, and data structures. Practice by creating variables of different types, printing them, checking their types with type(), and reassigning them to see how Python handles the change.

check your understanding question 1 of 5

Frequently Asked Questions

A variable in Python is a name that refers to a value stored in your computer's memory. You create one by writing a name, followed by the equals sign, followed by the value you want to store. Python does not require you to declare a type before using a variable.

No. Python uses dynamic typing, which means the interpreter determines the type of a variable based on the value you assign to it. You can even reassign a variable to a completely different type later in your code.

Variable names must start with a letter (a-z, A-Z) or an underscore. After the first character, names can contain letters, digits (0-9), and underscores. Names are case-sensitive, and you cannot use Python reserved keywords like if, for, or class as variable names.

A value is a piece of data stored in memory, such as the number 42 or the string "hello". A variable is a name tag attached to that value. The variable does not contain the data itself; it references the location in memory where the data lives.

Yes. Because Python is dynamically typed, you can assign an integer to a variable and then reassign a string to the same variable on the next line. The variable simply points to the new value and the type changes accordingly.

Snake case is the Python convention for naming variables where each word is lowercase and separated by underscores, such as user_age or total_score. This convention is recommended by PEP 8, the official Python style guide.

Python raises a NameError. Unlike some languages that give variables a default value, Python requires every variable to be assigned a value before you can reference it in your code.

Use the built-in type() function. For example, type(my_var) returns the class of the value that my_var currently references, such as <class 'int'> or <class 'str'>.

Yes. Python supports multiple assignment in a single line. You can write x, y, z = 1, 2, 3 to assign different values, or a = b = c = 0 to assign the same value to several variables at once.

Scope determines where in your code a variable is accessible. A variable created inside a function has local scope and can only be used within that function. A variable created outside all functions has global scope and is accessible throughout the module.