Definition, use, and parameter passing implementation code of the Python def function

Source: Internet
Author: User
In Python programming, for some programs that require repeated calls, you can use a function to define the basic form:

def function name (parameter 1, Parameter 2, ..., parameter n):

The EXECUTE statement function name is the name of the invocation, and the parameter is the passed-in parameter, which can be defined more or not.

# example 1: simple function using # coding=gb2312# definition function def hello ():  print ' Hello python! '  # Call Function    Hello ()  >>> Hello python!

Functions can have parameters and return values, parameters will be left-to-right matching, parameters can be set default values, when the function is not given the corresponding parameters, will be assigned by default values.

# example 2: Cumulative calculated value # coding=gb2312# definition function def myadd (a=1,b=100):  result = 0  i = a while  i <= B:  # default value is 1+2+3+......+100< C8/>result + = i      i + = 1  return result# print 1+2+......+10 print    myadd (1,10) print myadd ()    # Use default parameters 1,100print Myadd   # A assignment 50,b use default  >>> 55>>> 5050>>> 3825

When the parameters of a Python function are passed, it is worth noting that when a parameter is passed in, the variable is treated as a temporary assignment to the parameter variable, and if the object is referenced.

# example 3:# coding=gb2312def Testpara (P1,P2):  p1 = ten  p2.append (' hello ') L = []   # defines an array pair like a = $   # assigns a value to variable a testpar A (a,l) # variable A with Object array L passed in as parameter to   print a # After printing a run parameter for the value of V in L: # Print the members of an array object the    A variable is not re-evaluated after calling the function print v >>> >>> Hello  # While object L array adds member Hello
  • 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.