Skip to main content
What is the scope in python?


Hey everyone TheCodingProject is back with the answer to another question from the world of Python and this time it’s about the “scope” in python code.
In Python the scope is indeed decided by the number of indents. It must be 4 spaces. In Python every object always has a local scope and the scope is always decided by the indent of that particular block within which the object is defined. Let's consider an example
  1. def foo ():
  2. var = 'abc'
  3. return var
In the above example, the scope of the variable var is limited to the function foo and how is the scope of the variable var is defined? It's defined by the indent that it maintains I.e. by making the indentation such that it comes under the function definition of foo, which tells the Python interpreter that the variable var is within the local scope of function foo. Now if you do the following -
  1. def foo():
  2.   var = 'abc'
  3. return var
  4. var = 'xyz'
  5. print (var)
  6. print(foo())
The output should be like the following-
  1. xyz
  2. abc
Why do you think the value of var was as such? The reason was again teh local scope of variable var. Notice that when the variable var was having a value ‘xyz' then the indent of the variable was same as the function definition of foo so the scope of variable didn't fall under the function definition. So, when we say print(var) then the current value of var was printed I.e. ‘xyz'. In the second print statement when the function foo was called then the value of variable var that was defined inside foo was printed since the instance of var having value ‘abc' was defined inside the indentation of function foo.
Now when we give only 1 space before the variable var inside foo-
  1. def foo():
  2. var = 'abc'
  3. return var
When you try to call the above function the interpreter will throw an error since we have defined only a single space before the variable var.
So, you see in Python the scope is decided by the indentation and the indent is 4 spaces and this is how the interpreter understands that which scope is to be used and under which block of code.
So, folks this was all for this week but more stuffs are coming your way next week, til then happy coding.


If you wanna get started in learning Python then why not get started with the python basics chapter. Check out this link to access the Python Basic Chapter for free. If you want to download a copy of the chapter then purchase the chapter by clicking on the following link.


Subscribe to my youtube channel to watch new python programming tips and tutorials..

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.

Comments

Popular posts from this blog

While Loop in python : How to become a Python Jedi - Beginners Python Programming Tutorial - Part 6

Welcome back friends to another post on TheCodingProject and we are back with the sixth chapter of the Beginner Python programming tutorial - How to become a python Jedi. This time we are going to learn how to use while loop in python.
The While Loop statement? In Python Programming, There are 3 types of loops →
While loop For loop Nested loops
Now to get into some action→
Let’s consider that two groups of aliens have landed on earth  out of these two one of the group is good & the other are the bad guys. The govt. Has given you the task of designing a system that takes in input from different eyewitnesses & then classify that whether the aliens that these eyewitnesses encounter are the good guys or bad guys.










Copy & paste the above code in your editor and run this code. What do you get ? When the input is Autobots it matches the condition aliens == "Autobots" & it enters the loop to prints the statement pointed by the print method.
We can also use negative conditions…

How To Install Tensorflow On Windows

Why I am writing this tutorial ?
Recently I have started learning machine learning specifically DeepLearning for one of my pet projects, and during this quest of mine I wanted to learn TensorFlow library which is developed by Google. Learning is one thing but first I needed to install the library, but there was this problem which I faced during the installation. I am using Anaconda as my package manager and trying to install the Tensorflow library on my work computer which is behind a proxy, installing directly from the anaconda cloud while behind a proxy always displayed a proxy error.

How did I resolve the issue? Well this is exactly what I am going to write down in the below tutorial. The Steps To Follow
Before we get all cosy with this short tutorial on Tensorflow installation, let me tell you that
this tutorial is on the assumption that you have already installed Anaconda on your machine and you know at least a few basic things or two about python.
If you want to learn basic pyt…

For Loop in python 3 : How to become a Python Jedi - Beginners Python Programming Tutorial - Part 7

I am back again with another part in the beginner python programming tutorial series. We will continue with "Loops" but this time we are going to learn how to use for loop in python 3. 


Difference between For loop and While Loop
A "For Loop" and "While Loop" have a common connection that both are looping statement, but major difference is that unlike while loop, a for loop gets executed a specified number of times.



We can break down the For loop into the following components(Refer to the for loop flow chart below).
for keywordvariable namecall to the range functioncolon operatorclause or block of code Hovering over the flow-chart
The above flow-chart is..well horrible I know, but I make my own drawings because I can't afford an artist. So, bear with me. In the above flowchart is a very basicy, laymanish representation of a for loop. The decision box contains the For statement along-with it's soulmate i.e the Range function. As long as the variable I …