Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right.
Let's play a little game.
Can you guess what the output of this program is going to be I have over here a close one.
Then we create a function called confusion where we have a equals to five and we return.
Hey my question is when we print a and when we print confusion What do you expect to happen here.
Take a second to figure out.
Pause if you need to and let's find out
one in five.
Is that what you expected.
Let's go line by line.
First we assign a value of 1 to variable a.
We then create a function called confusion and Python interpreter is going to say all right.
Confusion function.
I know what it is.
I'm going to put it in memory when you need to use me.
Just let me know.
And then it's going to go to line eight and line eight is going to say hey print a what's a.
Well the only time that I've seen a is right here A equals one.
So go into memory what's a equals one.
But then when I run confusion like this I'm going to say hey a is going to equal to five and now we
return eight which is five.
OK what if I change the order of this.
What if I go like this.
What will happen if I click Run.
Is that what you expected
once again.
We have a close one.
Then we define the function and then we print confusion now.
Confusion here is going to assign a equal to five and then return a but why didn't this change.
Why is a still equal to 1 by the time we get here.
And this is because of scope.
Right what the Python interpreter does is when it runs the confusion function it's going to say hey
a is going to equal to 5 but I'm creating a variable here called a in my own universe.
I don't know anything about a equals one here.
I have my own universe.
And after we run this function then while we're done we've returned the function and it's completely
gone but we never left the universe into the global scope so there's a set of rules that the Python
interpreter goes through to check a variable.
And the rules are this first it's going to check with the local.
So start with local and what we call is a local scope a local scope is a scope.
That's part of this local function.
So a is a local scope.
It's going to check there.
Hey do I know the variable A BOAT.
YEAH.
WHEN I RETURN A I have a variable defined here.
So it starts there.
But let's say a wasn't there.
Let's say I remove a and I click Run here I get both once because the second rule is if there is nothing
in the local variable or local scope then number two is there a parent local scope in our case.
What's the parent of this function.
Well it's the global scope but we can also do another function let's say parent and parent is a parent
of the confusion function.
And in here let's say a equals to 10 now in here we'll return confusion.
Just so we can use it again and actually run it.
So let's call parent
and click Run.
All right let's go through this.
So it's not too confusing.
We have their parent function that gets called.
So when we call that we assign a equals to 10 and then we also create a new function confusion.
And then finally we return confusion and actually run it.
So we're going to return a.
And then in here print whatever the value of a is which is 10 and why is that.
Well because we first start with the local scope.
Hey does confusion know what a is.
No I don't have it in my local scope.
OK.
Does my parent local scope have it.
So it's going to go up a level and say Hey does parent function know what it is.
Yep.
I have a equals to 10.
So it's going to print that.
The third rule or the Third Order is global and global is what we call the indentation of nothing.
It's this file.
Right.
Whatever the File has that is global.
In our case if a equals to 10 doesn't exist here there's no local there's no parent local and I click
Run it checks the global scope
and then finally there's one other rule and this is number four and it's a tricky one.
And this is what we call built in Python or built in Python functions so Python comes with predefined
functions such as well the sum that we've talked about before this some let's say just give it five
is going to check or actually let's not give it anything let's just do some here when I run this I get
built in function some.
Now how do you know what this was.
What this variable at the end of the day was.
Well it because it said hey do I have some inside of this universe.
No I don't.
OK.
Do I have some in my parent scope.
No I don't.
OK.
Do I have a sum in global.
No.
There's nothing some in here and then the final thing it's going to check is hey is some some sort of
a built in python function.
Yep.
Python actually gives us some.
So you can use it so it doesn't error out and this is the scope rules that the Python interpreter falls.
Very very cool.
There's still a few more things that we need to learn.
So I'll see you in the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.