Skip to playerSkip to main contentSkip to footer
  • 5/21/2025
Python utilizes a scope resolution rule known as LEGB, which dictates the order in which Python searches for variable names:
Local (L):
This scope encompasses names defined within the current function. If a variable name is found in the local scope, Python uses that value.
Enclosing function locals (E):
If the name is not found locally, Python searches the scopes of any enclosing functions (in nested functions).
Global (G):
If the name is not found in any enclosing scopes, Python looks in the global scope, which includes names defined at the top level of the module.
Built-in (B):
Finally, if the name is not found in the global scope, Python checks the built-in scope, which contains pre-defined functions and constants.

If a variable is assigned a value within a function, it is generally considered local to that function, unless explicitly declared as global using the global keyword. Variables defined outside of any function are considered global and can be accessed from within functions, although their values can only be modified within a function if declared global.
Transcript
00:00that we have to take rules
00:02last lecture
00:04we have to teach
00:06that x is equal to 1
00:08this is not equal to
00:10let's say
00:12a variable function
00:14and here
00:16if I define
00:18x is equal to 2
00:20and return to x
00:22these two variables
00:24this is not one variable
00:26this is not one variable
00:28this is not one variable
00:30this is not one variable
00:32this is not one variable
00:34this is not one variable
00:36okay
00:38if I print
00:40x
00:42print
00:44let's say
00:45func
00:46I think
00:48same value return
00:50or different value return
00:52execute
00:54voila
00:56two variables
00:58treat
01:00now
01:02we will see
01:04what rules
01:06that
01:07this variable
01:08is
01:09this variable
01:10is
01:11and
01:12what principle
01:14is
01:15that
01:16python
01:17check
01:18variable
01:19check
01:20where
01:21do
01:22where
01:23do
01:24now
01:25I have a
01:26concept
01:27expand
01:28let's say
01:29variable
01:30and
01:31name
01:32iphone
01:3310
01:34on
01:35road
01:36variable
01:37string
01:38and
01:39name
01:40iphone
01:41on
01:42road
01:43on
01:44my home
01:45my home
01:46my home
01:47my home
01:48this is
01:49that
01:51phone
01:52iphone
01:53iphone
01:54on road
01:55but
01:56this is
01:57on
01:58home
01:59home
02:00this is
02:02home
02:03in
02:04iphone
02:05road
02:06on road
02:07from
02:08return
02:09phone
02:10Okay, now, as I gave you a example, the variable, the scope of a house, the other thing is accessible to the house, the other thing is accessible to the house, but the other thing is accessible to the house.
02:25However, if I print the phone again, I call this function, my home, and let's see what we get.
02:39Run.
02:40Before we get the phone on the road, the phone on the home.
02:43Interesting.
02:44Okay, now I have to think that if I put it on the phone, we will get the same result, phone on the home and phone on the road.
02:55Imagine that you have a phone
02:58and you have a phone misplaced.
03:02If you have a phone, you don't exist.
03:04You don't have a phone.
03:06So, where do you see that phone?
03:09Of course, if you go back and see that
03:11that you don't have a phone.
03:13So, if you go back and see that phone,
03:16then you have a phone.
03:19Look, I have removed this phone.
03:21You have a phone.
03:22So, I have a phone.
03:24I have a phone.
03:26You can see that you have a phone on the phone.
03:28You have a phone on the phone.
03:30So, I have returned it on the phone.
03:33Interesting.
03:34Now, I have to go back and see that
03:36if I have a function
03:38wrap up.
03:40I have a function.
03:42Def parents home.
03:45It is a function.
03:46It's a function.
03:48It's a function.
03:50My home is in my parents' home.
03:53Okay?
03:54My home is in my parents' home.
03:56Now, basically,
03:58my phone is in my parents' home.
03:59I will say that
04:01my phone is in my parents' home.
04:03is on home and this is on parents home, parents, on parents home,
04:16home.
04:20Now, if you have a phone in your house, if you don't have a phone,
04:27if you don't have a phone in your house,
04:36then if you don't have a phone in your house,
04:40then you can see a phone in your house and see how you can use them.
04:47Now, when I call me, Nobody is wearing the phone in my house.
04:49reason is that it is return not to me so I will return my home
04:58return my underscore home so I will return my home
05:03my home function call sorry parents function call
05:06Okay my bad guys, I have parents spelling and function call, so I close this function
05:18and call it. I hope that you can see it again. Interesting. Now look at this.
05:27Basically, this is a memory address show, and this is a memory address.
05:33This is a memory address. This is a memory address.
05:37Basically, we have to do this function call.
05:41Now look at this function.
05:43Now look at this function.
05:45I need a variable to find a function.
05:49Now look at this method.
05:51First of all, I can see my home, my function.
05:55If my phone is a phone, I can see my parents' house.
05:59I can see my parents' home.
06:01Remember that this function is a nested function.
06:03The function inside function is a function.
06:05I can see my simple terms.
06:07I will not use this function.
06:09I will not use this function.
06:11This example will be ingrained in your mind.
06:13If you don't have a variable in your home,
06:15then you can see your parents' home.
06:17Now look at this function.
06:19Now look at this function.
06:21There is a variable in parents' function.
06:23And that is iPhone 10 on parents' home.
06:25So you can see your apps display on iOS 10 on account.
06:27So you can see my phone on the road display.
06:31Okay.
06:32So I run this function just to remove the confusion.
06:36Now I'll run this function.
06:38You can see my parents' home.
06:41Now I will remove this function.
06:43Now I can see that your phone is your home,
06:46or a parent's phone is your home.
06:48I can see your phone in a while.
06:52Then I will say your phone is your home.
06:53I will see that iPhone on the road.
07:23I will see that iPhone on the road.
07:38I will see my home on the road.
07:40I will see my home on the road.
07:44My home on the road.
07:49If I haven't met my home on the road,
07:51I will see it.
07:52I will see it in my parents.
07:55If I haven't met my parents,
07:59I will see it in my home.
08:01If I haven't met my home on the road,
08:03I will see it in my home.
08:05What is the principle?
08:07I will see it in my home.
08:09The principle is that Python starts with
08:15The principle starts with
08:22The principle starts with

Recommended