Last modified: May 29, 2026

What Are Variables in Python

Variables are a core concept in Python. They let you store data. Think of a variable as a labeled box. You put a value inside. Then you use the label to get the value later. Python makes this easy and flexible.

This article explains everything about Python variables. You will learn how to create them. You will see naming rules. We cover data types and best practices. Examples and outputs are included. This guide is perfect for beginners.

What Is a Variable in Python?

A variable is a name that refers to a value. Python does not need a type declaration. You just assign a value. The variable holds that value in memory. You can change it anytime.

Python uses dynamic typing. This means a variable can change its type. For example, you can store a number first. Then store text in the same variable. This is different from languages like Java or C.

Here is a simple example:

# Creating a variable
name = "Alice"
print(name)

# Changing the value
name = 42
print(name)

Alice
42

Notice the variable name first holds a string. Then it holds an integer. Python allows this. It is flexible but needs careful use.

How to Create Variables in Python

Creating a variable is simple. Use the assignment operator =. Put the variable name on the left. Put the value on the right. No special keyword is needed.

Here is the basic syntax:

# Syntax: variable_name = value
age = 25
city = "New York"
pi = 3.14159
is_student = True

You can create multiple variables in one line. This saves space.

# Multiple variables in one line
x, y, z = 1, 2, 3
print(x, y, z)

1 2 3

You can also assign the same value to many variables. This is useful for constants.

# Same value to multiple variables
a = b = c = 0
print(a, b, c)

0 0 0

Variable Naming Rules in Python

Python has strict rules for variable names. Follow them to avoid errors. Here are the key rules:

  • Names must start with a letter or underscore. They cannot start with a number.
  • Only letters, numbers, and underscores are allowed. No spaces or special symbols.
  • Names are case-sensitive. age and Age are different.
  • You cannot use Python keywords. Words like if, for, while are reserved.

Good variable names are clear. Use descriptive words. For example, use student_name not sn.

Here are examples of valid and invalid names:

# Valid names
my_var = 10
_var = 20
myVar2 = 30

# Invalid names (will cause errors)
# 2myvar = 10   # starts with number
# my-var = 20   # hyphen not allowed
# my var = 30   # space not allowed
# if = 40       # keyword

Always use meaningful names. This makes your code readable. Other programmers will thank you.

Data Types and Variables

Python supports many data types. Variables can hold any type. Common types include integers, floats, strings, booleans, lists, and dictionaries.

You can check the type using the type() function. This is helpful for debugging.

# Checking variable types
age = 25
price = 19.99
name = "Bob"
is_active = True

print(type(age))
print(type(price))
print(type(name))
print(type(is_active))

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>

You can change a variable's type by reassigning. This is called type conversion. Python does it automatically when needed.

# Type conversion
num = "100"
print(type(num))

# Convert to integer
num = int(num)
print(type(num))
print(num + 50)

<class 'str'>
<class 'int'>
150

For more on variable types in other contexts, see our JavaScript Variables Guide. Understanding variables across languages helps.

Variable Scope in Python

Scope defines where a variable is accessible. Python has two main scopes: global and local.

A global variable is defined outside any function. It is accessible everywhere. A local variable is inside a function. It only exists there.

Here is an example:

# Global variable
x = 10

def my_function():
    # Local variable
    y = 5
    print("Inside function:", x, y)

my_function()
print("Outside function:", x)
# print(y)  # This would cause an error

Inside function: 10 5
Outside function: 10

Use the global keyword to modify a global variable inside a function. This is rare but useful.

# Using global keyword
count = 0

def increment():
    global count
    count += 1

increment()
increment()
print(count)

2

Be careful with global variables. They can make code hard to debug. Prefer local variables when possible.

Best Practices for Python Variables

Follow these tips to write clean code. They help avoid bugs and improve readability.

  • Use snake_case for variable names. Example: user_age not userAge.
  • Keep names short but descriptive. total_price is better than tp.
  • Avoid using single letters except for loops. i and j are okay for counters.
  • Initialize variables before using them. This prevents reference errors.
  • Use constants with uppercase names. Example: MAX_SIZE = 100.

Here is a good example:

# Good variable naming
student_name = "Alice"
student_age = 20
course_list = ["Math", "Science"]
is_enrolled = True

# Constants
PI = 3.14159
MAX_RETRIES = 5

For more examples, check our JavaScript Variables Examples. The principles are similar across languages.

Common Mistakes with Variables

Beginners often make these errors. Avoid them to save time.

Using undefined variables: Always assign a value first. Using a variable without assignment gives an error.

# Error: undefined variable
# print(my_var)  # NameError

Mixing up variable names: Case matters. Name and name are different.

# Case sensitivity
name = "Alice"
print(Name)  # Error: Name not defined

Using reserved keywords: Do not use words like class, def, or import as variable names.

# Invalid: using keyword
# class = 10  # SyntaxError

These mistakes are easy to fix. Just follow the naming rules.

Variables and Memory

Variables store data in memory. Python manages this automatically. When you assign a value, Python creates an object. The variable references that object.

Multiple variables can reference the same object. This is called aliasing. Changes to one may affect others if the object is mutable.

# Aliasing example
list1 = [1, 2, 3]
list2 = list1  # Both point to same list
list2.append(4)
print(list1)  # Changed too

[1, 2, 3, 4]

For immutable types like integers, this is not an issue. But for mutable types like lists, be careful.

Conclusion

Variables are the foundation of Python programming. They store data and make code dynamic. You learned how to create them. You saw naming rules and data types. Scope and best practices were covered too.

Practice with small programs. Try different variable names. Experiment with types. The more you use variables, the more natural they become. Python's simplicity makes it a great language to start with.

Remember: good variable names lead to good code. Keep learning and coding. Your skills will grow fast. For more on variables in other languages, see our Types of JavaScript Variables article.