Last modified: May 29, 2026

Define a Variable in Python

Variables are the building blocks of any program. In Python, defining a variable is simple and intuitive. Unlike other languages, you do not need to declare the type. Python uses dynamic typing. This means the variable type is inferred from the value you assign.

To define a variable, just choose a name and use the equals sign. The syntax is: variable_name = value. That is it. There is no keyword like "var" or "let". Python keeps it clean.

For example, to store a number, write:

 
# Define a variable with an integer value
age = 25
print(age)

25

Rules for Variable Names

Python has a few simple rules for naming variables. Follow them to avoid errors. First, names must start with a letter or an underscore. They cannot start with a number. Second, the rest of the name can contain letters, numbers, or underscores. No spaces or special characters like @ or $ are allowed.

Third, variable names are case-sensitive. Name and name are different variables. Fourth, avoid using Python keywords like "if", "for", or "print". These have special meaning.

Here is a good example:

 
# Valid variable names
my_name = "Alice"
student_grade = 92
count1 = 10
_total = 100

And a bad example:

 
# Invalid variable names (will cause errors)
# 2nd_place = "Bob"  # starts with a number
# my-name = "Eve"    # contains a dash
# class = "math"     # 'class' is a reserved keyword

Assigning Multiple Variables

Python allows you to assign values to multiple variables in one line. This is a neat trick. Use commas to separate the variable names and values. The order matters.

Example:

 
# Assign multiple variables in one line
x, y, z = 5, 10, 15
print(x)
print(y)
print(z)

5
10
15

You can also assign the same value to multiple variables. Use the equals sign between them.

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

0
0
0

Understanding Variable Types

Python variables can store different types of data. The common types are integers, floats, strings, and booleans. You can check the type using the type() function. This is useful for debugging.

Example:

 
# Check the type of a variable
name = "John"
age = 30
price = 19.99
is_student = True

print(type(name))
print(type(age))
print(type(price))
print(type(is_student))

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

Notice that Python automatically assigns the type. You do not need to specify it. This is called dynamic typing. It makes code faster to write.

Reassigning Variables

You can change the value of a variable at any time. Just assign a new value. The variable will forget the old value. You can also change the type. Python allows this freely.

Example:

 
# Reassign a variable to a different type
value = 10
print(value)
value = "now a string"
print(value)

10
now a string

This flexibility is powerful. But be careful. Changing types can cause confusion. Use consistent naming to keep your code readable.

Common Mistakes to Avoid

Beginners often make a few mistakes. One is using an undefined variable. If you try to print a variable before assigning it, Python raises a NameError. Always define variables before use.

Another mistake is using reserved keywords. Python has a list of words you cannot use. For example, True, False, and None are reserved. Using them as variable names will cause errors.

Here is an example of a NameError:

 
# This will cause an error
# print(score)  # score is not defined
score = 100
print(score)

100

Always define your variables first. This is a good habit.

Best Practices for Naming

Use descriptive names. A variable like total_price is better than tp. It makes your code self-documenting. Use lowercase letters and underscores. This is called snake_case. It is the Python standard.

Avoid single-letter names except for counters in loops. For example, use i in a for loop. But for important data, use full words. Also, keep names short but clear.

Example of good naming:

 
# Good variable names
user_email = "[email protected]"
order_total = 45.50
is_verified = True

Example of poor naming:

 
# Poor variable names
u = "[email protected]"  # unclear
ot = 45.50               # cryptic
v = True                 # meaningless

Variables in Different Scopes

Variables can be global or local. A global variable is defined outside any function. A local variable is defined inside a function. Local variables only exist inside that function. Global variables can be accessed anywhere.

To modify a global variable inside a function, use the global keyword. Otherwise, Python creates a new local variable with the same name.

Example:

 
# Global and local variables
x = 10  # global variable

def my_function():
    global x
    x = 20  # modifies the global variable

my_function()
print(x)

20

Without the global keyword, the function would create a local x. The global x would stay as 10. Use global variables sparingly. They can make code harder to debug.

Conclusion

Defining a variable in Python is straightforward. Just pick a name and assign a value. Follow the naming rules and best practices. Use descriptive names and avoid reserved keywords. Remember that Python is dynamically typed. This gives you flexibility but also responsibility.

Practice by writing small programs. Try assigning different types and reassigning values. Check the type using type(). Avoid common mistakes like using undefined variables.

If you are familiar with other languages, you might notice similarities. For example, understanding how variables work in JavaScript can help. You can learn more about JavaScript Variables Guide for comparison. Also, see Types of JavaScript Variables for a deeper look at types. Finally, check JavaScript Variables Examples to see real-world use cases.

Keep coding and experimenting. Variables are the first step to mastering Python.