Local and Global Variables in Python: Explained with Examples and Tasks | Python for Beginners
Learn the difference between local and global variables in Python with simple explanations, practical examples, and beginner-friendly tasks. Understand constants, best practices, and how to manage variables effectively in your programs.
Local variables are variables declared inside a function. They can only be accessed within that function and are destroyed when the function completes execution..
It only exists while the function is running.
You cannot use it outside the function.
Characteristics:
Created when the function starts
Only accessible within the function
Destroyed when the function exits
Example:
def greet(): message = "Hello, world!" # local variable print(message)greet()# This will cause an error:# print(message)
message is local to greet() and can't be accessed outside.
Key Points:
Local variables are created when the function starts.
They are destroyed when the function ends.
They do not affect variables outside the function.
Task for Students:
Write a function called add_numbers that:
Takes two numbers as input.
Adds them together.
Stores the result in a local variable.
Prints the result.
Example Solution:
def add_numbers(a, b): result = a + b print("Sum:", result)add_numbers(5, 10)
2. Global Variables
What is a Global Variable?
A global variable is a variable that is declared outside all functions and can be accessed throughout the entire program..
It can be used both inside and outside functions.
Example:
name = "Ali" # global variabledef greet(): print("Hello", name)greet()print("Goodbye", name)
name is accessible inside and outside the function.
Changing a Global Variable Inside a Function
If you want to modify a global variable inside a function, you must use the global keyword.
{% include youtube-video.html video_type=video_type video_id=video_id %}
3. Constants
A constant is a variable that should not change once assigned.
In Python, constants are typically declared as global variables with ALL_CAPS names by convention.
Python does not force constants, but developers treat them as unchangeable.
In programming:
A convention is a common habit or best practice that developers choose to follow to make code more readable and consistent.
Python itself does not force it, but good programmers still follow it.
Example:
PI = 3.14159 # constantdef area_of_circle(radius): return PI * radius * radiusprint(area_of_circle(5))
PI = 3.14159 # This is a constant (by convention)TAX_RATE = 0.20 # Another constantdef calculate_tax(amount): return amount * TAX_RATEprint(calculate_tax(100)) # Output: 20.0
4. Best Practices:
Avoid excessive use of global variables as they can make code harder to maintain
Use ALL_CAPS for constants to indicate they shouldn't be modified
When you must use global variables, declare them clearly at the top of your file
5. Tasks:
Task #1: Global Variable Task
Create a global variable called language set to "Python".
Write a function show_language() that prints "I love Python!" using the global variable.
Task #2:
Define a constant GRAVITY = 9.8.
Write a function weight_on_earth(mass) that calculates and returns the weight.
Task #3:
Create a program with:
A global constant DISCOUNT set to 0.10
A global variable total_purchases initialized to 0
A function make_purchase(amount) that adds to total_purchases and applies the discount
A function show_total() that prints the current total