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

Understanding of functions in Python

編輯:Python

Popular speaking : So called functions ,  It refers to a piece of code that implements specific functions “ take ” A name ,  You can go through   This name is used to execute ( call ) This function  

From function definer ( People who implement functions ) From the perspective of ,  At least it needs to think clearly  3  spot :

  Functions need several key data that need to change dynamically ,  These data should be defined as parameters of the function  

The internal implementation of the function  

The function needs to send out several important data ( It's the data that the caller wants ),  These data should be   Defined as the return value  

Function definition and call

  You have to define a function before you can use it   The syntax format for defining functions is as follows : 

Def  Function name  (  Parameter list ) 

Function statement  

Return  Return value  

Call function :  Function name  ( Shape parameter ) 

Python  Declaration functions must use  def  keyword , A detailed description of the function syntax format is as follows : 

Function name :  From a grammatical point of view ,  A function name needs only a legal identifier  ,  From a program's readability point of view ,  The function name should be composed of one or more meaningful words ,  The letters of each word are all lowercase ,  Words and words   Use underline to separate  

Parameter list :  Used to define the parameters that the function can receive ,  The parameter list consists of multiple parameter names ,  Name of multiple formal parameters   Use English commas (, )  separate ,  Once the parameter list is specified when defining a function ,  When you call this function, you must pass   The corresponding parameter value ,  in other words ,  Who calls the function and who is responsible for the assignment of parameters

  Function statement part :  When you receive a formal parameter ,  How to calculate formal parameter data ,  That is, the code part

 Return  part :  When your code is calculated ,  The data that needs to be returned to the caller ,  If... Is not specified in the definition  return  Parameters ,  The function will not return the result after the operation .  In the function , return  After the code ,  It won't run again  

Return  Return multiple values

  If the program needs to have multiple return values ,  You can wrap multiple values into a list and return ,  Multiple values can also be returned directly   If  python  Function returns multiple values directly , python  Will automatically encapsulate multiple return values into meta groups

Format : return  value  1 ,  value  2 ,  value  3 ( The value can be a number ,  character string ,  Variable ,  Array ,  Dictionary, etc )

  If multiple values are returned ,  It can also be received separately with multiple variables  

example : x  The function throws two values ,  Can be received : a , b=x ( )  The first value will be given  a ,  The second value will give  b 

Write a function ,  Implement the passed in array ,  If the passed in parameter is an integer or decimal ,  Just add up the values ,  Sum and average

 c=[1,2,3,4,5] 

 def test(test): 

  sum=0

  count=0 

  for i in test: 

       if isinstance(i,int) or isinstance(i,float): 

           sum+=i 

           count+=1

   return sum,sum/count

 a,b=test(c) 

 print(a,b) 

Find the even number ,  Encapsulated in a function ,  Implement the passed in array ,  Automatically calculate even numbers  

def w(a): 

   x=[] 

   for i in a:

     if isinstance(i,int)or isinstance(i,float): 

       x.append(i) 

 n=[]

 for q in x: 

     if q%2==0:

        n.append(q) 

print(n) 

return n

a=[1,2,4,5,7,1234,123,45,24,46,'qwer',66,0.66,62,0.66] 

w(a) 

When defining a variable in a program ,  This variable has a scope ,  The scope of a variable is called its scope .  According to the position of the defined variable ,  There are two kinds of variables :  local variable :  Variables defined in functions ,  Including the parameters ,  Are called local variables .  Local variables are only valid within functions

Global variables : Outside the function , Globally defined variables , Called global variables , Global variables are valid for the entire code segment .


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