Check out our courses:
Spring and Microservices Weekend Live Batch : [ Ссылка ]
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : [ Ссылка ]
For More Queries WhatsApp or Call on : +919008963671
website : [ Ссылка ]
Instagram : [ Ссылка ]
Linkedin : [ Ссылка ]
TELUSKO Android App : [ Ссылка ]
Discord : [ Ссылка ]
In this lectre we are discussing:
#1 Scope of variable
#2 Local variable
#3 Global variable
#4 Global keyword
#5 Global() function
#1 Scope of variable
-- scope of variable means where we can access the variable
-- there are two types of scope of variable
1. local scope
2. global scope
#2 Local variable
-- local variable means variable which is defined inside the function
-- we can access the local variable inside the function only
-- we cannot access the local variable outside the function
Local Scope:
When a variable or function is defined inside a function, it is said to be in the local scope of that function.
Variables defined in the local scope are only accessible within that function and are destroyed once the function
completes execution. Local variables have the highest priority in accessing over global variables of the same name.
def func():
x = 10
print(x)
func()
-- the variable x is defined inside the function, and it can only be accessed within the function.
#3 Global variable
-- global variable means variable which is defined outside the function
-- we can access the global variable inside the function
-- we can access the global variable outside the function
x = 10
def func():
print(x)
func()
-- the variable x is defined outside the function, and it can be accessed within the function.
#4 Global keyword
-- if we want to access the global variable inside the function and we want to change the value of
global variable inside the function then we have to use global keyword.
x = 10
def func():
global x
x = 15
print(x)
func()
-- in this case no new variable is created inside the function,
but the global variable x is accessed and modified inside the function.
#5 Global() function
-- if we want to access the global variable inside the function and we want to change the value of global variable
inside the function then we have to use global() function.
e.g
x = 10
def func():
x = 15
print("local x: ",x)
y = globals()['x']
print("global x: ",y)
globals()['x'] = 20
-- using globals()['x'] we can access the global variable x inside the
function and we can change the value of global variable x inside the function.
Python Tutorial to learn Python programming with examples
Complete Python Tutorial for Beginners Playlist : [ Ссылка ]
Python Tutorial in Hindi : [ Ссылка ]
Github :- [ Ссылка ]-
Java and Spring Framework For beginners with Spring Boot : - [ Ссылка ]
Java Tutorial for Beginners (2023) :- [ Ссылка ]
Subscribe to our other channel:
Navin Reddy : [ Ссылка ]
Telusko Hindi :
[ Ссылка ]
![](https://s2.save4k.ru/pic/QYUbLevwgDQ/maxresdefault.jpg)