What are the types of variables in Python?

There are four main types of variables based on their scope and lifetime:

  1. Local Variables: These variables are defined within a function and can only be accessed within that specific function. They have a limited scope and are created when the function is called and destroyed when the function exits. Local variables are not accessible outside the function.

Example:

def example_function():
    x = 10  # x is a local variable
    print(x)

example_function()  # Output: 10
# print(x)  # This would raise an error since x is not defined outside the function
Code language: Python (python)
  1. Global Variables: These variables are defined outside any function and have a global scope, making them accessible from any part of the code, including within functions. Global variables exist throughout the program’s lifetime.

Example:

y = 5  # y is a global variable

def another_function():
    print(y)

another_function()  # Output: 5Code language: Python (python)
  1. Instance Variables (Attributes): These variables belong to instances (objects) of a class. They are defined within the class but outside any method and are unique to each instance of the class. Instance variables represent the state of the object.

Example:

class MyClass:
    def __init__(self, value):
        self.value = value  # value is an instance variable

obj1 = MyClass(100)
obj2 = MyClass(200)

print(obj1.value)  # Output: 100
print(obj2.value)  # Output: 200Code language: Python (python)
  1. Class Variables (Static Variables): These variables are shared among all instances of a class. They are defined within the class, outside any method, and are the same for all objects of that class. Class variables represent attributes that are common to all objects of the class.

Example:

class MyClass:
    class_variable = 50  # class_variable is a class variable

obj1 = MyClass()
obj2 = MyClass()

print(obj1.class_variable)  # Output: 50
print(obj2.class_variable)  # Output: 50Code language: Python (python)

It’s important to understand the differences between these variable types, as they have various implications for how they can be accessed and modified within your code.

  1. Non-local Variables (Free Variables): These variables are defined in an enclosing function and are used in nested functions. They are neither local nor global but fall somewhere in between. Non-local variables can be accessed and modified within the nested function but cannot be assigned directly within it.

Example:

def outer_function():
    x = 10  # local variable in outer_function

    def inner_function():
        nonlocal x
        x = 20  # modifying the non-local variable
        print("Inner function:", x)

    inner_function()
    print("Outer function:", x)

outer_function()
# Output:
# Inner function: 20
# Outer function: 20Code language: Python (python)
  1. Class Constants: These variables are similar to class variables but represent constant values that should not be modified. Conventionally, they are written in uppercase letters to indicate their constant nature, although Python does not enforce constant behavior.

Example:

class MathConstants:
    PI = 3.14159
    E = 2.71828

print(MathConstants.PI)  # Output: 3.14159Code language: Python (python)
  1. Mutable vs. Immutable Variables: Python variables can be further categorized based on their mutability. Immutable variables, like numbers, strings, and tuples, cannot be changed after creation. In contrast, mutable variables, such as lists, dictionaries, and sets, can be modified after their creation.

Example of immutable variables:

x = 10
y = "hello"
z = (1, 2, 3)Code language: Python (python)

Example of mutable variables:

my_list = [1, 2, 3]
my_dict = {"name": "John", "age": 30}
my_set = {1, 2, 3}Code language: Python (python)

These are some of the main types of variables in Python. Understanding these variable types and their characteristics is crucial for writing efficient and effective Python code.

Read More;

  • Yaryna Ostapchuk

    I am an enthusiastic learner and aspiring Python developer with expertise in Django and Flask. I pursued my education at Ivan Franko Lviv University, specializing in the Faculty of Physics. My skills encompass Python programming, backend development, and working with databases. I am well-versed in various computer software, including Ubuntu, Linux, MaximDL, LabView, C/C++, and Python, among others.

    View all posts

Leave a Comment