Introduction
Welcome to my 48th blog post on the Python journey. On day 48, I learned about two types of variables in Python, viz local and global variables. I also learned about the difference in the accessibility of local and global variables and how they can be effectively used in Python. Now Let's dive into more details and understand the working of the local and global variables in Python.
So let's get started......
Local Variables
It is a variable that is defined within a function
It is accessible only within the function in which it is defined
It is created when the function is called and is destroyed when the function returns.
Global Variables
It is a variable that is defined outside of a function
It is accessible from within any function in your code.
It is not destroyed and stays in the program till the program executes
Example -
Here, we have a global variable 'x' and a local variable 'y'.
We can access the value of the global variable x from within the function, but we cannot access the value of the local variable y outside of the function.
x = 10 # global variable
def my_function():
y = 5 # local variable
print(y)
my_function()
print(x)
print(y) # this will cause an error because y is a local variable and is not accessible outside of the function
Global Keyword
What if we want to modify the 'global' variable from inside the function?
In this case, we make use of the 'global' keyword. This declares the variable as a global variable and it can be accessed from the global scope.
Example -
x = 10 # global variable
def my_function():
global x
x = 5 # this will change the value of the global variable x
y = 5 # local variable
my_function()
print(x) # prints 5
# print(y) # this will cause an error because y is a local variable and is not accessible outside of the function
Here we used the global keyword to declare the global variable x from within the function. As a result, the value of x is changed to 5.
Note - To avoid unexpected program behaviour and make debugging harder, it is generally a good practice to avoid modifying global variables from within functions.
Resources Used
You can watch the video of Day#48 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 48, I learned about difference of local and global variables and how we can access the global variable form within the functions.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #48
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.