-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction_scope.py
More file actions
35 lines (23 loc) · 1 KB
/
Copy pathFunction_scope.py
File metadata and controls
35 lines (23 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# In Python, variables have scope (where they can be accessed) and lifetime (how long they exist). Variables are created when a function is called and destroyed when it returns. Understanding scope helps avoid unintended errors and improves code organization.
# Types of Scope in Python
# Local Scope (inside a function) – Variables declared inside a function are accessible only within that function.
# Global Scope (accessible everywhere) – Variables declared outside any function can be used throughout the program.
def sum(a, b):
# a and b are local variables
c = a + b
z = 1 # It creates a local variable called z which is destroyed after this function returns.
return c
def greet():
z = 32 # Local Variable
print("Hello")
z = 8 # z is a global Variable
print(z)
print(sum(4, 6))
print(z)
print("\n")
x = 10 # Global variable
def my_func():
x = 5 # Local variable
print(x)
my_func()
print(x) # (global x remains unchanged)