The initial python-function

Source: Internet
Author: User

function of functions

There is a need to find out the length of a list

# S1 The length of the string s1="1234566"count= 0 for in S1:    + = 1print(count)

#如果又要求s2 = length of "5543322"? You can only repeat the code that asks for S1.

What if I use a function?

def   My_len ():    "123321123"    count= 0    for In   ST:        + = 1    print# call function My_ Len ()# to get the length of the string

1. Function structure

Python code execution process, read from the top, to Def, only the function name into memory, not to read the contents of the function, when the function is called, it will return functions of the name, call function internal code

  Note: The function in the actual work, do not write print in the function body, because the function is the logical process

def keyword function name (set to the same variable): #冒号的作用是隔开函数名和函数体

function body

def   My_len ():    "123321123"    count= 0    for   in ST:        + = 1    return count

Function Execution: function name + parentheses

My_len ()  #相当于函数的执行者 (caller)

2. Return (value returned)

Function: 1, encounter return, End function

deffunc1 ():Print("1")    Print("2")    return     Print("3")    Print("4") func1 ()#This results in only 1, 2, not 3 and 4, because the function body is stopped when the function body execution encounters a return

2. Return a value to the caller of the function

 def   Func1 ():  return  1def   Func2 ():  return  def   func3 () :  return  1,2,3print   (Func1 ())  print   (FUNC2 ()) A,b,c  =func3 ()  print   (a,b,c)    return value in 3 cases The return of FUNC1 returns a value of 1 Func2 return is a value of None func3 return is a (three-dimensional), the type is a tuple can assign the value of the tuple to A,b,c 
      

We will arrange it later, please look forward to

3, the function of the parameter

#从实参角度 individual parameters are indicated by default in argv

1. Position parameters

2. Keyword parameters

3, mixed parameter keyword parameter must be behind the position parameter

Three-dimensional expression

#从形参角度

1. Position parameters

2. Default parameters

3. Dynamic parameter (*args,**kwargs) Universal reference

Args: Receives all positional parameters in the form of tuples

Kwargs: Receive all keyword arguments and put them in the dictionary

Position of the parameter

Function Execution: * Break the liver function, will provide an iterative object of the element, only open into the *args inside

*args * is the definition of the function plus *, is the location parameter you passed into the aggregation, added to a tuple,

**kwargs * is the function of the definition of add * *, is the keyword you passed in the parameter aggregation, added to a dictionary

The step of the function

Built-in namespaces, which can be referenced, cannot be modified, if you want to refer to global

The role of global

1. Global variables not declared

2. Change existing global variables within a function

nonlocal: Only local variables are referenced and changed, and subsequent references are changed only

1. Global variables cannot be manipulated

2, after modifying the variable, then the reference will change

Name of function:

1, can be assigned to each other value

2, can be used as the parameters of the function

3, can be used as the parameters of the container class function

      

The initial python-function

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.