1. Functions
function, in general, functions are functions, functions are used to encapsulate the function, the function is divided into two types, one is the system comes with the use of without us to write. The other function is custom and requires us to write its function, which is a high degree of freedom called a custom function.
#Encoding:utf-8#the function of the system comes with#implements the ability to take string lengthsA="hello,jiyongxin"PrintLen (a)# the#Implementing a String cutA="Student"b=a.split ("u")PrintB#[' st ', ' Dent ']#Custom FunctionsdefA ():Print "This is a custom a function"A ()#This is a custom a function
Definition of the function:
① declares that the specified part is a function
② to write this function
Format:def function name ():
function body
function body
2. Formal parameters and arguments
Parameters are the data that the function needs to implement the function
Print len ("123aw") where parentheses are the parameters of the function
Formal parameters: Formal parameters generally occur in the Korean definition process, the formal parameter refers to the name of the parameter, and does not represent the value of the parameter, he is only a formal number, only a function of the name, which position has the parameter of which
# what is a formal parameter def Function1 (A, B):#A and Bare formal parameters if a>B: print A Else : Print b
Arguments: Arguments typically occur when a function is called, generally referring to a parameter-specific value
# Encoding:utf-8 # What is an argument def Function1 (A, B):#A and B are formal parameters if a>B: Print a Else : Print Bfunction1 (# result is 3, here the argument in parentheses is an argument
3, the transfer of parameters
The parameters are passed in sequence during a Python function call.
#Encoding:utf-8#Passing of parameters#First: The simplest deliverydefFindmax (A, b):ifA>B:PrintSTR (a) +"Big" elifa<B:PrintSTR (b) +"Big" Else: Print "as Big"Findmax (5,8)#8 Large#Second: Assignment transferdefPrintnum (a,b=9): PrintaPrintBprintnum (6)#6,9Printnum (5,12)#5,12#Third: Key parametersdeffunction (a=1,b=6,c=7): PrintaPrintbPrintcfunction ()#1,6,7function (2,3,5)#2,3,5function (a=5,c=1,b=2)#5,2,1#Note that arguments cannot conflict#function (b=2,c=3,2) will have an error
4. Global variables and local variables
What is a scope: A variable in Python works within a certain range, which we call scope in the scope in which it works
# Encoding:utf-8 def Fun (): i=8print i
#这句代码会报错, because I is a local variable in the fun function, only valid within the function, and this output statement is the global scope, is not found in this I
Local variables
In Python, scopes are scoped rather than global variables, which we call local variables. In a function, if our variable is not declared as a global variable, it defaults to a local variable
# Encoding:utf-8 def Fun (a): i=7 Print # 7 output is the local variable within the range of Ii=9 fun(i)print#9 Output is the global scope of your I
Global variables
In Python, if we want a variable to be scoped globally. That is, in all parts of the program, then we need to make a global declaration of this variable, which is a global variable.
# encoding:utf-8 def Fun (a): global I i =7 print i # 7 Fun (1) I = 8 print i # 8 def Fun2 (a): global I i =7 print i # 7 i = 8fun2 ( 1) # 7
Python-12:python Syntax Basics-Functions