The way of python learning (function and functional programming)

The way of python learning (function and functional programming)

Definition of function

Function is a programming method of logic structure and process
The difference between a function and a procedure is that a function has a return value and a procedure has no return value

# function
def func1():
    '''Test function'''
    print('function')
    return 0
# process
def func2():
    '''Test process'''
    print('process')

The benefits are:
1. The code can be reused.
2. The method is easy to expand.
3. The program is easy to maintain.

Return value of function

Note: the return value can be any type

def test1():
    pass
def test2():
    return 0
def test3():
    return 1,"Hello",{"a":"b"},[1,2,3,4]

a1 = test1()
a2 = test2()
a3 = test3()

print('test1 Printed is the return number%s , Return value%s' %(type(a1),a1))
print('test2 Printed is the return number%s , Return value%s' %(type(a2),a2))
print('test3 Printed is the return number%s , Return value%s' %(type(a3),a3))

#The printing result is:
#test1 prints the return number < class' nonetype '> and the return value is None
#test1 prints the return number < class' Int '> with a return value of 0
#test1 prints the return number < class' tuple '> and the return value (1,' hello ', {' a ':' B '}, [1, 2, 3, 4])

Functions with parameters

Normal parameters

def youcanfunc(x,y):
    print(x)
    print(y)

youcanfunc(1,2) # Call according to the position parameter of sequential value transfer
youcanfunc(y = 1,x = 2) # Keyword call

Default parameters

def youcanfunc(x,y = 2): # Contains a default parameter
    print(x)
    print(y)
youcanfunc(1) #You can pass value to y or not. If you do, go according to the value.

Parameter group

def groupfunc(*args): # How many parameters are not fixed
    print(args)
groupfunc(1,2,3,4) # Two methods of parameter transfer 
groupfunc(*[1,2,3,4,5])
# The returned result will be a tuple
# (1, 2, 3, 4)
# (1, 2, 3, 4, 5)

Parameter is Dictionary

def func(**kwargs):
    print(kwargs)

func(name = "tianrun",age = 20)
func(**{'name' : "tianrun",'age' : 20})
# Print results: {'name':'tianrun ','age': 20}

Global variable and local variable

name = 'tianrun' # global variable
def func(x):
    name = 'tt' # local variable
    print(x , name)
func(name)
print(name)
# Print:
# tianrun tt
# tianrun
# Note: it can be proved that the name in the function has no effect on the name outside
# If you want to change global variables in a function, use global decoration

name = 'tianrun'
def func(x):
    global name
    name = 'tt'
    print(x , name)
func(name)
print(name)
# Print:
# tianrun tt
# tt

# Note: try not to change the global variable in the function;

Note: if the global variables are list, dictionary and other variables, they can be changed locally.

recursion

Recursion is to call itself
See the example below

def aa (n):
    print(n)
    return aa(n+1)
aa(1) # It will loop indefinitely, but according to python, it can only recurse 999 times

characteristic:
1. There must be a clear end condition
2. Low efficiency. If there are too many loops, the memory (stack) may overflow

other

Get system time and format words

>>> import time
>>> time_formate = '%Y-%m-%d %X'
>>> print(time.strftime(time_formate))
2017-12-14 15:09:04
>>> 

Tags: Python Programming

Posted on Tue, 19 May 2020 08:38:32 -0700 by earthlingzed