0% found this document useful (0 votes)
25 views3 pages

Python Variable Scope Explained

This worksheet explains variable scope in Python, detailing local, global, enclosed, and built-in scopes. It provides examples of local and global variables, how to modify global variables within functions, and common mistakes related to variable scope. Additionally, it includes exercises to practice understanding these concepts.

Uploaded by

saso memy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Python Variable Scope Explained

This worksheet explains variable scope in Python, detailing local, global, enclosed, and built-in scopes. It provides examples of local and global variables, how to modify global variables within functions, and common mistakes related to variable scope. Additionally, it includes exercises to practice understanding these concepts.

Uploaded by

saso memy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Functions & Variable Scope Worksheet

Understanding Variable Scope in Python

1. What is Variable Scope?


Scope determines where a variable can be accessed in a program.

Types of Scope:
- Local Scope: Inside a function
- Global Scope: Outside any function
- Enclosed Scope: Inside a nested function
- Built-in Scope: Predefined by Python

2. Local Variables (Inside Functions)


A local variable exists only inside the function where it is defined.

Example:
def greet():
message = 'Hello!'
print(message)

greet() # Works fine


print(message) # Error! 'message' is not defined outside

3. Global Variables (Outside Functions)


A global variable exists everywhere in the program.

Example:
name = 'Alice'
def greet():
print('Hello,', name)
greet()
print(name) # Works fine

4. Modifying Global Variables Inside a Function


To modify a global variable inside a function, use 'global'.

Example:
count = 0
def increase():
global count
count += 1
print(count)
increase()

5. Enclosed Scope (Nested Functions)


Inner functions can access variables from outer functions.

Example:
def outer():
message = 'Hello from outer!'
def inner():
print(message)
inner()
outer()

6. Common Mistakes in Variable Scope


1. Assuming Local Variables Exist Everywhere:
def add():
result = 10 + 5
print(result) # Error!

2. Forgetting to Use 'global':


count = 0
def increase():
count += 1 # Error! Needs 'global count'
increase()

Exercises: Predict the Output

Exercise 1: Local vs. Global


What will this code print?
x = 10
def my_function():
x=5
print('Inside function:', x)
my_function()
print('Outside function:', x)

Exercise 2: Fix the Global Variable Error


Fix the error in this code:
counter = 0
def increment():
counter += 1 # Error here!
print(counter)
increment()

Exercise 3: Nested Scope


What will this print?
def outer():
message = 'Hello'
def inner():
print(message)
inner()
outer()

You might also like