


1. Global variables and local variables
These two variables are the same as variables in other languages. Global variables are simply variables that can be used throughout the code. Its scope is the entire function, and Local variables have a limited scope, often within a code area.
It should be noted that if you just call the value of the global variable in the function without changing its value, there is no problem at all, but if you want to change the value of the global variable in the function without special processing, The Python language handles this situation by automatically generating a local variable with the same name as the called global variable, which means that the global variable is shielded, and operations on the variable will not affect the value of the global variable. (Although it looks like it has changed)
For example, in the following program, although the value of count is changed in the function, the value of count printed outside the function is still 5.
count=5def Myfun(): count=10 print(count) Myfun() print(count)
If you have to modify the value of a global variable inside the function, you can use the keyword global to modify the variable inside the function. This means that the operation is an operation on the global variable, rather than generating an Global variables are the same as local variables.
print("======使用global之后的变量======") count=5def Myfun(): global count#声明与赋值不能一块进行 count=10 print(count) Myfun() print(count)
2. Embedded (internal) function
In short, an embedded function is a function defined inside the function
It is worth noting that the internal function can only be called outside of it Function call, but cannot be called outside. In other words, who has the right to use it within who owns it.
print("======内部函数的使用======")def fun1(): print("fun1()正在被调用") def fun2(): print("fun2()正在被调用") fun2() fun1()
3. Closure
A closure is the parameter used by an embedded function to call its external function.
You need to pay special attention when calling this function.
def funX(x): def funY(y): return x*y return funY print(funX(5)(8))
4. Variable problems in closures
The following code will report an error when executed. Because the parameters of the external function are called inside the embedded function, and the parameter x is a global variable for the function Fun2(), due to the shielding effect, the function error occurs.
def Fun1(): x=5 def Fun2(): x*=x return x return Fun2()#Fun1()
There are two ways to solve the above problem:
One is to use non-stack data structure to solve the problem
The second is to use the nonlocal keyword to solve the problem
#一种解决办法就是使用非栈存储,使用序列等来存储def Fun1(): x=[5] def Fun2(): x[0]*=x[0] return x[0] return Fun2() print(Fun1(),"\n")#在一种解决办法就是使用nonlocal关键字def Fun3(): x=5 def Fun4(): nonlocal x x*=x return x return Fun4() print(Fun3())
You need to think carefully about the variables in the function. After all, there are some differences from what you learned before.
The above is the content of the seven variables and built-in functions of Python's zero-based introduction. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

NumPyarraysarebetterfornumericaloperationsandmulti-dimensionaldata,whilethearraymoduleissuitableforbasic,memory-efficientarrays.1)NumPyexcelsinperformanceandfunctionalityforlargedatasetsandcomplexoperations.2)Thearraymoduleismorememory-efficientandfa

NumPyarraysarebetterforheavynumericalcomputing,whilethearraymoduleismoresuitableformemory-constrainedprojectswithsimpledatatypes.1)NumPyarraysofferversatilityandperformanceforlargedatasetsandcomplexoperations.2)Thearraymoduleislightweightandmemory-ef

ctypesallowscreatingandmanipulatingC-stylearraysinPython.1)UsectypestointerfacewithClibrariesforperformance.2)CreateC-stylearraysfornumericalcomputations.3)PassarraystoCfunctionsforefficientoperations.However,becautiousofmemorymanagement,performanceo

InPython,a"list"isaversatile,mutablesequencethatcanholdmixeddatatypes,whilean"array"isamorememory-efficient,homogeneoussequencerequiringelementsofthesametype.1)Listsareidealfordiversedatastorageandmanipulationduetotheirflexibility

Pythonlistsandarraysarebothmutable.1)Listsareflexibleandsupportheterogeneousdatabutarelessmemory-efficient.2)Arraysaremorememory-efficientforhomogeneousdatabutlessversatile,requiringcorrecttypecodeusagetoavoiderrors.

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
