E-Learning

Learn Python – How To Use a Global Variable Defined in Another Function

 
Within Python, as a programming language, there are different types of variables. At this article, we’ll see a brief explanation about them and show you how to use a global variable inside a function.
 

Definition of global variable in Python

global variable in Python is one that is not defined within the scope of the function that is using it. For example:

variable_global = 0
x = 42
v = 1

 

Definition of local variable in Python

When you set the value of the variable within the function, it is a local variable. An example of a local variable is shown below:

def f():    
 v = 10 
print v

This local variable can only be used after it has been defined within the function. If you try to use it before it is inserted into the code, an error message will happen:

def f():
  print  v
 v = 10
UnboundLocalError: Local variable 'v' referenced before assignment.

 

Using a global variable in a function:

To use a global variable within a function, you must declare it as global within that function.

v = 1
 def f():    
  global v     
print  v

This code above allows you to use the global value within the function, without actually having to declare its value within the function. One difference between the global and local variables in Python is that the global variable can be declared after the function that will use it, without happening an error like the mentioned before. This can be seen in the example:

def f ():     
 print  x  
x = 42
42

You may declare a value for the global variable in the function. This causes the value of this variable takes the value written within the function, from this point on in the code.

x = 3
 def f (): 
  global  x     
   x = 1 
print  x 
'1'

Attention: Be careful when doing this, because the control of these value changes can be very complex.

If you know more ways how to use a global variable within a function, share it in the comments area below.

Now that you found the answer to this question, and want to explore other ones, you can check our videos on Python programming. Below are some examples:

 

You can also subscribe to some broadcasters that streaming videos about Python, such::

 

emilyanndd

 

 

thenewboston

 

 

Another cool way to find out interesting things about Python is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

3 weeks ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

2 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

4 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

7 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

8 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

9 months ago