When writing programs, you constantly need to work with pieces of information, like numbers, text, or results of calculations. Instead of using the raw data directly everywhere, it's much more practical to give that data a name and refer to it by that name. This is where variables come in. Think of a variable as a labeled container or a named placeholder in your computer's memory where you can store a value.
In Python, a variable is a symbolic name that refers to a value. You use variables to store data so that you can reference and manipulate it later in your program. The term "variable" implies that the value stored can change or vary during the program's execution.
Imagine you're organizing items in boxes. You wouldn't just put things in unmarked boxes; you'd label them ("Winter Clothes", "Books", "Kitchen Utensils"). Variables serve a similar purpose in programming: they provide descriptive names for memory locations that hold data.
Creating a variable in Python is straightforward using the assignment operator, which is the equals sign (=
). The structure is:
variable_name = value
Here's how it works:
=
sign.Let's look at a few examples:
message = "Hello, Python Learner!"
count = 10
price = 49.99
is_active = True
In these examples:
message
is a variable storing a string of text.count
is a variable storing an integer (a whole number).price
is a variable storing a floating-point number (a number with a decimal).is_active
is a variable storing a boolean value (True
).Python is dynamically typed, meaning you don't need to explicitly declare the type of data a variable will hold. Python automatically determines the type based on the value you assign to it. When you assign "Hello, Python Learner!"
to message
, Python understands that message
now holds a string.
Once you've created (or declared and initialized) a variable, you can use its name in your code to access the value it holds.
message = "Welcome"
print(message) # Output: Welcome
items = 5
cost_per_item = 2.50
total_cost = items * cost_per_item # Use variables in calculations
print(total_cost) # Output: 12.5
Using variables makes your code more readable and easier to maintain. If the cost_per_item
changes, you only need to update it in one place (where the variable is assigned), rather than searching for every instance of the number 2.50
in your code.
Choosing good variable names is important for writing clear and understandable code. Python has specific rules and widely accepted conventions for naming variables:
Rules (Must be followed):
_
). They cannot start with a number.myVariable
, myvariable
, and MYVARIABLE
are distinct variables.if
, else
, for
, while
, def
, class
, True
, False
, None
).Conventions (Should be followed for readability):
user_name
instead of un
, total_price
instead of tp
).snake_case
for variable names consisting of multiple words. This means all letters are lowercase, and words are separated by underscores (e.g., first_name
, number_of_items
).l
, O
, or I
as they can be easily confused with numbers 1
and 0
. Short names like x
, y
, or i
are often acceptable in specific contexts like loop counters or mathematical coordinates, but descriptive names are generally preferred.Here are examples of valid and invalid variable names:
name
, age
, user_input
, _internal_counter
, price_2024
2nd_place
(starts with a number), user-name
(contains a hyphen), class
(is a keyword)You can change the value stored in a variable by assigning a new value to it using the assignment operator (=
) again.
# Assign an initial value
score = 100
print("Initial score:", score) # Output: Initial score: 100
# Update the value
score = 150
print("Updated score:", score) # Output: Updated score: 150
# Assign a value based on the current value
score = score + 20 # Read the current value (150), add 20, store the result (170) back
print("Final score:", score) # Output: Final score: 170
Each time you assign a new value, the old value associated with that variable name is replaced.
Variables are fundamental building blocks in Python. They allow you to label and store data, making your programs flexible and easier to understand. As you progress, you'll see variables used constantly to manage information flow within your code.
© 2025 ApX Machine Learning