declare

Python 3 Programming Tutorial - Global and Local Variables



Sharing buttons:

what is going on everybody welcome to

another Python 3 basics video in this

video we're now going to be discussing

the concepts of global and local

variables so when users start using

functions they quickly get a little

confused

when it comes to global and local

variables and they get this you know

dreaded you know variable not defined

even when they clearly see that it is

defined or so they think

so these terms of global and local

corresponds to a variables reach within

a script or a program so a global

variable is a variable that can be

accessed anywhere a local variable

opposite means it can only be accessed

within its you know environment or its

frame so a local variable cannot be

accessed globally inherently anyways and

a global variable can be right but

you've got to turn it into a global

variable and some people have a

misunderstanding of what a global

variable actually is so let's go ahead

and give some examples so if you if you

haven't committed this to memory no

problem I think actually seeing help

centers helps you to understand so x

equals x this variable has no parent

function but it's actually not a global

variable it just so happens that it's

committed to memory before any sort of

function or anything is called so for

example we can say define example and we

can actually say you know Z equals 5

print Z and that's totally fine and we

can call example and as we expect it's

going to print 5 so we know that works

but what starts giving trouble to people

is instead of let's get rid of this and

let's say you want to print X well we

run this function and we see we can

print X so we were able to access X from

this function and

what happens is users naively think that

because they were able to access it now

they're able to play with this variable

led site so print X plus five okay so

they start playing with it they see okay

well we can start modifying it and all

this cool cool cool so we have you know

access to this but the problem is when

they start to do something like this X

plus equals six and they go to run it

mmm and you see unbound local error

local variable X referenced before

assignment yet this makes people just

pull out the hair because first of all

they never got there they didn't get the

things printed so they're freaking out

like so they didn't get the you know

confirmation here because when you get

rid of this it does print him it's only

you only get that error whenever you try

to actually do something like add six to

that variable and so they start pulling

out their hair because up till now they

thought they were getting away with

doing that but the problem is X is not a

global variable so there's a few ways

that you can get around this obviously

the easiest thing to do would be to

convert X to a global variable so to do

that you type out global space X and

what that does is it's notifying Python

that hey guys X is actually a global

variable and so now what we can do is

print X and then we can even do X plus

equals five for int X so now we go to

run this sure enough six eleven

everything worked we were able to use

our global variable now that's it for

global variables but a lot of teachers

you're going to find or maybe you work

for a company or something and the CTO

says nobody gets to use global functions

or I mean global variables don't use

them though some people don't like the

idea of using global variables there's

actually you know a couple of things you

can do to get around this one one way

that people will do it is the following

they might say globe globe x equals x

because we already know we can access

X we just can't modify it so then you

could print globe X and then we could do

globe X plus equals five and print X and

we're able to do that six eleven so we

get the same output that we wanted but

maybe we're not happy with that because

when we do this now we've changed

variables here so and also globe X is a

local variable and so we're not able to

access globe X outside of this function

so say we've got multiple functions and

we want them to work together we're

going to be able to do that so what do

you do instead well generally what

you're going to see is instead you'll

get everything up to this point no

problem but instead what we'll have what

users will do is they're going to return

globe X with example so when this

function is run it's going to return a

value and what this means is it's almost

like when you do five plus five in

Python what five plus five does is it

returns ten and so by that same token

what we can do is if you wanted to set a

value to five plus five or set a very

five plus five equal to a variable what

you would do is you would say maybe x

equals the function of example and now

print oops print X save and run that we

see that we were able to do that so this

is one way that you can actually end up

modifying this and within the main loop

let's say without using global variables

because again some people really just

are very anal about global variables so

hopefully that helps some of you guys

out especially with the whole accessing

this local variable but then not being

able to modify it so hopefully save you

guys from pull out your hair this is

something that did give me some trouble

when I was starting out so well if I

save you guys some of that so hopefully

you guys enjoyed as always thanks for

watching things for all the supporting

subscriptions and until next time