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

Python For Oil and Gas: Website - Linkedin - Youtube

The document discusses variable scope in Python functions. It defines two functions, one that returns a variable x set within the function, and one that tries to print x. This fails with a name error, showing that x is locally scoped to the first function only. The document then introduces a global variable x and shows how to modify it from within a function using the global keyword, demonstrating the difference between local and global variables in functions.

Uploaded by

LIC Helper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Python For Oil and Gas: Website - Linkedin - Youtube

The document discusses variable scope in Python functions. It defines two functions, one that returns a variable x set within the function, and one that tries to print x. This fails with a name error, showing that x is locally scoped to the first function only. The document then introduces a global variable x and shows how to modify it from within a function using the global keyword, demonstrating the difference between local and global variables in functions.

Uploaded by

LIC Helper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2/2/2021 Python for O&G Lecture 31 - Functions (SCOPE of Variable) - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw

# define two functions here

def function_1():
x = 5
return x

def function_2():
print(x)

/
2/2/2021 Python for O&G Lecture 31 - Functions (SCOPE of Variable) - Colaboratory

# call the 1st function

function_1()

# call the 2nd function

function_2()

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-5b88b5969a89> in <module>()
1 # call the 2nd function
2
----> 3 function_2()

<ipython-input-1-43e40ba878f8> in function_2()
8
9 def function_2():
---> 10 print(x)
11
12

NameError: name 'x' is not defined

SEARCH STACK OVERFLOW

print(x)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-4-fc17d851ef81> in <module>()
----> 1 print(x)

NameError: name 'x' is not defined

SEARCH STACK OVERFLOW

/
# see the difference??
2/2/2021 Python for O&G Lecture 31 - Functions (SCOPE of Variable) - Colaboratory
# see the difference??

# Why this is happening? - scope os x is limited to only function_1

# These type of varibles which are defined inside a function are called as LOCAL VARIABLE

Global Variable

# Introduce the global variable

x = 10 # global

def function_1():
x = 5 # local
return x

function_1()

print(x)

10

x = 10

def function_1():
global x
x = 5 # global x /
t
2/2/2021 Python for O&G Lecture 31 - Functions (SCOPE of Variable) - Colaboratory
return x

function_1()

print(x)

x = 10

def function_1():
global x
x = 5 # global x
return x

print(x)

10

function_1()

print(x)

/
2/2/2021 Python for O&G Lecture 31 - Functions (SCOPE of Variable) - Colaboratory

You might also like