Practical 14: Local Variable Program
Practical Name: Local Variable Program Objective: To demonstrate local
variable scope and lifetime in Python functions.
def demonstrate_local_variables():
def outer_function():
# Local variable in outer function
outer_var = "I'm in outer function"
def inner_function():
# Local variable in inner function
inner_var = "I'm in inner function"
print("Inside inner_function:")
print(f"Inner variable: {inner_var}")
print(f"Can access outer variable: {outer_var}")
print("Inside outer_function:")
print(f"Outer variable: {outer_var}")
inner_function()
try:
print(inner_var) # This will raise an error
except NameError as e:
print(f"Cannot access inner_var from outer function: {e}")
# Demonstrate variable lifetime
def counter_function():
count = 0 # Local variable
count += 1
return count
print("Demonstrating local variables:")
outer_function()
print("\nDemonstrating variable lifetime:")
print(f"First call: {counter_function()}")
print(f"Second call: {counter_function()}")
print(f"Third call: {counter_function()}")
demonstrate_local_variables()
Output:
Demonstrating local variables:
Inside outer_function:
Outer variable: I'm in outer function
Inside inner_function:
1
Inner variable: I'm in inner function
Can access outer variable: I'm in outer function
Cannot access inner_var from outer function: name 'inner_var' is not defined
Demonstrating variable lifetime:
First call: 1
Second call: 1
Third call: 1
Practical 15: Global Variable Program
Practical Name: Global Variable Program Objective: To demonstrate global
variable usage, modification, and best practices in Python.
def demonstrate_global_variables():
# Global variable declaration
global_counter = 0
def modify_global_correct():
global global_counter
global_counter += 1
print(f"Global counter inside function: {global_counter}")
def modify_global_incorrect():
try:
global_counter += 1 # This will raise an error
except UnboundLocalError as e:
print(f"Error without global keyword: {e}")
class CounterClass:
class_counter = 0 # Class variable (similar to global)
def increment(self):
CounterClass.class_counter += 1
return CounterClass.class_counter
print("Initial global counter:", global_counter)
modify_global_correct()
print("Global counter after modification:", global_counter)
print("\nTrying to modify global without 'global' keyword:")
modify_global_incorrect()
print("\nDemonstrating class variables:")
counter1 = CounterClass()
counter2 = CounterClass()
2
print(f"Counter1 increment: {counter1.increment()}")
print(f"Counter2 increment: {counter2.increment()}")
print(f"Class counter value: {CounterClass.class_counter}")
demonstrate_global_variables()
Output:
Initial global counter: 0
Global counter inside function: 1
Global counter after modification: 1
Trying to modify global without 'global' keyword:
Error without global keyword: local variable 'global_counter' referenced before assignment
Demonstrating class variables:
Counter1 increment: 1
Counter2 increment: 2
Class counter value: 2
Practical 16: Static Variable Program
Practical Name: Static Variable Program Objective: To demonstrate static
variables and class-level attributes in Python classes.
def demonstrate_static_variables():
class Employee:
# Static variables
company_name = "TechCorp"
employee_count = 0
def __init__(self, name):
self.name = name # Instance variable
Employee.employee_count += 1 # Modifying static variable
@staticmethod
def get_company_info():
return f"Company: {Employee.company_name}, Employees: {Employee.employee_count}"
@classmethod
def change_company_name(cls, new_name):
cls.company_name = new_name
# Demonstrate static variable usage
print("Initial company info:")
print(Employee.get_company_info())
3
# Create employees
emp1 = Employee("Alice")
emp2 = Employee("Bob")
print("\nAfter creating employees:")
print(Employee.get_company_info())
# Change company name
Employee.change_company_name("NewTech")
print("\nAfter changing company name:")
print(Employee.get_company_info())
# Demonstrate static variable sharing
print("\nAccessing through instances:")
print(f"Employee 1 company: {emp1.company_name}")
print(f"Employee 2 company: {emp2.company_name}")
# Demonstrate instance vs static variables
emp1.department = "IT" # Instance variable
print("\nInstance variables:")
print(f"Employee 1 department: {emp1.department}")
try:
print(f"Employee 2 department: {emp2.department}")
except AttributeError as e:
print(f"Employee 2 department: Not found - {e}")
demonstrate_static_variables()
Output:
Initial company info:
Company: TechCorp, Employees: 0
After creating employees:
Company: TechCorp, Employees: 2
After changing company name:
Company: NewTech, Employees: 2
Accessing through instances:
Employee 1 company: NewTech
Employee 2 company: NewTech
Instance variables:
Employee 1 department: IT
Employee 2 department: Not found - 'Employee' object has no attribute 'department'
Would you like me to continue with the next set of practicals? We can cover
4
method overloading, classes, modules, and file operations next.