Previous Post
Python Introduction FAQ: Answers to the Most Common Questions for Beginners
Introduction
Variables are the building blocks of any programming language. In Python, they’re incredibly flexible and easy to use. Let’s dive into what they are, how they work, and how to use them effectively.
What is a variable in Python?
In real life, we store items in different boxes and label them with names to remember what’s inside. Similarly, in Python, we store values and assign names to them. These names are called variables, and each variable holds a value. Example:
# Simple variable examples
name = "Python"
age = 25
height = 5.8
is_student = True
print(name) # Output: Python
print(age) # Output: 25In many other programming languages, we need specific commands to declare or assign variables. In Python, however, a variable is created as soon as we assign a value to it. The equal sign (=) is used for this assignment.
Python is a dynamically typed language, which means we don’t need to declare the type of a variable beforehand. A variable can hold numbers, text, lists, or even more complex objects, and its type can change while the program runs. This flexibility makes Python both simple and powerful for beginners as well as experienced programmers.
Overall, understanding variables is fundamental to programming in Python, as they serve as the building blocks for storing and managing information in any program.
Variable Creation and Assignment
In Python, you create a variable the moment you assign a value to it using the assignment operator (=). You don’t need a separate declaration step or to specify the data type beforehand—Python handles that dynamically!
Let’s look at some examples to better understand variables:
num = 100
name = 'Python'Here, num is a variable that holds the number 100, while name stores the text Python. You can think of variables as labeled boxes in Python’s memory—whatever value you put inside a box, you can later use again by calling the box’s name.
Now let’s see another example:
data1 = 'James'
data2 = 'Alan'
data3 = 'Max'
print(data1)Output:
JamesHere, we created three different variables, but we only print the value of data1, so the output is James
Now, let’s see what happens if we update the value of a variable:
data1 = 'James'
data1 = 'Python'
print(data1)Output:
PythonWe used the same variable name data1, but assigned a new value Python. When we print data1, Python shows the latest value, Python.
Python reads code from top to bottom and left to right, so the most recent assignment is always the one that gets stored. For example:
data1 = 'James'
data1 = 'Java'
print(data1)
data1 = 'Python'
print(data1)Output:
Java
PythonThe first print(data1) outputs Java. The second print(data1) outputs Python.
This shows that a variable in Python always holds its most recently assigned value.
How Variables Work in Python (Behind the Scenes)
When you assign a value to a variable, Python creates an object in memory and the variable name becomes a reference (a tag) pointing to that object.
Example:
a = 5
b = aBoth a and b now point to the same object in memory (5).
If you change a, it doesn’t affect b if a new object is created.
a = 10
print(b) # Output: 5That’s because integers are immutable — when you reassign, Python creates a new object.
Variables Refer to Memory Locations
When we create a variable in a programming language, we are essentially assigning a name to a specific location in the computer’s memory. The variable name is just the label on the box, while the memory location is the actual box that stores the value. For example: text1 = 'Hello' Here text1 is the label (variable name) and the memory location (box) stores the value Hello.
So, when we assign a value to a variable, we are putting that value into the box. When we use the variable in our code, we are actually referring to the value stored in that box. The variable itself doesn’t directly hold the data—it simply points to (or refers to) the memory location where the data is stored. For example:
text1 = 'Hello'Here, text1 refers to the memory location where Hello is stored. The variable doesn’t store the string itself; it’s just a reference to that data.
Variable Can Consist Different Kind of Values
Every variable in Python has a data type — it defines what kind of value the variable holds.
Common types include:
int– Integer numbersfloat– Decimal numbersstr– Text (string)bool– True/False valueslist,tuple,set,dict– Collection types
Example:
num = 10 # int
price = 9.99 # float
name = "Alice" # str
is_active = True # bool
fruits = ["apple", "banana", "cherry"] # listTo check the type of a variable, use the built-in type() function:
num = 100
print(type(num))Output:
<class 'int'>Multiple Assignment in Python
Python allows assigning values to multiple variables in a single line.
Example:
x, y, z = 10, 20, 30
print(x, y, z) # Output: 10 20 30You can also assign the same value to multiple variables:
a = b = c = 50
print(a, b, c) # Output: 50 50 50Checking the Memory Address of a Variable
Whenever we create a variable, Python assigns it a memory address. This address tells us where the object is stored in memory. Python provides a built-in function id() to check the memory address (also called the identity) of any object.
Example: Checking the memory address of a string
text1 = 'Hello'
print(id(text1))Output:
2763130778560Here, text1 is a variable, and id(text1) shows the memory address of the string Hello.
Variable Scope: Local vs Global
Variables in Python have different scopes depending on where they are created.
Local Variable
Defined inside a function — only accessible within that function.
def my_function():
x = 10
print(x)
my_function()
# print(x) # Error: x not defined outside functionGlobal Variable
Defined outside any function — accessible throughout the file.
x = 50
def show():
print(x)
show()
print(x)If you want to modify a global variable inside a function, use the global keyword:
x = 100
def change():
global x
x = 200
change()
print(x) # Output: 200Variable Naming Rules and Conventions
Python has specific rules for naming variables:
- Variable names must start with a letter or underscore (
_). - They cannot start with a number.
- They can only contain letters, numbers, and underscores.
- Names are case-sensitive (
nameandNameare different).
Valid examples:
age = 25
_name = "Python"
user1 = "James"Invalid examples:
1user = "Invalid" # starts with a number
user-name = "Error" # hyphen not allowedSome other examples:
# Valid variable names
username = "john_doe"
user_name = "john_doe" # Snake case (recommended)
userName = "john_doe" # Camel case
_user_id = 123 # Starting with underscore
name2 = "second_name" # Contains numbers
# Invalid variable names (will cause errors)
2name = "invalid" # Cannot start with number
user-name = "invalid" # Cannot contain hyphens
class = "invalid" # Cannot use keywordsConventions (PEP 8 Style):
- Use
snake_casefor variable names. - Use UPPER_CASE for constants.
- Avoid single-letter names unless in loops (
i,j).
Example:
total_amount = 500
PI = 3.14159How to Delete a Variable in Python?
In Python, we don’t usually need to delete variables manually, because the garbage collector automatically frees memory when variables are no longer used. However, if we want to delete a variable before that, we can use the del keyword. For example:
text1 = 'Hello'
print(text1)
del text1
print(text1)Output
Hello
NameError: name 'text1' is not defined.First, we create a variable text1 and store the value Hello. When we use del text1, the variable is deleted from memory. If we try to access text1 after deleting it, Python raises a NameError, because the variable no longer exists.
So we can use del when necessary; otherwise, most of the time, Python manages memory automatically.
Conclusion
Understanding variables is one of the first and most important steps in learning Python. They allow you to store and manage data easily, helping you build logic and structure in your programs. With Python’s simple syntax and dynamic typing, creating and using variables feels natural and beginner-friendly.
As you continue learning Python, remember that variables are used everywhere — from basic scripts to complex projects. Using clear names, following proper conventions, and understanding how variables behave will make your code cleaner, easier to read, and more efficient.
Next Post
Python Variables: Naming Rules and Conventions Explained
Suggested Posts
1. Python Variables: Naming Rules and Conventions Explained
2. Python Variables: Assigning Multiple Values And Unpacking Collections
3. Python Variables: The Complete Frequently Asked Questions (FAQ) Guide

I really appreciate how this guide explains Python variables with friendly examples and practical naming tips. I’ll start applying these tips to keep my code readable and tidy.