How to define global variables in Python.
In Python, you can use the global keyword to define global variables. Here is an example code for defining a global variable:
# 定义一个全局变量
global_var = 10
def function():
# 使用全局变量
global global_var
global_var += 5
print(global_var)
function() # 输出15
By using the global keyword inside a function, you declare a variable as a global variable, allowing you to modify the value of the global variable within the function. It is important to note that when modifying a global variable inside a function, the global keyword must be used to prevent the creation of a new local variable.