Python's global variables and local variables

Global definitions are generally capitalized

Global definitions are generally capitalized
APPLE =1
a = None
def fun():
global a
a =2return a +100print(APPLE)print(a)print(fun())print(a)

zbx@zbxpc:~$ /usr/bin/python3 /home/zbx/desktop/mofan_python.py
1
None
1022

If the a in the custom function wants to become a global variable, you can use global, and you also need to define a outside;

Note: the a before print(fun()) is a local variable of the function, and the one printed after running is a global variable.

Recommended Posts

Python's global variables and local variables
Python's history, advantages and disadvantages
Ubuntu environment variables and ADB configuration
Python's exception concept introduction and handling
Python's jieba module installation and download