0% found this document useful (0 votes)
69 views

Global and Local Variables

Global variables are defined outside of functions and can be accessed inside functions. Local variables are defined inside functions and are only accessible within that function. To modify a global variable inside a function requires using the global keyword, which tells Python that the variable being assigned is global in scope rather than local. Global variables can be used by any code both inside and outside of functions, while local variables are only accessible within their defining function.

Uploaded by

Albin P J
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Global and Local Variables

Global variables are defined outside of functions and can be accessed inside functions. Local variables are defined inside functions and are only accessible within that function. To modify a global variable inside a function requires using the global keyword, which tells Python that the variable being assigned is global in scope rather than local. Global variables can be used by any code both inside and outside of functions, while local variables are only accessible within their defining function.

Uploaded by

Albin P J
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

GLOBAL AND LOCAL

VARIABLES
GLOBAL
Global variables are the one that is defined and declared outside a function and we
need to use them inside a function. 
GLOBAL VARIABLES

Variables that are created outside of a function are known as global variables.
Global variables can be used by everyone, both inside of functions and outside.
Example
Create a variable outside of a function, and use it inside the function
x = "awesome"

def myfunc():
 
print("Python is " + x)

myfunc()
x = "awesome"

def myfunc():
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)
THE GLOBAL KEYWORD

Normally, when you create a variable inside a function, that variable is local, and can
only be used inside that function.

To create a global variable inside a function, you can use the global keyword.
def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)

You might also like