In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to achieve local variables and global variables in python, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Local variable Local Variables
Enter:
#! / usr/bin/python
# Filename: func_local.py
X = 50
Def func (x):
Print ('x is', x)
X = 2
Print ('Changed local x to', x)
Func (x)
Print ('x is still', x)
Output:
X is 50
Changed local x to 2
X is still 50
Explanation:
The function of this example is to output the value of the variable x.
The first x is defined externally, and the second x is defined inside a function. The function of the function is to output the value of x first, then make a new definition, assign a new value to x, and then output the value of x.
Call this function to execute, the initial value of x is 50, the output statement inside the function changes x to 2, and the output is 2.
In another output statement outside the function, the value of x is still the initial value of 50.
It shows that the variables outside the function are global variables, and the scope is in the global environment, while the x inside the function is a local variable, which can only be changed within the function.
Global variable global Variables
Enter:
#! / usr/bin/python
# Filename: func_gobal.py
X = 50
Def func (x):
Global x
Print ('x is', x)
X = 2
Print ('Changed gobal x to', x)
Func (x)
Print ('Value of x is', x)
Output:
X is 50
Changed global x to 2
Value of x is 2
Explanation:
The difference between this example and the previous example is that inside the function, the variable x has an extra declaration, gobal x, which means that the x inside the function is now not a local variable, but a global variable.
As you can see in the output result, the value of x in the output statement outside the function changes after the declaration is added.
After reading the above, do you have any further understanding of how to implement local and global variables in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.