Python 12 min read

Python Variables: The Comprehensive Guide to Core Concepts

Learn how to declare, manage, and understand variables in Python, including dynamic typing, scope, memory references, and professional best practices.

Muhammad Ijaz
Written by Muhammad Ijaz
Software Engineering Student & Founder of Skilloratic
Published: July 28, 2026 Last updated: August 22, 2026
Python programming language code on screen
Variables are the building blocks of any Python application.
This guide is part of our comprehensive Python Developer Roadmap Worldwide.

1. What Are Variables in Python?

At its core, a variable in programming is essentially a reserved memory location to store values. In other words, a variable in a Python program gives data to the computer for processing. However, Python handles variables differently than many other statically-typed languages like C++ or Java.

In Python, variables are better thought of as names or labels attached to objects in memory, rather than boxes where you put data. When you assign a value to a variable, Python creates an object in memory and binds the variable name to that object.

"In Python, variables are not buckets containing values; they are labels pointing to objects." - Luciano Ramalho, Fluent Python

2. Variable Assignment and Naming Rules

Assigning a value to a variable in Python is straightforward. You do not need to declare the variable type beforehand. You simply use the assignment operator =.

# Simple assignment
name = "Skilloratic"
age = 5
is_learning = True

# Multiple assignment in a single line
x, y, z = 10, 20, 30

# Assigning the same value to multiple variables
a = b = c = 100

When naming variables, Python enforces a few strict rules and suggests several conventions:

  • Must start with a letter or underscore: user_age or _private_var are valid. 1st_place is invalid.
  • Can only contain alphanumeric characters and underscores: (A-z, 0-9, and _). No special characters like @, $, or % are allowed.
  • Case-sensitive: Age, age, and AGE are three distinct variables.
  • Cannot be a Python keyword: You cannot name a variable class, def, return, if, etc.

3. Dynamic Typing and Data Types

Python is a dynamically typed language. This means you do not need to explicitly declare the data type of a variable. The Python interpreter automatically infers the data type based on the assigned value at runtime.

Furthermore, the type of a variable can change during the execution of a program if you reassign it to a different type of value. This flexibility makes Python incredibly fast to write but requires developers to be mindful of what their variables currently hold.

# Dynamic typing in action
my_variable = 100          # Initially an integer (int)
print(type(my_variable))   # Output: <class 'int'>

my_variable = "Hello"      # Now a string (str)
print(type(my_variable))   # Output: <class 'str'>

my_variable = [1, 2, 3]    # Now a list
print(type(my_variable))   # Output: <class 'list'>
Pro Tip (Python 3.5+): While Python is dynamically typed, you can use Type Hints to indicate expected data types. This doesn't enforce types at runtime, but helps IDEs and tools like mypy catch bugs early. Example: age: int = 25.

4. Variable Scope (Local, Global, Nonlocal)

The scope of a variable defines the region of the code where the variable is accessible. Understanding scope is crucial to avoid bugs related to unexpected variable modifications.

Local Scope

Variables declared inside a function are in the local scope. They cannot be accessed from outside that function.

def greet():
    message = "Hello, World!"  # Local variable
    print(message)

greet()
# print(message)  # This would raise a NameError

Global Scope

Variables declared outside of any function have a global scope and can be accessed anywhere in the file. If you need to modify a global variable inside a function, you must use the global keyword.

counter = 0  # Global variable

def increment():
    global counter
    counter += 1
    print(counter)

increment()  # Output: 1

Nonlocal Scope

Used in nested functions, the nonlocal keyword allows you to modify a variable in the nearest enclosing scope that is not global.

def outer_function():
    x = "local"
    
    def inner_function():
        nonlocal x
        x = "nonlocal"
        print("Inner:", x)
        
    inner_function()
    print("Outer:", x)

outer_function()
# Output:
# Inner: nonlocal
# Outer: nonlocal

5. Best Practices for Naming Variables

Writing code that a machine can understand is easy; writing code that humans can understand takes discipline. Adhering to Python's style guide (PEP 8) ensures your code is professional and readable.

  • Use snake_case for standard variables and functions: E.g., user_account_balance instead of userAccountBalance.
  • Be descriptive but concise: A variable named employee_salary is infinitely better than s or emp_sal.
  • Use UPPERCASE_WITH_UNDERSCORES for constants: Variables whose values should never change (like configuration values) should be capitalized. E.g., MAX_RETRIES = 5.
  • Prefix "private" variables with an underscore: E.g., _internal_state. This is a convention to tell other developers, "This is for internal use only, tread carefully."

6. Memory Management and Object References

Because variables are just labels, when you assign one variable to another, they both point to the exact same object in memory. This is particularly important when dealing with mutable objects like lists or dictionaries.

# Immutable objects (integers, strings, tuples)
a = 10
b = a
a = 20
print(b)  # Output: 10 (b still points to the original integer 10)

# Mutable objects (lists, dicts, sets)
list_x = [1, 2, 3]
list_y = list_x
list_x.append(4)
print(list_y)  # Output: [1, 2, 3, 4] 
# Both labels point to the same list in memory!

To check if two variables point to the exact same memory location, you can use the is operator or compare their id() values.

7. Free Resources & Internships (2026)

To help you continue your Python journey, we have compiled a list of excellent free resources, bootcamps, and internship opportunities for 2026.

Free Learning Resources

Python Internship Platforms

Industry References & Sources

Claims regarding popularity, career demand, and salary expectations for Python developers are backed by the following official reports.

Python Variables Essential Resources

Ready to take the next step? Here are the most relevant and targeted resources specifically for Python Variables:

Comments

Leave a Reply

No comments yet. Be the first to share your thoughts!

Share this Article
Ijaz Ahmad

Ijaz Ahmad

Founder of Skilloratic

Ijaz is a passionate software engineer with over 2 years of experience building scalable web applications. He loves sharing his knowledge through comprehensive guides and tutorials.