From the course: Programming Foundations: Real-World Examples

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Local and global variables

Local and global variables - Python Tutorial

From the course: Programming Foundations: Real-World Examples

Start my 1-month free trial

Local and global variables

- While I'm in the process of cooking something, I like to keep the things I'm working with close at hand. In my local area. Functions do the same thing when they execute by keeping their input parameters and any variables which are created inside of the function within that function's local scope. When I want to use a specific resource, I'll look for it first in the local area. So, if I want to add some bacon to my omelette, I'll look for it near by. Here it is. So we'll add that. Now, if I want to add some cheese to my omelette, I'll start by looking for it in my local scope. But I don't seem to have any cheese here so I'll expand my search to look in the global scope. And in this case, that's my kitchen. So, I'll go get some cheese from the fridge. Oh! - Ooh! Bacon! Can I have some? - Oh no, no, no, no. You can't touch that bacon. That bacon is my input ingredient, which means it's in my local scope. Only I can use objects that are local to my function while I'm executing it. If…

Contents