Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconPython Desbloqueado para Principiantes
Python Desbloqueado para Principiantes

Chapter 5: Functions

5.4: Scope of Variables

In Python, it's important to understand the visibility and accessibility of a variable, which depends on its scope. Scope refers to the area in your code where a variable can be accessed or used. By understanding variable scope, you can write efficient and error-free code. Python has two main types of variable scopes: 

5.4.1: Global scope:

A variable declared outside a function or a block of code has global scope. This means that you can access the variable from anywhere in the code, including within functions. However, modifying the value of a global variable inside a function is not recommended unless you use the global keyword.

Understanding the difference between global and local scope is essential for writing effective Python code. By using the appropriate scope for your variables, you can ensure that your code is efficient and easy to read. 

Example:

global_var = "I am a global variable"

def my_function():
    print(global_var) 

my_function()  # Output: I am a global variable

5.4.2: Local scope:

A variable declared inside a function or block of code has a local scope, which means that it can only be accessed within that function or block of code. This is a useful feature because it allows us to keep variables contained within a specific section of our code, avoiding any potential conflicts with other variables that may have the same name. 

However, it is important to note that once the function or block of code finishes its execution, the local variable is destroyed and cannot be accessed anymore. This is why it is crucial to properly manage the scope of your variables, ensuring that they are accessible when and where they are needed, but not kept around unnecessarily after their use has ended.

Example:

def my_function():
    local_var = "I am a local variable"
    print(local_var)

my_function()  # Output: I am a local variable
print(local_var)  # Error: local_var is not defined in the global scope

It's important to note that if a local variable has the same name as a global variable, the local variable takes precedence inside the function. This means that any operations performed on the variable inside the function will not affect the global variable.

Example:

x = 10

def modify_x():
    x = 5
    print(f"Inside the function, x is {x}")

modify_x()  # Output: Inside the function, x is 5
print(f"Outside the function, x is {x}")  # Output: Outside the function, x is 10

In summary, understanding the scope of variables is crucial for managing data throughout your code. Global variables can be accessed anywhere in the code but should be used sparingly, while local variables are restricted to their respective functions or blocks of code.

Exercise 5.4.1: Accessing Global Variables Inside a Function

In this exercise, you will practice accessing a global variable inside a function without modifying it.

Instructions:

  1. Create a global variable called name and assign it the value "John".
  2. Define a function called print_name that prints the global variable name.
  3. Call the print_name function.

Solution:

name = "John"

def print_name():
    print(name)

print_name()

Output:

John

Exercise 5.4.2: Modifying Global Variables Inside a Function

In this exercise, you will practice modifying a global variable inside a function using the global keyword.

Instructions:

  1. Create a global variable called counter and assign it the value 0.
  2. Define a function called increment_counter that increments the global variable counter by 1.
  3. Call the increment_counter function three times.
  4. Print the value of counter.

Solution:

counter = 0

def increment_counter():
    global counter
    counter += 1

increment_counter()
increment_counter()
increment_counter()

print(counter)

Output:

3

Exercise 5.4.3: Local Variables vs. Global Variables

In this exercise, you will practice using both local and global variables with the same name and observe their behavior inside and outside a function.

Instructions:

  1. Create a global variable called message and assign it the value "Global message".
  2. Define a function called print_local_message that:
    a. Creates a local variable called message with the value "Local message".
    b. Prints the local variable message.
  3. Call the print_local_message function.
  4. Print the global variable message.

Solution:

message = "Global message"

def print_local_message():
    message = "Local message"
    print(message)

print_local_message()
print(message)

Output:

Local message
Global message

5.4: Scope of Variables

In Python, it's important to understand the visibility and accessibility of a variable, which depends on its scope. Scope refers to the area in your code where a variable can be accessed or used. By understanding variable scope, you can write efficient and error-free code. Python has two main types of variable scopes: 

5.4.1: Global scope:

A variable declared outside a function or a block of code has global scope. This means that you can access the variable from anywhere in the code, including within functions. However, modifying the value of a global variable inside a function is not recommended unless you use the global keyword.

Understanding the difference between global and local scope is essential for writing effective Python code. By using the appropriate scope for your variables, you can ensure that your code is efficient and easy to read. 

Example:

global_var = "I am a global variable"

def my_function():
    print(global_var) 

my_function()  # Output: I am a global variable

5.4.2: Local scope:

A variable declared inside a function or block of code has a local scope, which means that it can only be accessed within that function or block of code. This is a useful feature because it allows us to keep variables contained within a specific section of our code, avoiding any potential conflicts with other variables that may have the same name. 

However, it is important to note that once the function or block of code finishes its execution, the local variable is destroyed and cannot be accessed anymore. This is why it is crucial to properly manage the scope of your variables, ensuring that they are accessible when and where they are needed, but not kept around unnecessarily after their use has ended.

Example:

def my_function():
    local_var = "I am a local variable"
    print(local_var)

my_function()  # Output: I am a local variable
print(local_var)  # Error: local_var is not defined in the global scope

It's important to note that if a local variable has the same name as a global variable, the local variable takes precedence inside the function. This means that any operations performed on the variable inside the function will not affect the global variable.

Example:

x = 10

def modify_x():
    x = 5
    print(f"Inside the function, x is {x}")

modify_x()  # Output: Inside the function, x is 5
print(f"Outside the function, x is {x}")  # Output: Outside the function, x is 10

In summary, understanding the scope of variables is crucial for managing data throughout your code. Global variables can be accessed anywhere in the code but should be used sparingly, while local variables are restricted to their respective functions or blocks of code.

Exercise 5.4.1: Accessing Global Variables Inside a Function

In this exercise, you will practice accessing a global variable inside a function without modifying it.

Instructions:

  1. Create a global variable called name and assign it the value "John".
  2. Define a function called print_name that prints the global variable name.
  3. Call the print_name function.

Solution:

name = "John"

def print_name():
    print(name)

print_name()

Output:

John

Exercise 5.4.2: Modifying Global Variables Inside a Function

In this exercise, you will practice modifying a global variable inside a function using the global keyword.

Instructions:

  1. Create a global variable called counter and assign it the value 0.
  2. Define a function called increment_counter that increments the global variable counter by 1.
  3. Call the increment_counter function three times.
  4. Print the value of counter.

Solution:

counter = 0

def increment_counter():
    global counter
    counter += 1

increment_counter()
increment_counter()
increment_counter()

print(counter)

Output:

3

Exercise 5.4.3: Local Variables vs. Global Variables

In this exercise, you will practice using both local and global variables with the same name and observe their behavior inside and outside a function.

Instructions:

  1. Create a global variable called message and assign it the value "Global message".
  2. Define a function called print_local_message that:
    a. Creates a local variable called message with the value "Local message".
    b. Prints the local variable message.
  3. Call the print_local_message function.
  4. Print the global variable message.

Solution:

message = "Global message"

def print_local_message():
    message = "Local message"
    print(message)

print_local_message()
print(message)

Output:

Local message
Global message

5.4: Scope of Variables

In Python, it's important to understand the visibility and accessibility of a variable, which depends on its scope. Scope refers to the area in your code where a variable can be accessed or used. By understanding variable scope, you can write efficient and error-free code. Python has two main types of variable scopes: 

5.4.1: Global scope:

A variable declared outside a function or a block of code has global scope. This means that you can access the variable from anywhere in the code, including within functions. However, modifying the value of a global variable inside a function is not recommended unless you use the global keyword.

Understanding the difference between global and local scope is essential for writing effective Python code. By using the appropriate scope for your variables, you can ensure that your code is efficient and easy to read. 

Example:

global_var = "I am a global variable"

def my_function():
    print(global_var) 

my_function()  # Output: I am a global variable

5.4.2: Local scope:

A variable declared inside a function or block of code has a local scope, which means that it can only be accessed within that function or block of code. This is a useful feature because it allows us to keep variables contained within a specific section of our code, avoiding any potential conflicts with other variables that may have the same name. 

However, it is important to note that once the function or block of code finishes its execution, the local variable is destroyed and cannot be accessed anymore. This is why it is crucial to properly manage the scope of your variables, ensuring that they are accessible when and where they are needed, but not kept around unnecessarily after their use has ended.

Example:

def my_function():
    local_var = "I am a local variable"
    print(local_var)

my_function()  # Output: I am a local variable
print(local_var)  # Error: local_var is not defined in the global scope

It's important to note that if a local variable has the same name as a global variable, the local variable takes precedence inside the function. This means that any operations performed on the variable inside the function will not affect the global variable.

Example:

x = 10

def modify_x():
    x = 5
    print(f"Inside the function, x is {x}")

modify_x()  # Output: Inside the function, x is 5
print(f"Outside the function, x is {x}")  # Output: Outside the function, x is 10

In summary, understanding the scope of variables is crucial for managing data throughout your code. Global variables can be accessed anywhere in the code but should be used sparingly, while local variables are restricted to their respective functions or blocks of code.

Exercise 5.4.1: Accessing Global Variables Inside a Function

In this exercise, you will practice accessing a global variable inside a function without modifying it.

Instructions:

  1. Create a global variable called name and assign it the value "John".
  2. Define a function called print_name that prints the global variable name.
  3. Call the print_name function.

Solution:

name = "John"

def print_name():
    print(name)

print_name()

Output:

John

Exercise 5.4.2: Modifying Global Variables Inside a Function

In this exercise, you will practice modifying a global variable inside a function using the global keyword.

Instructions:

  1. Create a global variable called counter and assign it the value 0.
  2. Define a function called increment_counter that increments the global variable counter by 1.
  3. Call the increment_counter function three times.
  4. Print the value of counter.

Solution:

counter = 0

def increment_counter():
    global counter
    counter += 1

increment_counter()
increment_counter()
increment_counter()

print(counter)

Output:

3

Exercise 5.4.3: Local Variables vs. Global Variables

In this exercise, you will practice using both local and global variables with the same name and observe their behavior inside and outside a function.

Instructions:

  1. Create a global variable called message and assign it the value "Global message".
  2. Define a function called print_local_message that:
    a. Creates a local variable called message with the value "Local message".
    b. Prints the local variable message.
  3. Call the print_local_message function.
  4. Print the global variable message.

Solution:

message = "Global message"

def print_local_message():
    message = "Local message"
    print(message)

print_local_message()
print(message)

Output:

Local message
Global message

5.4: Scope of Variables

In Python, it's important to understand the visibility and accessibility of a variable, which depends on its scope. Scope refers to the area in your code where a variable can be accessed or used. By understanding variable scope, you can write efficient and error-free code. Python has two main types of variable scopes: 

5.4.1: Global scope:

A variable declared outside a function or a block of code has global scope. This means that you can access the variable from anywhere in the code, including within functions. However, modifying the value of a global variable inside a function is not recommended unless you use the global keyword.

Understanding the difference between global and local scope is essential for writing effective Python code. By using the appropriate scope for your variables, you can ensure that your code is efficient and easy to read. 

Example:

global_var = "I am a global variable"

def my_function():
    print(global_var) 

my_function()  # Output: I am a global variable

5.4.2: Local scope:

A variable declared inside a function or block of code has a local scope, which means that it can only be accessed within that function or block of code. This is a useful feature because it allows us to keep variables contained within a specific section of our code, avoiding any potential conflicts with other variables that may have the same name. 

However, it is important to note that once the function or block of code finishes its execution, the local variable is destroyed and cannot be accessed anymore. This is why it is crucial to properly manage the scope of your variables, ensuring that they are accessible when and where they are needed, but not kept around unnecessarily after their use has ended.

Example:

def my_function():
    local_var = "I am a local variable"
    print(local_var)

my_function()  # Output: I am a local variable
print(local_var)  # Error: local_var is not defined in the global scope

It's important to note that if a local variable has the same name as a global variable, the local variable takes precedence inside the function. This means that any operations performed on the variable inside the function will not affect the global variable.

Example:

x = 10

def modify_x():
    x = 5
    print(f"Inside the function, x is {x}")

modify_x()  # Output: Inside the function, x is 5
print(f"Outside the function, x is {x}")  # Output: Outside the function, x is 10

In summary, understanding the scope of variables is crucial for managing data throughout your code. Global variables can be accessed anywhere in the code but should be used sparingly, while local variables are restricted to their respective functions or blocks of code.

Exercise 5.4.1: Accessing Global Variables Inside a Function

In this exercise, you will practice accessing a global variable inside a function without modifying it.

Instructions:

  1. Create a global variable called name and assign it the value "John".
  2. Define a function called print_name that prints the global variable name.
  3. Call the print_name function.

Solution:

name = "John"

def print_name():
    print(name)

print_name()

Output:

John

Exercise 5.4.2: Modifying Global Variables Inside a Function

In this exercise, you will practice modifying a global variable inside a function using the global keyword.

Instructions:

  1. Create a global variable called counter and assign it the value 0.
  2. Define a function called increment_counter that increments the global variable counter by 1.
  3. Call the increment_counter function three times.
  4. Print the value of counter.

Solution:

counter = 0

def increment_counter():
    global counter
    counter += 1

increment_counter()
increment_counter()
increment_counter()

print(counter)

Output:

3

Exercise 5.4.3: Local Variables vs. Global Variables

In this exercise, you will practice using both local and global variables with the same name and observe their behavior inside and outside a function.

Instructions:

  1. Create a global variable called message and assign it the value "Global message".
  2. Define a function called print_local_message that:
    a. Creates a local variable called message with the value "Local message".
    b. Prints the local variable message.
  3. Call the print_local_message function.
  4. Print the global variable message.

Solution:

message = "Global message"

def print_local_message():
    message = "Local message"
    print(message)

print_local_message()
print(message)

Output:

Local message
Global message