Function knowledge points

Keywords: Python Pycharm

Elementary knowledge of function

  • Process oriented programming vs functional programming

    • Process oriented programming

       

      s1 = 'fjdsklfjdsfklds' count = 0 for i in s1: count += 1 print(count) l1 = [1, 22, 33, 44, 44, 545] count = 0 for i in l1: count += 1 print(count)

      Disadvantages:

      1. The code is repetitive and not public.
      2. The readability of the code is poor.
    • Functional programming

       

      l1 = [1, 22, 33, 44, 44, 545] s1 = 'fjdsklfjdsfklds' def len_(argv): count = 0 for i in argv: count += 1 print(count) len_(l1) len_(s1)

      characteristic:

      1. Reduce code repeatability.
      2. Enhance the readability of the code.
  • Functional programming

    There are only two kinds of programming, process oriented programming and object-oriented programming. The essence of functional programming is also process oriented programming, but it is upgraded on this basis and easier to use. A function encapsulates a function, login function, registration function, test length, etc.

Note: the function is to "integrate a piece of code with independent functions" into a whole and name it, and "call the name" at the required position to complete the corresponding requirements.

Operation of function

pep8 specification

def Function name():
	Function body
  • Def keyword, def defines a function.
  • The function name is the same as the setting variable. It is descriptive. Try to reduce the number of words and connect them with underscores.
def func():
    print(111)
    print(222)
    print(333)

func()
  • Function name ()   Executor of function

Return value

def func():
    print(666)
    print(111)
    return
    print(222)
    print(333)
    #return 'barry'
    #return [1,2,3],'barry',100
func()
  • The function does not write return but returns None (the function only writes one return and returns None)
  • return's first function is to terminate the function.
  • The second function of return is to return a value to the executor of a function.
  • If only one value is returned after return, the data type of the value will not be changed. (multiple values are returned after return in the form of tuples.)

Parameters of function

  • Formal and argument

def len_(a):  # The definition of a function. The parameters in this function are formal parameters
    count = 0
    for i in argv:
        count += 1
    return count
    # print(count)
print(len_('qwer'))  # Function's execution arguments
  • The placeholder variable of the function is called a formal parameter. The data passed to this variable during execution is called an argument

  • Position parameters

def meet(sex,age,hobby):
    print('Set gender%s,Age%d,hobby%s' %(sex,age,hobby))

# Position parameters
meet('female',60,'Rent collection')
  • Keyword parameter (default value parameter)

def meet(sex,age,hobby='Play basketball'): #coca #Default value parameter#
    print('Set gender%s,Age%d,hobby%s' %(sex,age,hobby))
    
# Keyword parameters
meet(sex='male',age=70,hobby='square dancing')
  • Set default value parameters. Commonly used parameters are generally set as default value parameters.

  • If no value is passed to the default value parameter, the default parameter will be used. If the value is passed, the default parameter will be overwritten.

  • The position parameter should precede the default value parameter.

  • Mixed parameters

def meet(sex,age,hobby):
    print('Open the software')
    print('Set gender%s,Age%d,hobby%s' %(sex,age,hobby))
    print('Slide left')
    print('Slide right')
    print('hook up or not')
    print('........')


# Keyword parameters
meet('female',hobby='study',age=20)
  • Note: position parameters must be in front of keyword parameters and correspond one by one

      • Universal parameter

  • polymerization

  • When the function is defined, * represents aggregation. A star aggregates all < U > position parameters < / u > of the argument angle into a tuple and assigns values to < U > args < / u >.
  • When the function is defined, * * represents aggregation. The two stars aggregate all < U > keyword parameters < / u > of the argument angle into a dictionary and assign them to < U > kwargs < / u >.
  • Location parameter, default value parameter, * args,**kwargs.
def sum_(*args,**kwargs):
    # When the function is defined, * represents aggregation. A star aggregates all the position parameters of the argument angle into a tuple and assigns it to args.
    # When the function is defined, * represents aggregation. The two stars aggregate all keyword parameters of the argument angle into a dictionary and assign it to kwargs.

    print(args)
    print(kwargs)
sum_(2, 33, 5,name='barry',age=18)
  • Break up
l1 = [22, 33, 55, 666]
l2 = ['barry', 'alen']
func(*l1,*l2)  # ===   func(22,33,55,666,....)

When the function is executed, * stands for fragmentation. * iterable scatters all elements in the iteratable object into position parameters.

dic1 = {'name': 'barry', "age":18}
dic2 = {'height': 176, 'weight':160}
func(**dic1,**dic2)

When the function is executed, it represents scatter. dict scatters all key value pairs in the dictionary into keyword parameters. (keyword parameters must be of string type)

  • Keyword parameters only
def func(a, b, *args, c, sex='male', **kwargs,):
    print(a,b)
    print(sex)
    print(c)
    print(args)

func(1,2,3,4,c=666)

Final order of formal parameter angles: position parameter * args is only for keyword parameters, and the default value parameter is * * kwargs

  • Ternary operation
    # Variable 1 = variable 2 (data) if conditional else variable 3 (data)
    # c = a if a > b else b

Posted by kroll on Sat, 18 Sep 2021 06:17:13 -0700