Local variables and global variables

Source: Internet
Author: User

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #author:james Tao4 5 #Global variables: variables defined at the first level, at the beginning of the program, are called global variables, scopes: the entire program6School='ABC'7 8 #local variable: A variable defined in a subroutine is called a local variable, scope: A subroutine that defines the variable9 defchange_name (name):Ten     GlobalSchool#change global variables in a function and use as little as possible OneSchool='DEF' A     Print('before change', Name,school) -Name='Tao'#name only takes effect in the function, only the number, string in the local cannot change the global -     Print(' after change', Name,school) the  -Name='James' - change_name (name) - Print(name)#When a global variable has the same name as a local variable, local variables work within the subroutine that defines the local variable, and other local global variables work + Print(school) -  + #In addition to numbers and strings, lists, dictionaries, collections, and classes can be changed locally in the global Anames=['Jmaes','Kobe','Jordan'] at defchange_list (): -names[0]='Tao' -     Print(names) - change_list () - Print(names)

Results:

Local variables and global variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.