Python Variable Range

Source: Internet
Author: User

1, local variables, global variables

Python has 2 variables scoped to local variables, global variables.

Variable search path is: local variable, global variable

They simply mean that the value of the local variable is valid only within the local scope. The scope of global variables is global.

For example:

  

A = 3      def  px ():    = 4    print  (a)print  (a) PX ()

Print (a)

The result of the previous segment of the code is 3 4 the A in the 3,PX function is only a local variable, which is only valid in the code block in which it resides. Can't change the value of a outside

But with global, we'll see:

A = 3def  px ():    Global  a            = 4    print  (a)print  (a) px ()print (a)

When you add global, the variable a becomes a global variable, and its scope is global. Modify it to take effect in the global scope

Python Variable Range

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.