How to use a global variable in function?

0 votes
How do I create or use a global variable in function?

Is it necessary to store the global variable in a local variable of the function which needs its access?
Sep 27, 2018 in Python by ana1504.k
• 6,630 points
16 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The global variable can be used in other functions by declaring it as global in each function that assigns to it.

It can be used as such :

globvar = 0

def set_globvar_to_one():
    global globvar    
    globvar = 1

def print_globvar():
    print(globvar)     

set_globvar_to_one()
print_globvar()     
answered Sep 27, 2018 by SDeb
• 10,700 points

Related Questions In Python

0 votes
1 answer

How do I use urllib to see if a website is 404 or 200 in Python?

For Python 3, try doing this: import urllib.request, ...READ MORE

answered Nov 29, 2018 in Python by Nymeria
• 3,500 points

edited Dec 11, 2018 by Nymeria 181 views
0 votes
1 answer

How to use Pandas HDF5 as a Database in Python?

HDF5 works fine for concurrent read only ...READ MORE

answered Nov 30, 2018 in Python by Nymeria
• 3,500 points

edited Dec 10, 2018 by Nymeria 61 views
0 votes
2 answers

What is a Tuple in Python and how to use it?

  Tuple is a collection of Python objects.it is ...READ MORE

answered Apr 12 in Python by rajesh
• 1,210 points
67 views
0 votes
1 answer

How to run a Function in Spyder and check output?

You will see the output of the ...READ MORE

answered May 17 in Python by Omkar
• 66,880 points
28 views
0 votes
1 answer

How can we set environment variables from java?

For setting the environment variable, you can ...READ MORE

answered Jun 21, 2018 in Java by Akrati
• 960 points
22 views
0 votes
2 answers

difference between class and instance attributes

Apart from the performance, there is a ...READ MORE

answered Sep 17, 2018 in Python by SDeb
• 10,700 points
24 views
0 votes
1 answer

How can I print variable and string on same line in Python?

Use , to separate strings and variables while printing: print ...READ MORE

answered Sep 17, 2018 in Python by Priyaj
• 56,140 points
37 views
0 votes
1 answer

How does Python know whether a variable in the class is a method or a variable?

In python objects/variables are wrapped into methods ...READ MORE

answered Sep 18, 2018 in Python by ariaholic
• 7,320 points
27 views
0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 28, 2018 in Python by SDeb
• 10,700 points
48 views
0 votes
1 answer

How to use multiprocessing queue in Python?

This is a simple example of a ...READ MORE

answered Oct 25, 2018 in Python by SDeb
• 10,700 points
126 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.