Python 3 Learning Note (v)----variables, recursion, and higher order functions

Source: Internet
Author: User

One, variable

1. A variable defined in a subroutine is called a local variable, and a variable defined at the beginning of the program is called a global variable.
2. The global variable scope is the entire program, and the local variable scope is the subroutine that defines the variable.
3. When a global variable has the same name as a local variable: Local variables work within subroutines that define local variables, and global variables work elsewhere.

1Country =" China"  #Global Variables2 3 defchange_name (name):4     GlobalCountry#If you want to change a global variable in a function, there is only one way to declare the global+ variable name in the function. Never use this method .5Country ="America"   #Local Variables6     Print("Current name is", name)7Name ="Cmdr_irlo"     #This function is the scope of this variable, which only takes effect in this function. Name is called a local variable8     Print("changed name is", name)9 TenName ="Irlo" One change_name (name) A Print(name) - Print(country)#If a function changes a global variable, it takes effect after the function is called. Do not change global variables -  the " " - def country (): #这种方式可以执行, but never use it that way. Because the function is called in different places, it causes the program to be confused. - Global Country - country = "China" +  - Country () + Print (country) A " "

Local variables cannot change the global variables of strings or integers, but complex like lists, dictionaries, collections, and classes can be changed globally (tuples cannot be changed)

1Country =" China"2color = ["Red","Green","Blue"]3 defChange_color ():4     Print(color)5Color[0] ="Red"   #Local variables cannot change the global variables of strings or integers, but complex like lists, dictionaries, collections, and classes can be changed globally (tuples cannot be changed)6     Print(color)7 8 Change_color ()9 Print(color)

Second, recursion

Inside a function, you can call other functions. If a function calls itself internally, the function is a recursive function.

Recursive properties:

1. There must be a clear end condition
2. Each time a deeper level of recursion is reached, the problem size should be reduced compared to the previous recursion
3. Recursive efficiency is not high, too many recursive hierarchy will lead to stack overflow (in the computer, function calls through the stack (stack) This data structure implementation, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will reduce the stack frame. Because the size of the stack is not infinite, there are too many recursive calls, which can cause the stack to overflow.

1 def Calc (n): 2     Print (n) 3     if int (N/2) >0:4         return calc (int (n/2))5     Print(n)    # Int (==0) 6 7 Calc (10)

Third, higher order function

A variable can point to a function, which can receive a variable, and a function can receive another function as a parameter, a function called a higher order function.

1 def Add (a,b,f): 2     return F (a) +f (b)  #f (b) is the absolute value of the function f take b 34 res = Add (1,-5,abs)  # ABS is the default function takes absolute value 5 Print (RES) 6  

Python 3 Learning Note (v)----variables, recursion, and higher order functions

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.