Scope in Jupyter notebook

Suppose i have defined a variable “Age” as 20 in cell #1 and i am calling the variable Age to print its value in cell #3 it is showing Name error, and it says, " name ‘Age’ is not defined". So the scope of the variable i am defining in a cell is restricted to that cell itself and outside that cell it is showing not defined. Please help.

How was this variable defined?
Variables defined inside functions only have local scope inside the function. They are not available outside that function.
Otherwise, variables declared globally should be available in the cells after.

It would be better if you post a screenshot of the same here. It would be easier to highlight the error.


Please Help

Yes, the variable was declared globally only

I suppose you didn’t run the cell which had name =“Ankit”. That’s the meaning of the error. You didn’t send the ss of the code due to which the error came. (i.e. name=“Ankit”).

As a reference, I would send this to you. Maybe you can figure out the error from this.

1 Like


Sorry i did not include the full coding in the ss. Have a look at this ss and please help.

You have not executed the three cells i.e. name, age, has_android_phone. Because until and unless you don’t execute it, Python will not store the variables in memory. So, it’s necessary to execute the 3 cells as mentioned above and then execute the 4th cell.

That will surely solve the problem.

1 Like

Execute the cells in order.

1 Like

Oh great!!!Done…Thank you so much Aditya