Skip to content

Python global variable across functions

  • by

Python global variables can be across functions because global variables are defined outside any function. You can access and modify this variable by any function in the program.

Python global variable across functions example

Simple example code defining a global variable and using it in multiple functions.

count = 0


def increment():
global count
count += 1
print("count: ", count)


def decrement():
global count
count -= 1
print("count: ", count)


def print_count():
print("Final count: ", count)


increment()
increment()
decrement()
print_count()

Output:

Python global variable across functions

Do comment if you have any doubts or suggestions on this Python global variable topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *