Python global variables and local variables # Python global variables and local variables
# Variables in Python: global variables and local variables # in many languages, when declaring global variables, we all like to define the name of global variables as uppercase # define the number of students as 100STUDENT_NUMBER = 100 # The school name SCHOOL_NAME = 'Guangzhou University '"" \ to get global variables: STUDENT_NUMBER value "" def getStudentNumber (): return STUDENT_NUMBER "" \ reset global variable: STUDENT_NUMBER value "" def setStudentNumber (num ): global STUDENT_NUMBER = num "" \ get global variable: value of the school name SCHOOL_NAME "def getSchoolName (): return SCHOOL_NAME" \ reset global variable: school name SCHOOL_NAME value "def setSchoolName (name): global SCHOOL_NAME = name def changeValue (): name = 'Peking University 'number = 90 print ('Global variable STUDENT_NUMBER = ', getStudentNumber () print ('Global variable SCHOOL_NAME =', getSchoolName ()) print ('##################################### ##') print ('Local variable namber = ', number) print ('Local variable name =', name) print ('##################################### ##') print ('change the global variable value... ') print ('##################################### ##') setStudentNumber (number) setSchoolName (name) print ('Global variable STUDENT_NUMBER = ', getStudentNumber () print ('Global variable SCHOOL_NAME =', getSchoolName ()) if _ name _ = '_ main _': changeValue ()