Novice help needed functions

Dear All,
Apologies for the post, but I am new to Python and apart from getting seriously disheartened with the Dataquest course because of constant errors and no help from Dataquest (although I have emailed quite a few times), I am struggling with the intermediate section on functions.
I hope that someone out there might be able to help me as my confidence is hitting bottom.
In the explanation regarding Memory Isolation, Dataquest got me to code (although I couldn’t) a def function to show that a variable defined within a function is temporarily held in memory. So when you recall the function, the variable is not defined anymore. E.g.

When print_constant() is called, x = 3.14 is indeed executed, but the quirk is that Python only saves the x variable temporarily . Python saves x into a kind of temporary memory , which is immediately erased after the print_constant() finishes running.

But then the explanation states:
The temporary memory associated with a function is isolated from the memory associated with the main program. The consequence of this is that we can initialize a variable x = 10 in the main program, and then execute x = 3.14 in the body of a function without overwriting the x variable of the main program.

The last part seems to contradict the first explanation and now I am increasingly confused and frustration levels are on the up. It appears to me (looking at the screen grabs Dataquest uses), x = 10 outside the function appears to allow x = 3.14 to be defined inside the function and therefore both x = 10 and x = 3.14 are printed.

Why isn’t x = 3.14 forgotten?

Please help as I want to conquer (or be able to use it more like!) Python which has been the bane of my life!

You can use to visualise how it works to better understand the code.

1 Like

I understand that you are having trouble comprehending the material/code. However, I do not have enough information in terms of code or reference to the mission link to help you. Please provides code and mission link in order for people to understand your post and gives a faster help reply.

Let me add the student success team @Sahil.

Hi Andrew,

Don’t know if that’s gonna help but from what I understand, variables defined inside a function are in an isolated namespace wich means they don’t mix with variables inside the main. I see no contradiction in what you wrote.

For example:


This allows this kind of code:

Here (x=x) means ‘the variable x in the function takes the value of x in the main’. While this is not recommended because of the confusion, this works because they belong in different namespace hence having the same name doesn’t matter.

However, if a variable is called inside a function but is not defined in the function, Python will pick the variable from the main. This is not recommended either because the function then depends on your main and cannot be used in another program. The reverse doesn’t work though (as you’ve seen in the previous examples.

Hope it helps you!


Hi Andrew, not entirely clear of your confusion but hope i can help clarify some of your thoughts.

x = 10 outside the function appears to allow x = 3.14 to be defined inside the function

What variable is defined outside the function scope does not restrict what is defined inside a function, so i won’t say it “allows” x=3.14 inside. You can have no x=10 outside and still have x=3.14 inside. The names don’t clash because they are in different scopes. You can read about LEGB rule of how python resolves variable names.

therefore both x = 10 and x = 3.14 are printed.

I assume you are running an entire program and not line by line?
You seem to be inferring that x=3.14 is not forgotten, that it exists because both values of x were printed?
It’s impossible to know where the program is when printing those values just by looking at the output. (unless you really know the language well, or place specific identifying print statements at important branches of the code). Yes that x existed at some point in the program, but it would be more accurate and helpful for understanding by using a REPL tool like terminal or jupyter notebook that allows you to execute items line by line so you always know which state you are in and the variables in a function would be removed by the garbage collector once you call and return from the function and become undefined (assuming you did not return the variable from the function and assign to a variable of the same name in the namespace that called that function)

Hello All,

Thanks for your help as it is very much appreciated.
The pictures with the code is:

I suppose I got myself into rather a state yesterday with trying to understand - if I cannot understand something as fully as I can, I get very angry with myself and the situation! However, I have never got on with programming and I really want to.

From the picture the top left screen shows the NameError because x is inside the function and so it says that it’s non defined. Looking directly under to the next picture you get x = 10, then the function and the output executes the function which prints the two values of x. I do not understand how the x = 3.14 is printed when its inside the function and the main scope defined x = 10 should be the assigned value. I’ sorry for being stupid on this, but I cannot understand how a variable inside and outside a function can do this.


I hope this also will help to clarify my issues!
I have just read the concluding part on memory isolation and where Python looks for variables, that makes sense. However the following is in the prose:

The top shows the code with assigned variables in the main and the variables being re-assigned in the function. From my understanding, Python looks inside the function to find the variables it needs, if it does not find them there, it will then look in the main. However, the bottom code shows x = 3.14 defined in the function without an x defined in the main, and throws up an error - cannot compute!

Code snippet 1A:

def print_constant():
     # x is a local variable within the function print_constant
     # Once outside of the function, the runtime program does not 
     # have access to the x.   
     x = 3.14

# At this line, we are outside of function due to indentation of the code. 
# print and def print_constant are on the same indentation column. 

# We are outside of the function, the run time program has no access/scope to 
# the local variable/label x within def print_constant.
# There is no global x declared before x is being called within this script. 
# Hence, the error result in x is not being defined. 

In the above code snippet 1A, x = 3.14 has only local scope to the function print_constant. Once outside off the function, there is no x defined.

To fix the problem in code snippet 1A, simply remove x on the last line or see below at code snippet 1A-Fixed.

Code snippet 1A-Fixed:

def print_constant():
     # x is a local variable within the function print_constant
     # Once outside of the function, the runtime program does not 
     # have access to the x.   
     x = 3.14


# The line was removed to fix the error. 

Code snippet 1B:

# x is a global variable/label with value 10
x = 10

def print_constant():
     # x is a local variable within the function print_constant
     # Once outside of the function, the runtime program does not 
     # have access to the x.   
     x = 3.14


# Outside of the function print_constant scope, 
# x takes the value of global x
# x has a value of 10

In the above code snippet 1B, on the last line, since the line is declared outside of the function scope, the x value will take value of the global label/variable x, which is 10.

Hello and thank you Alvin, however my confusion continues I’m afraid!

I understand the indentation remarks you made, only doesn’t typing print_constant() in Python then make the program read over the code to find print_constant and then execute it?
This is where I will have a lot of trouble if it doesn’t do that. To me, the program should execute the function, which holds the variable of x inside. It should then act upon the instructions implicit to the function and return x = 3.14. I take it that something is wrong with my thinking?

1 Like

I am sorry, but I do not follow your question.

What do you meant by this?

It should not execute according on how you interpret it but according Python interpreter.

The main concepts about your misunderstanding is global variable versus local variable. The limitations of the variable/label scope is what differentiate global and local variable.

The code snippet 1A:

When you call a function, its like you are going into a house. Then you saw that the house’s x = 3.14. Once you exit the function, it’s like you are exiting the house and no longer have access to what’s inside of the house.

You can’t access the house’s item without being inside of the house.
That is, you can’t access the function local variable x, once you exit the function.

Here I am asking when you type print_constant() on a new line (not at the same indentation as the function), do you run/call the function called print_constant()?

The other two statements continue from here!

As long as you execute a function call, regardless of the line at which it is indented, the function always executes provided that the function is valid during run time.

When I say indentation, I am trying to show you the differences between global and local variable in terms of scope.

Watch out for whether a function returns something or just prints it, especially when writing your own. A function without a return statement will automatically return None. Similarly, a function with only the word return will return None.

Good morning!

Thanks to all for trying to explain what can be considered by most as something fairly simple and I state that I have now found out the problem and it was the way the explanation was made by Dataquest - and to be frank, I am not happy about it.

I have just typed in:

def print_constant():
       x = 3.14

and got what I expected to see:

now the ‘example’ is as follows:

def print_constant():
       x = 3.14


And having the little ‘x’ on its own means that there is no value assigned to this ‘x’ only inside the function, so the program read the function but returned the error due to the lone ‘x’ outside the function. Being an ex-educator, this example could have done with a little more clarity to it to remove confusion.
I now understand what was wrong with the example and I would like to thank people for attempting to help me understand.

I have edited your post to be more readable. Use back ticks to format the python code.

def print_constant():
x = 3.14

add back ticks above and below your code block

def print_constant():
x = 3.14
to transform to below’s code

def print_constant():
       x = 3.14

or you can click edit button on your post to see how I format your code.