GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python Variables Python Variableslast modified February 26, 2025 Variables in Python are used to store data that can be referenced and manipulated throughout a program. Python supports various types of variables, including bound/unbound, static, class, and instance variables. This tutorial covers the different contexts in which variables are used, along with practical examples. Variables are names that refer to values stored in memory. They allow us to store and manipulate data in our program. Python variables do not require explicit declaration and can change types dynamically. Variables can be categorized based on their scope and context, such as local, global, class, and instance variables. Module variables
In Python, module variables are variables that are defined at the top level
of a module (a Key Characteristics:
Let's consider a module named example_module.py
# Module variables
PI = 3.14159
E = 2.71828
# Function that uses module variables
def calculate_circle_area(radius):
return PI * radius * radius
# Class that uses module variables
class MathConstants:
def get_constants(self):
return PI, E
In this example We can import these module variables into another module or script: main.py
from example_module import PI, E
print(f'The value of PI is: {PI}')
print(f'The value of E is:{E}')
In this case, Bound and Unbound VariablesThis example demonstrates bound and unbound variables in Python. bound_unbound.py
x = 10 # Bound variable (assigned a value)
def my_function():
print(y) # Unbound variable (not assigned a value)
# my_function() # Uncommenting this line will raise a NameError
A bound variable is one that has been assigned a value, like
Function-scoped VariablesThis example demonstrates Function-scoped variables in Python. static_variable.py
def my_function():
if not hasattr(my_function, "counter"):
my_function.counter = 0 # Static variable
my_function.counter += 1
return my_function.counter
print(my_function()) # Output: 1
print(my_function()) # Output: 2
print(my_function()) # Output: 3
A function-scoped variable retains its state across function calls. Class VariablesThis example demonstrates class variables in Python. class_variable.py
class MyClass:
class_var = 10 # Class variable
print(MyClass.class_var) # Output: 10
obj1 = MyClass()
obj2 = MyClass()
print(obj1.class_var) # Output: 10
print(obj2.class_var) # Output: 10
MyClass.class_var = 20 # Modify class variable
print(obj1.class_var) # Output: 20
print(obj2.class_var) # Output: 20
A class variable is a variable that is shared by all instances of a class. It is defined within the class but outside any method. Class variables are accessed using the class name or an instance of the class. Instance VariablesThis example demonstrates instance variables in Python. instance_variable.py
class MyClass:
def __init__(self, value):
self.instance_var = value # Instance variable
obj1 = MyClass(10)
obj2 = MyClass(20)
print(obj1.instance_var) # Output: 10
print(obj2.instance_var) # Output: 20
obj1.instance_var = 30
print(obj1.instance_var) # Output: 30
print(obj2.instance_var) # Output: 20
An instance variable is a variable that is unique to each
instance of a class. It is defined within the Global VariablesThis example demonstrates global variables in Python. global_variable.py
x = 10 # Global variable
def my_function():
global x # Declare x as global
x = 20
print(f"Inside function: {x}")
my_function()
print(f"Outside function: {x}")
# Output:
# Inside function: 20
# Outside function: 20
A global variable is a variable that is defined outside any
function or class and can be accessed throughout the program. To modify a global
variable inside a function, use the Local VariablesThis example demonstrates local variables in Python. local_variable.py
def my_function():
x = 10 # Local variable
print(f"Inside function: {x}")
my_function()
# print(x) # Uncommenting this line will raise a NameError
# Output:
# Inside function: 10
A local variable is a variable that is defined within a
function or block and is only accessible within that scope. Attempting to access
a local variable outside its scope raises a Best Practices for Using Variables
SourcePython Scopes and Namespaces Documentation In this article, we have explored Python variables and demonstrated their usage in different contexts through practical examples. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |