There are four main types of variables based on their scope and lifetime:
- 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)
- 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: 5
Code language: Python (python)
- 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: 200
Code language: Python (python)
- 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: 50
Code 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.
- 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: 20
Code language: Python (python)
- 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.14159
Code language: Python (python)
- 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;
- How to write update query in MySQL in Python?
- Simple Example Python Programs for Practice [Beginners ]
- What is for loop in python with example [10 Examples]
- Multiprocessing Python Example For Loop
- What is a list () in Python With Example
- What is an example of a user-defined function in Python?
- Can R and Python be used together? [With Example]
- How does format () work in Python?
- What is .2f Python format?
- What is the e function in Python?
- How do you write an automation test script in Python?
- How do you automate daily tasks in Python?