Learn How to Create Variables in Python: Absolute Beginners Tutorial

Variables are the foundation of every Python program. They let you store data, give it a meaningful name, and use it throughout your code. This tutorial walks you through everything you need to know to create, name, and use variables confidently in Python—even if you have never written a single line of code before.

If you are learning Python for the first time, variables are the place to start. Every concept you encounter going forward—functions, loops, conditionals, data structures—depends on your ability to store and retrieve data. In Python, creating a variable takes a single line of code, but the ideas behind that line will follow you throughout your entire programming career.

What Is a Variable in Python?

A variable is a name that refers to a value stored in your computer's memory. Think of it as a label you attach to a piece of data so you can find it again later. When you write age = 25, you are telling Python: store the integer 25 in memory and let me refer to it using the name age.

Unlike languages such as C++ or Java, Python does not require you to declare a variable's type before using it. There is no special keyword needed. A variable is created the moment you assign a value to it using the = operator, which is called the assignment operator.

python
# Creating your first variables
age = 25
name = "Ada"
temperature = 98.6
is_registered = True

print(age)            # 25
print(name)           # Ada
print(temperature)    # 98.6
print(is_registered)  # True

Each line above creates a variable and assigns it a value. The variable age holds an integer, name holds a string, temperature holds a float, and is_registered holds a boolean. Python figures out the type for you behind the scenes.

Note

A common misconception is to think of variables as boxes that hold data. In Python, variables are better understood as name tags. The data lives in memory as an object, and the variable is simply a name pointing to that object. When you reassign a variable, you move the name tag to a different object—you do not overwrite the old one.

Reassigning Variables

You can change the value a variable refers to at any time by assigning it a new value. The old value is not destroyed immediately—it remains in memory until Python's garbage collector removes it (assuming nothing else references it).

python
# Reassigning a variable
color = "blue"
print(color)   # blue

color = "green"
print(color)   # green

Multiple Assignment

Python lets you assign values to several variables in a single line. This can make your code shorter and easier to read when you are initializing related values together.

python
# Assign different values to multiple variables
x, y, z = 10, 20, 30
print(x, y, z)   # 10 20 30

# Assign the same value to multiple variables
a = b = c = 0
print(a, b, c)   # 0 0 0
Pro Tip

When using multiple assignment with different values, the number of variable names on the left must match the number of values on the right. If they do not match, Python will raise a ValueError.

code builder click a token to place it

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

your code will appear here...
"pythonista" var username == = let

Naming Rules and Conventions

Choosing good variable names is one of the easiest ways to make your code readable. Python enforces strict rules about what characters a name can contain, and the community follows additional conventions defined in PEP 8 (the official Python style guide).

The Rules (Enforced by the Interpreter)

A variable name must begin with a letter (a–z, A–Z) or an underscore (_). After the first character, the name can include letters, digits (0–9), and underscores. Spaces, hyphens, and special characters like @ or $ are not allowed. Names are case-sensitive, meaning Score and score are two completely different variables. You also cannot use Python's reserved keywords—words like if, for, class, return, and True—as variable names.

python
# Valid variable names
player_score = 100
_private_value = 42
userName2 = "Ada"

# Invalid variable names (will cause errors)
# 2nd_place = "silver"   # starts with a digit
# user-name = "Ada"      # contains a hyphen
# class = "Python 101"   # uses a reserved keyword

The Conventions (Recommended by PEP 8)

PEP 8 recommends snake_case for variable and function names: all lowercase letters with words separated by underscores. This convention is followed by the vast majority of the Python community and the standard library itself. Descriptive names are strongly preferred over abbreviations—total_price is clearer than tp, and student_count is better than sc.

Example
user_email, total_score, file_path
When to use
Variables, function names, and module names. This is the standard Python convention defined by PEP 8 and used throughout the standard library.
Example
MAX_RETRIES, API_KEY, BASE_URL
When to use
Constants—values that are intended to remain unchanged throughout the program. Python does not enforce immutability, but the convention signals intent to other developers.
Example
PlayerCharacter, HttpResponse, DatabaseConnection
When to use
Class names only. You will encounter this convention when you start working with object-oriented programming in Python. Do not use PascalCase for regular variables.
spot the bug click the line that contains the bug

This code is supposed to store a player's name and print it. One line has a naming error. Find it.

1 game_title = "Dungeon Crawler"
2 player-name = "Kira"
3 max_health = 100
4 print(game_title)
5 print(player-name)
The fix: Change player-name to player_name. Python interprets the hyphen as a minus operator, not as part of the variable name. Variable names can only contain letters, digits, and underscores.

Dynamic Typing and the type() Function

Python uses dynamic typing, which means you never declare a variable's type. The interpreter determines the type at runtime based on the value you assign. You can even reassign a variable to a value of a completely different type, and Python will not complain.

python
# Dynamic typing in action
data = 42
print(type(data))   # <class 'int'>

data = "hello"
print(type(data))   # <class 'str'>

data = [1, 2, 3]
print(type(data))   # <class 'list'>

The built-in type() function is your primary tool for inspecting what kind of data a variable currently holds. It returns the class of the object the variable points to. This is especially useful when debugging, because a variable that holds a string when you expected an integer will cause errors that can be hard to trace without checking the type.

Watch Out

Dynamic typing gives you flexibility, but it also means Python will not warn you if you accidentally reassign a variable to the wrong type. If a function expects an integer and receives a string instead, you will get a TypeError at runtime. Use type() and print() often while learning to verify your assumptions.

Type Casting

Sometimes you need to convert a value from one type to another. Python provides built-in functions for this: int(), float(), str(), and bool(). This process is called type casting.

python
# Type casting examples
age_str = "25"
age_int = int(age_str)       # Convert string to integer
print(age_int + 5)           # 30

price = 19
price_float = float(price)   # Convert integer to float
print(price_float)           # 19.0

count = 7
count_str = str(count)       # Convert integer to string
print("Items: " + count_str) # Items: 7
Variable names in Python are references (labels) that point to objects stored in memory.

How to Create and Use Variables in Python

Follow these four steps to create and use a variable correctly in any Python program.

  1. Choose a descriptive variable name

    Pick a name that describes the data the variable will hold. Use lowercase letters with underscores between words (snake_case), such as player_score or email_address. Avoid single-letter names except in short loops.

  2. Use the assignment operator to store a value

    Write your 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. Verify the variable with print() and type()

    Use print(player_score) to display the stored value and print(type(player_score)) to confirm its data type. This helps you catch mistakes early and understand what kind of data your variable holds.

  4. Reassign and reuse the variable

    To change a variable's value, assign it again: player_score = 10. Python will point the name to the new value. You can even change its type, such as player_score = "ten", because Python is dynamically typed.

"Readability counts." — The Zen of Python, PEP 20

Python Learning Summary Points

  1. Variables in Python are created by assigning a value with the = operator—no type declaration or special keyword is required.
  2. Variable names must start with a letter or underscore, can contain letters, digits, and underscores, and are case-sensitive. PEP 8 recommends snake_case for variables and functions.
  3. Python is dynamically typed, so a variable's type is determined by its assigned value and can change through reassignment. Use type() to inspect and int(), float(), str(), or bool() to cast between types.
  4. Multiple assignment lets you assign values to several variables on one line (x, y = 1, 2) or assign the same value to multiple variables (a = b = 0).
  5. Common beginner mistakes include using hyphens in variable names, starting a name with a digit, confusing = (assignment) with == (comparison), and using a variable before assigning it.

Variables are the starting point of your Python journey. Once you are comfortable creating, naming, and reassigning them, you are ready to move on to working with data types, operators, and control flow. Practice by creating variables for different kinds of data—strings, integers, floats, and booleans—and use print() and type() to verify your work as you go.

check your understanding question 1 of N

║ Each .faq-item needs a question in .faq-trigger and an ║ ║ answer in .faq-answer. Aim for 8–16 questions per article. ║ ║ Phrase questions as "What is...", "How does...", etc. ║ ║ Must match the FAQPage JSON-LD block question-for-question. ║ ╚══════════════════════════════════════════════════════════════╝ -->

Frequently Asked Questions

A variable in Python is a name that refers to a value stored in memory. You create one by writing a name, an equals sign, and a value, like age = 25. Python does not require you to declare a type beforehand.

No. Python is dynamically typed, which means the interpreter determines the type of a variable based on the value you assign to it. You can also reassign a variable to a value of a different type at any time.

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

Use the built-in type() function. For example, type(age) returns <class 'int'> if age holds an integer value. This is useful for debugging and verifying that your variables contain the data types you expect.

Yes. Python supports multiple assignment in two forms: you can assign different values to different variables with x, y, z = 1, 2, 3, or assign the same value to several variables with a = b = c = 0.

When you reassign a variable, Python creates a new object for the new value and points the variable name to that new object. The old object remains in memory until Python's garbage collector removes it if nothing else references it.

Snake_case is the naming convention recommended by PEP 8 for Python variables and functions. Words are written in lowercase and separated by underscores, such as user_name or total_score. This improves readability compared to other naming styles.

A NameError occurs when you try to use a variable that has not been assigned a value yet. This typically happens due to a typo in the variable name, using a variable before its assignment statement, or referencing a variable from a different scope.

The single equals sign (=) is the assignment operator, used to assign a value to a variable. The double equals sign (==) is the comparison operator, used to check whether two values are equal. Confusing the two is a common beginner mistake.

No. A Python variable name cannot start with a digit. For example, 1score is invalid and will produce a SyntaxError. The name must begin with a letter or an underscore, such as score1 or _score.