Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Functions in Python - Python Functions - Scaler Topics

The document discusses local variables in Python functions, which are variables declared inside the function body that can only be accessed within the function and not outside of it, while global variables declared outside the function can be accessed inside and outside the function. An example shows that when a local variable and global variable have the same name, the local variable takes precedence inside the function and the global variable is accessible outside the function.

Uploaded by

pptdownload81
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Functions in Python - Python Functions - Scaler Topics

The document discusses local variables in Python functions, which are variables declared inside the function body that can only be accessed within the function and not outside of it, while global variables declared outside the function can be accessed inside and outside the function. An example shows that when a local variable and global variable have the same name, the local variable takes precedence inside the function and the global variable is accessible outside the function.

Uploaded by

pptdownload81
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Academy Data Science Neovarsity

Sign in

outside of the Function.

Example:

x = 5 

def f1():
print("x in =", x)

f1()
print("x out=", x)

Output:

x in =5 
x out=5

Live Events
Local Variables
Spark for Dat…
A variable declared inside the Function’s body is called a local variable. Such a Amit Singh
variable is said to have a local scope, i.e., it is only accessible within the Function 2
and not outside.  1778 January 3.00

2023 | Hrs
Example: 8:00 PM

 Register with 1-
def f2():
Click
y = 3

View All Events


f2()
print(y)

Output:

NameError: name 'y' is not defined 

Example: Global and Local Variables with the Same Name

x = 15 

def f4():
x = 200
print("Local x:", x)

f4()
print("Global x:", x)

You might also like