Python 3 Study Notes (5) ---- variables, recursion and high-level functions, python high-level functions

Source: Internet
Author: User
Tags variable scope

Python 3 Study Notes (5) ---- variables, recursion and high-level functions, python high-level functions

I. Variables

1. the variables defined in the subroutine are called local variables, and the variables defined at the beginning of the program are called global variables.
2. The global variable scope is the whole 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, the local variable takes effect in the subroutine defining the local variable, and the global variable takes effect elsewhere.

1 country = "China" # global variable 2 3 def change_name (name): 4 global country # If you want to change the global variable in the function, there is only one way to declare the global + variable name in the function. Never use this method 5 country = "America" # local variable 6 print ("current name is", name) 7 name = "cmdr_irlo" # This function is the scope of this variable, this variable takes effect only in this function. name is called the local variable 8 print ("changed name is", name) 9 10 name = "Irlo" 11 change_name (name) 12 print (name) 13 print (country) # If the global variable of the function is changed, it takes effect after the function is called. Do not change the global variable 14 15''' 16 def country (): # This method can be executed, but never used this way. This is because when the function is called in different places, it will lead to program confusion 17 global country18 country = "China" 19 20 country () 21 print (country) 22 '''

Local variables cannot change the global variables of strings and integers, but complex variables such as lists, dictionaries, sets, and classes can be changed globally in the local area (tuples cannot be changed)

1 country = "China" 2 color = ["red", "green", "blue"] 3 def change_color (): 4 print (color) 5 color [0] = "red" # local variables cannot change the global variables of strings and integers, however, complex items such as list, Dictionary, set, and class can be changed globally in the local area (tuples cannot be changed) 6 print (color) 7 8 change_color () 9 print (color)

Ii. Recursion

You can call other functions within a function. If a function calls itself internally, this function is a recursive function.

Recursive features:

1. There must be a clear termination condition
2. Each time you enter a deeper layer of recursion, the problem scale should be reduced compared to the previous recursion.
3. if the recursion efficiency is not high, too many recursive layers will cause stack overflow (in the computer, function calls are implemented through the stack data structure. Every time a function is called, the stack adds a stack frame. Every time the function returns, the stack will subtract a stack frame. Because the stack size is not infinite, too many recursive calls will cause stack overflow)

1 def calc(n):2     print(n)3     if int(n/2)>0:4         return calc(int(n/2))5     print(n)    #int(1/2)==06 7 calc(10)

Iii. High-Order Functions

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

1 def add (a, B, f): 2 return f (a) + f (B) # f (B) is the function f to take the absolute value of B 3 4 res = add (1,-5, abs) # abs is the default function to take the absolute value 5 print (res) 6

 

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.