-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobal.py
43 lines (27 loc) · 874 Bytes
/
Global.py
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
# Variables that are created outside of a function are known as global variables
# Global variables can be used by everyone, both inside of functions and outside
a="Best"
def myfun1():
print("I Am "+a)
myfun1()
"""If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function.
The global variable with the same name will remain as it was, global and with the original value."""
a="Best"
def myfun2():
a="Good"
print("I Am "+a)
myfun2()
print("I Am "+a)
# To create a global variable inside a function, you can use the global keyword
def myfunc():
global x
x="Rana"
myfunc()
print(x)
# To change the value of a global variable inside a function, refer to the variable by using the global keyword
a="Best"
def myfun():
global a
a="Good"
myfun()
print("I Am "+a)