python basis (xv) - Function

#len
# = S 'gold boss Nurse'
# len (S)
# DEF my_len (): Custom Function #
# I = 0
# K for S in:
# = I +. 1
# Print (I)
#
# = length my_len ()
# Print (length)
# function
after # defined, can be called from anywhere it is needed
# did not return length, simply print
# The importance of the return of
# A, b
#len (A) # built-in functions
#len (b)
# Def my_len (): # Custom Function
# I = 0
# K for S in:
# = I +. 1
# I # return a return value
#
# = my_len length ()
# Print (length)
#len ()
#. 1. can not be changed, only calculate the length of the string s
# 2. outputted only results
# Returns the value of the
three cases the return value of #
    # no return value - return None
        # do not write return
        # write only return: the end of a function to continue
        # return None - not commonly used
    # returns a value of
        # can return any data type
        # long return can receive
        # If you have more return, then execute only the first in a program
    # return multiple values
        # receiving a number of variables: how much of the return value is received by how many variables
        # receiving a variable : a tuple is obtained
DEF FUNC # ():
# L = [ 'gold boss', 'brother']
# for I in L:
# Print (I)
# I IF == 'gold boss':
# return None
# Print ( '=' 10 *)
# RET = FUNC ()
# Print (RET)
# def func():
#     return {'k':'v'}
# print(func())
# def func2():
#     return 1,2,3  #return 1,2,3
#
# r= func2()
# print(r)

# Def my_len (s): # custom function requires only 0 parameter, received parameters, in the form of parameters, parameters
# I = 0
# for K in S:
# I + =. 1
# return I # Return Value
#
# RET = my_len ( 'gold boss small nurses') # pass parameters: transmission parameters, the actual parameters, arguments
# ret = my_len ([1,2,3,4,5] ) # pass parameters: parameter passing
# print (ret)
# What is the argument?
Syntax Parameters #
# formal and actual parameters concepts

# def f2(l1):
#     f1(l1)
#     for i in l1:
#         print(i)
#
# def f1(l1):
#     for i in l1:
#         print(i)
#
# f2([1,2,3,4])
# Parameter
    # no parameter
        # When you define a function and call the function in brackets do not write the contents of
    # one argument
        what is what # pass
    # multiple parameters
        # Location parameter
# DEF my_sum (A, b):
# Print (A, b )
# RES = A + B #resultThe
# return RES
#
# my_sum RET = (1,2)
# Print (RET)
# Standing on the angle argument:
    # pass parameters in accordance with the position of
    # by keyword parameter passing
    mixed with # can be used: it must be according to the position parameter passing, and then pass the parameter by keyword
            # can not pass to a plurality of values with a variable
Standing on the parameter angle #
    # positional parameters: must pass, and there are several parameters to pass several values
    # default parameters: can not pass, if the transmission is not using the default parameters, if used to pass pass
# def classmate (name, sex = 'M'):
# Print ( '% S:% S'% (name, Sex))
#
# Classmate ( 'brother')
# Classmate ( 'Meng')
# Classmate ( ' big Meng ')
# Classmate (' Franco ',' female ')
# Call the function only when the
    # pass by location: directly write the parameter values
    # by keyword: keyword = value
#-Defined function when:
    # positional parameters: Direct-defined parameter
    # default parameters, keyword parameters: parameter name = 'default value'
    # dynamic parameters: You can accept any number of parameters
                plus before # * parameter name, custom parameter name args ,
                before adding ** # parameter name, parameter name used to kwargs
    # order: location parameter, * args, default parameters, ** kwargs
DEF Classmate # (name, Sex):
# Print ( '% S:% S'% (name, Sex))
#
# Classmate ( 'brother', 'M')
# Classmate (Sex = 'M', name = 'brother')
# Def classmate (name, sex = ' M'):
# Print ( '% S:% S'% (name, Sex))
#
# Classmate ( 'brother')
# Classmate ( 'Longo', sex = ' Female')
# def sum(*args):
#     n = 0
#     for i in args:
#         n+=i
#     return n
#
# print(sum(1,2))
# print(sum(1,2,3))
# print(sum(1,2,3,4))
# def func(**kwargs):
#     print(kwargs)
#
# func(a = 1,b = 2,c =3)
# func(a = 1,b = 2)
# func(a = 1)
# Two dynamic parameters: can take any argument
    # * args: in accordance with the received value of parameter passing positions, organized into a tuple
    # ** kwargs: accepted by keyword parameter passing values, organized into a dictionary
    #args must precede kwargs
# DEF FUNC (* args, default =. 1, kwargs **):
# Print (args, kwargs)
#
# FUNC (1,2,3,4,5, default = 2, A = 'aaaa', b = 'bbbb ',)
Another parameter passing mode dynamic parameter #
# def func (* args): # standing on the angle parameter, coupled to the variable *, is a combination of all the values coming.
Print # (args)
#
# FUNC (1,2,3,4,5)
# L = [1,2,3,4,5]
# FUNC (L *) # standing on the angle of the argument, to a * plus sequence, this sequence is broken up in order
DEF FUNC # (** kwargs):
# Print (kwargs)
#
# FUNC (A =. 1, B = 2)
# D = { 'A':. 1, 'B': a dictionary definition 2} # D
# FUNC ( ** d)
Note # Function
# DEF FUNC ():
# '' '
# This function implements what functions
# 1 parameters:
# 2 parameters:
#: return: the length of a string or list
#' ''
# Pass
    # Default parameters trap
modified # file
# function
. # 1 defined def function
. # 2 function is called
. # 3 return values return function
. # 4-parameter function
    # parameter:
        Position # Parameters: must pass
        # * args: a plurality of locations may receive any parameter
        # default parameters: may not pass
        # ** kwargs: may receive a plurality of keyword parameter
    # argument: mass participation by location, by keyword parameter passing
# Function
    # built-in functions
    # custom function! ! ! ! !

Guess you like

Origin www.cnblogs.com/qingmuzi/p/12652438.html