36 Examples of getting started with Python-14 local variables and global variables

Source: Internet
Author: User

#014 <br/> # example where only variables in the function can be modified: <br/> def poweronlocal (appleeaten): <br/> Print 'apple is eaten! ', # Note, here, the comma is used to prevent automatic line breaks in python <br/> appleeaten = true <br/> # End of Def <br/> appleeaten = false <br/> poweronlocal (appleeaten) <br/> If appleeaten = false: <br/> Print 'haha! And apple! '<Br/> else: <br/> Print ~ Woo ~ Woo ~ '<Br/> # example of modifying global variables using the function: <br/> def poweronglobal (): <br/> global appleeaten # Catch a guy named "appleeaten" on the global page. <br/> Print 'apple has been eaten! ', <Br/> appleeaten = true <br/> # End of Def <br/> poweronglobal () <br/> If appleeaten = false: <br/> Print 'haha! And apple! '<Br/> else: <br/> Print ~ Woo ~ Woo ~ '

 

Output:

1
2
apple is eaten! Haha! And apple!
Apple has been eaten! Woo ~ Woo ~ Woo ~

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.