Python Variables: A Beginner’s Guide to Storing Data

Have you ever tried to remember a long number, only to wish you could just give it a simple name like “my_favorite_number”? In programming, we face this problem all the time. We need to store pieces of data—like numbers, text, or true/false values—and be able to recall and use them easily throughout our code.

This is where Python variables come to the rescue. They are one of the first and most important concepts you’ll learn, acting as the basic building blocks for every program you’ll ever write. By the end of this guide, you’ll understand what variables are, how to create them, and how to use them confidently in your own Python programs.

Let’s dive in and demystify this core concept!

What is a Variable in Python?

In simple terms, a variable is a named container that holds a value.

Think of it like a labeled box in your kitchen. You might have a box labeled “Sugar.” You can put 1 cup of sugar in it. Later, you can use that sugar by simply referring to the “Sugar” box, without having to measure out a new cup every time. Even better, if you run out, you can update the box’s contents with a new bag of sugar.

A Python variable works the same way:

  • It has a name (like I or username).
  • It holds a value (like 67 or "Hello World").
  • You can update the value inside it whenever you want.
  • At any given time, a variable can only hold one value.

How to Create Your First Variable

You create a variable and assign it a value using the assignment operator: the equal sign (=).

The syntax is always: variable_name = value

Let’s see it in action. We’ll create a variable named I and assign it the value 67.

I = 67

That’s it! You’ve just created a variable. The Python interpreter sees this command and does two things:

  1. Creates a container named I.
  2. Puts the value 67 inside it.

To see the value inside the variable, you can simply print it.

print(I)
# Output: 67

Working with Variables: The Basics

Updating a Variable’s Value

The name “variable” comes from the fact that the value can vary. To change the value, you simply use the assignment operator (=) again.

I = 67
print(I) # Output: 67

# Now, let's update the value
I = 99
print(I) # Output: 99

The old value (67) is overwritten and replaced with the new value (99). The container I now holds 99.

Using Variables in Expressions

The real power of variables shines when you use them in expressions. Python can use the value stored inside a variable to perform calculations.

I = 9
J = 1

# Let's add the values contained in I and J
print(I + J)
# Output: 10

When Python sees I + J, it looks up the current values in the I and J containers, replaces the names with those values (9 + 1), and then performs the calculation.

You can even store the result of an expression in a new variable.

I = 9
J = 1

K = I + J
print(K)
# Output: 10

In this case, the expression I + J is evaluated first (becoming 10), and then that result is assigned to the new variable K.

Beyond Integers: Different Types of Data

A common question from beginners is, “Can a variable only hold numbers?” The answer is a resounding no! Python variables are dynamic and can hold any type of data.

# An integer
a = 9
# A floating-point number (decimal)
b = 3.14
# A Boolean value (True or False)
c = True
# A String (text)
d = "Hello, PyScribe!"

print(a) # Output: 9
print(b) # Output: 3.14
print(c) # Output: True
print(d) # Output: Hello, PyScribe!

Advanced Variable Operations

Multiple Assignment

Python allows you to assign values to multiple variables in a single line, which can make your code more concise.

Assigning multiple values to multiple variables:

# Assign 10 e, f, g, h = 5, 3.4, True, "message"

print(h) # Output: message
print(e) # Output: 5x, 20 to y, and 30 to z
x, y, z = 10, 20, 30

print(x) # Output: 10
print(y) # Output: 20
print(z) # Output: 30

You can even assign different data types this way:

e, f, g, h = 5, 3.4, True, "message"

print(h) # Output: message
print(e) # Output: 5

Assigning the same value to multiple variables:

# Assign the value 100 to L, M, and N
L = M = N = 100

print(L) # Output: 100
print(M) # Output: 100
print(N) # Output: 100

Dynamic Typing

One of Python’s most loved features is dynamic typing. This means you don’t have to declare a variable’s type in advance. A variable that held a number one moment can hold a string the next.

my_var = 9
print(type(my_var)) # Output: <class 'int'>

# Now, let's assign a Boolean value to the same variable
my_var = False
print(my_var)      # Output: False
print(type(my_var)) # Output: <class 'bool'>

Deleting Variables

When you’re done with a variable and want to free up your computer’s memory, you can delete it using the del keyword.

I = 99
print(I) # Output: 99

del I    # Delete the variable I
print(I) # This will now cause a NameError

After using del I, trying to access I will result in a NameError: name 'I' is not defined because the variable no longer exists.

Rules for Naming Python Variables

To avoid errors, you must follow these rules when naming your variables:

  1. Allowed Characters: Names can contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
  2. Cannot Start with a Number: 9lives is invalid, but lives9 is valid.
  3. No Hyphens Allowed: my-variable is invalid. Use underscores for spaces: my_variable.
  4. Case-Sensitive: ageAge, and AGE are three different variables.
  5. Cannot Use Keywords: Do not use Python’s reserved words (like ifforiswhile)
# Valid names
my_variable = 10
user_name = "Alice"
total_count2 = 45

# Invalid names
2nd_place = "silver"  # Starts with a number
my-variable = 10      # Contains a hyphen
for = "loop"          # Uses a keyword

Ready to Go from Basics to Pro?

You’ve just taken a crucial first step in your Python programming journey by mastering variables. But this is only the beginning. To truly become proficient, you need a structured path that guides you through more complex concepts like functions, loops, and working with data.

Our comprehensive course is designed to take you from absolute beginner to confident Python developer. You’ll get expert instruction, hands-on projects, and a supportive community to help you succeed.

If you’re serious about mastering Python, check out our pre-recorded python comprehensive video course.

Conclusion

In this guide, you’ve learned that a variable is a fundamental container for storing data in Python. You now know how to create them, update them, and use them in expressions. You’ve seen that variables are dynamic and can hold any data type, and you’ve learned the essential rules for naming them correctly. Most importantly, you understand that variables are the key to writing efficient, readable, and powerful code.

Keep practicing by creating your own variables and playing with different operations. The more you code, the more natural it will feel!

Common Questions About Python Variables

Can a variable name have a space in it?
No, variable names cannot contain spaces. Use the underscore character (_) to separate words, for example: user_first_name. This is known as snake_case and is the convention in Python.

What happens to the old value when I assign a new one to a variable?
The old value is overwritten and lost. Python’s memory management will automatically clean up the old value if it’s no longer being used.

How do I know what type of data is in a variable?
You can use the type() function. For example, if my_var = "hello", then type(my_var) would return <class 'str'>, telling you it’s a string.