Scope refers to the coding area where a particular Python variable is accessible. Hence one cannot access any particular variable from anywhere from the code. We have studied about variables in previous lectures. Recall that a variable is a label for a location in memory. It holds a value. Not all variables are accessible, and not all variables exist for the same amount of time.
Where the variables defined determines that is it accessible or not, and how long it will exist.
A variable that is declared inside a function or loop is called a local variable. In the case of functions, when we define a variable within a function, its scope lies within the function only. It is accessible from the point where it is defined until the end of the function. It will exist for as long as the function is executing. Local variables cannot be accessed outside the function. The parameter names in the function, they behave like a local variable.
def sum(): a=10 #local variable cannot be accessed outside the function b=20 sum=a+b print( sum) print(a) #this gives an error
When you try to access variable “a” outside the function, it will give an error. It is accessible within the function only.
On the other hand, a global variable is easier to understand; it is not declared inside the function and can be accessed anywhere within a program. It can also be defined as a variable defined in the main body of the program. Any function or loop can access it. Its scope is anywhere within the program.
a=1 #global variable def print_Number(): a=a+1; print(a) print_number()
This is because we can only access the global variable, but we cannot modify it from inside of the function.
For this purpose, we use the global keyword. In Python, the global keyword allows us to modify the global variable. It is used to create a global variable and make changes to the variable in a local scope.
When we define a function inside another function, it becomes a nested function. We already know how to access a global variable from a function by using a global keyword. When we declare a local variable in a function, its scope is usually restricted to that function alone. This is because each function and subfunction stores its variables in its separate workspace.
A nested function also has its own workspace. But it can be accessed to the workspaces of all functions in which it is nested. A variable whose value is assigned by the primary function can be read or overwritten by a function nested at any level within the primary.
# l = 10 # Global # # def function1(n): # # l = 5 #Local # m = 8 #Local # global l # l = l + 45 # print(l, m) # print(n, "I have printed") # # function1("This is me") # # print(m) x = 89 def harry(): x = 20 def rohan(): global x x = 88 # print("before calling rohan()", x) rohan() print("after calling rohan()", x) harry() print(x)
Sir i am getting a eror after writing this code Please check my code once # l = 10 # Global # # def function1(n): # # l = 5 # Local # m = 23 # Local # # global 1 # l = 1 + 54 # print(l, m) # print(n,"I have printed") # # function1("This is me") # # print(l) def Porvi() : x = 20 def rohan(): global x x = 88 print("before calling rohan()", x) rohan() print("after calling rohan()", x) Porvi() print(x)
No downloadable resources for this video. If you think you need anything, please post it in the QnA!
Any Course related announcements will be posted here