程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python -- variable, parameter passing, anonymous, recursive function (day05)

編輯:Python

Catalog

One 、 Basic type of function

Two 、 Function parameter reference value

3、 ... and 、 Anonymous functions

  Four 、 Recursive function

5、 ... and 、 Advantages and disadvantages of recursion


What is a function : A series of Python The combination of sentences , You can run one or more times in a program ,
It usually completes specific independent functions
Why use functions :
Maximize code reuse and minimize redundant code , The overall code structure is clear , Problem localization
Function definition :
def Function name ():
     The body of the function [ A series of python sentence , Represents an independent function ]
Function call :
  In essence, it is to execute the code block in the function definition , Before calling the function Must be defined first
 

One 、 Basic type of function

# local variable It's a variable defined inside a function 【 The scope is limited to the interior of the function 】
# Different functions You can define the same local variable , But each uses its own No impact
# The role of local variables : In order to save data temporarily It needs to be defined in the function to store
# ------------ Global variables ----------------
# pro Is defined as a global variable 【 The scope is different 】
# When global variables and local variables are defined repeatedly , The program will give priority to the variables defined inside the function 【 Hydra 】
# If you want to modify the global variable inside the function For immutable types 【str Tuples number】 You have to use global Keyword to declare
# The following two are all variables
pro=' Computer information management '
name=' Miss Wu '
def printInfo():
# name='peter' # local variable
print('{}.{}'.format(name,pro))
pass
def TestMethod():
name=' Lau Andy '
print(name,pro)
pass
def changeGlobal():
'''
To modify global variables
:return:
'''
global pro # Declare global variables Can only be modified after
pro=' Marketing Management ' # local variable
pass
changeGlobal()
print(pro) # Has it been modified
TestMethod()
printInfo()

Two 、 Function parameter reference value

a=1 # Immutable type
def func(x):
print('x The address of {}'.format(id(x)))
x=2
print('x After the value of is modified, the address {}'.format(id(x)))
print(x)
pass
# Call function
# print('a The address of :{}'.format(id(a)))
# func(a)
# print(a)
# Variable type
# li=[]
# # def testRenc(parms):
# #
# # li.append([1,3,4,54,67])
# # print(id(parms))
# # print(' Inside {}'.format(parms))
# # pass
# #
# # print(id(li))
# # testRenc(li)
# # print(' External variable object {}'.format(li))
# Summary
# 1. stay python among Everything is object , When a function is called , The argument passes a reference to the object
# 2. After understanding the principle , You can better control Whether the processing inside the function will affect the data changes outside the function
# Parameter passing is done by object reference 、 Parameter passing is done by object reference 、 Parameter passing is done by object reference

3、 ... and 、 Anonymous functions

# Anonymous functions
# grammar :
# lambda Parameters 1、 Parameters 2、 Parameters 3: expression
# characteristic
# 1. Use lambda Keyword to create a function
# 2. Functions without names
# 3. The expression after the colon of an anonymous function has and only has one , Be careful : Is an expression , Instead of statements
# 4. Anonymous function comes with return, And this return The result of the expression is the result of the expression evaluation
# shortcoming
# lambde It can only be a single expression , Not a code block ,lambde Is designed to meet the scenario of simple functions ,
# Can only encapsulate limited logic , Complex logic cannot achieve , You have to use def To deal with it
def computer(x,y):
'''
Calculate the data and
:param x:
:param y:
:return:
'''
return x+y
pass
# The corresponding anonymous function
M=lambda x,y:x+y
# Call anonymous functions through variables
# print(M(23,19))
# print(computer(10,45))
result=lambda a,b,c:a*b*c
# print(result(12,34,2))
age=25
# print(' You can join the army ' if age>18 else ' Keep going to school ') # It can replace the traditional double branch writing
# funcTest=lambda x,y:x if x>y else y
# print(funcTest(2,12))
# rs=(lambda x,y:x if x>y else y)(16,12) # Call directly
# print(rs)
varRs=lambda x:(x**2)+890
print(varRs(10))
#

  Four 、 Recursive function

# The condition that recursion satisfies
# Call yourself
# There must be a clear end condition
# advantage : The logic is simple 、 The definition is simple
# shortcoming : Easy to cause stack overflow , Memory resources are tight , Even memory leaks
# Find the factorial
# In a circular way
def jiecheng(n):
result=1
for item in range(1,n+1):
result*=item
pass
return result
# print('10 The factorial {}'.format(jiecheng(10)))
# Recursively to achieve
def diguiJc(n):
'''
Recursive implementation
:param n: Factorial parameter
:return:
'''
if n==1:
return 1
else:
return n*diguiJc(n-1)
pass
# Recursively call
print('5 The factorial {}'.format(diguiJc(5)))
# Recursive case Simulation Implementation Traversal of tree structure
import os # Introduce file operation module
def findFile(file_Path):
listRs=os.listdir(file_Path) # Get all folders under this path
for fileItem in listRs:
full_path=os.path.join(file_Path,fileItem) # Get the complete file path
if os.path.isdir(full_path):# Determine whether it is a folder
findFile(full_path) # If it's a folder Recursion again
else:
print(fileItem)
pass
pass
else:
return
pass
# Call search folder object
findFile('E:\\ Software ')

5、 ... and 、 Advantages and disadvantages of recursion


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved