Python Variables
We start with one of the most fundamental concepts in programming: what is a variable?
Think of a box in your house used to store children’s toys. You label that box "toys," so when someone asks where the toys are kept, you can simply say, “in the toys box.”
In computers, the “box” exists in memory. The computer allocates a space in memory and assigns it an internal identifier. When we give that memory location a name, we call it a variable. In some programming languages, a variable can be introduced without immediately storing a value. This step is called a declaration. When memory is allocated and the variable is able to store data (often by assigning a value), this is called a definition.
Python does not separate declaration and definition. A variable is created only when a value is assigned to it. In Python, variables are created the moment they are assigned a value. Python variables have some unique characteristics:
- They do not require a type declaration
- They can change types dynamically after assignment (what we call reassignment)
- The assignment operator (=) links the name on the left to the value on the right
age = 13 # variable definition
age = "thirteen" # variable reassignment (changing the initial value assigned to it)Local & Global Variables:
An important question arises: “Where can I access this variable from?”
This brings us to variable scope, which determines where in a program a variable can be accessed. Understanding scope is essential for writing bug-free and maintainable code.
A variable scope determines where a variable can be accessed in a program. Understanding scope helps us know whether a variable is available throughout the entire program or only in a specific part of it.
In Python, variables generally fall into two categories based on their scope:
- Local variables
- Global variables
Local variables: Created inside a function or passed as arguments to a function. They can only be accessed within that function and do not exist outside of it
def greetings():
message = "Salutations! It is nice having you here" # local variable
print(message)
greetings() # this will print out the value of message
print(message) # This will raise an exception/error => Name not found!In the above example, message exists only inside the greetings function. Trying to access it outside the function results in an error.
Get Joseph Ridge’s stories in your inbox
Join Medium for free to get updates from this writer.
Global variables: Created outside of all functions and can be accessed from anywhere in the program, including inside functions (if referenced).
message = "Salutations! It is nice having you here." # global variable
def greetings():
print(message)
greetings() # this will print out the value of message
print(message) # this will give the value of message hence run Here, the message is accessible both inside the function and in the main program because it is global.
Note:
- A variable is created the moment you assign a value to it
- Variables do not need to be declared with a type
- Variables can change types dynamically after they are set
- Python does not have a separate declaration step; variables exist only when assigned
We will explore these concepts more vividly when we handle functions, classes, and object-oriented programming, where the difference between local and global variables becomes very important.
We end this by understanding the definition of a variable:
A variable is a reference to something stored in memory. To be able to see this, have a look at this:
"""
- name is the variable
- print() standard output
- id() method that will allow us to get memory address of a particular stored entity
Note:
-Integers, strings, etc., are objects in Python
"""
name = "John Doe" # this is a variable definition
print(name) # standard output will print the value from memory
print(id(name)) # printing out id(name) which usually corresponds to the object ("John Doe") memory address. Now from the above, you notice that we are using the Python interpreter directly from the shell. We have defined the variable, but when we call the id() function and pass the name value, we attain the memory address of the value stored in memory (the identity of the object, not the variable itself) presented as an integer (number), hence the above definition.
Output of the above:
THE END
see you on the next …









