-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.local_global_variables.py
More file actions
63 lines (39 loc) · 1.05 KB
/
21.local_global_variables.py
File metadata and controls
63 lines (39 loc) · 1.05 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
61
62
63
# Python Local Variables
# Example : 1
print('>>>> Example - 1 >>>>')
def greet():
msg_hello = 'Hello'
print('Local variable : ', msg_hello)
greet()
# try to access message variable
# print(msg_hello) #NameError: name 'msg_hello' is not defined
# Python Global Variables
# Example : 1
print('\n')
print('>>>> Example - 2 >>>>')
# declare global variable
msg_hi = 'Hi'
def greet_one():
print('Local variable : ', msg_hi)
greet_one()
print('Global variable : ', msg_hi)
# Example : 2
print('\n')
print('>>>> Example - 3 >>>>')
# declare global variable
msg_bye = 'Bye'
def greet_two():
print('Local variable : ', msg_bye)
# msg_bye = 'Bye Bye' #UnboundLocalError: local variable 'msg_bye' referenced before assignment
greet_two()
# Example : 3
print('\n')
print('>>>> Example - 4 >>>>')
# declare global variable
msg_bye_bye = 'Bye Bye'
print('Global variable before update : ', msg_bye_bye)
def greet_three():
global msg_bye_bye
msg_bye_bye = 'Bye Bye Updated'
print('Updated Global variable : ', msg_bye_bye)
greet_three()