-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path27_local_global.py
More file actions
64 lines (45 loc) · 1.21 KB
/
27_local_global.py
File metadata and controls
64 lines (45 loc) · 1.21 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# --------------------------
# Local/Global Variable
# --------------------------
# --- Rules ---
"""
1. A local variable is declared inside a function and can only be used inside that function.
2. Variables inside functions are local by default.
3. Use global to change global variables inside a function.
4. Use nonlocal to change variables in enclosing (but non-global) functions.
5. Be careful with global variables — too many globals make code messy.
"""
# Example 1:- Local Variable
def my_func():
x = 10 # Local variable
print(x)
my_func()
# Example 2:- Global Variable
x = 20 # Global variable
def my_func():
print(x) # Can access global variable inside function
my_func()
print(x) # Works here too
# Example 3:- When Local & Global Variables Have Same Name
x = 50 # Global
def my_func():
x = 10 # Local (hides global)
print("Inside:", x)
my_func()
print("Outside:", x)
# Example 4:- Changing a Global Variable Inside a Function
x = 5
def change_global():
global x
x = 100
change_global()
print(x) # Output: 100
# Example 5:- Nested Functions & nonlocal
def outer():
x = 10
def inner():
nonlocal x
x = 20
inner()
print(x)
outer() # Output: 20