current position:Home>Python notes - Day7 - Fundamentals of functions

Python notes - Day7 - Fundamentals of functions

2022-05-15 06:02:45Wandering mage 12

Preface
python Grammar learning , Leave it to those who need it , Understand everything !!

# coding=utf8
# @time:2022/4/6 20:04
# Author  Haoyu 

# 1. The concept of function 
'''''''''
1) Definition ( What is a function ): A function is the encapsulation of code that implements a specific function 
( The essence of function is function , Different functions have different functions )
2) Classification of functions :
 System function :python Functions that have been defined , Programmers only need to call these functions when they need them ; Columns such as :print、input wait 
 Custom function : Functions defined by the programmer 
'''''''''

# 2. Definition of function 
'''''''''
 grammar :
def  Function name ( Parameter list ):
     Function documentation 
     The body of the function 
    
 explain :
1)def   -    keyword : Fixed writing 
2) Function name     -    The programmer named it himself 
                 All the letters are lower case , Separate words with underscores 
                 See the name and know the meaning ( See the function name and know the function of this function )
                 Do not use the system function name 、 Class name and module name 
3)()    -    Fixed writing 
4) Parameter list   -    With ’ Variable 1‘,’ Variable 2‘,’ Variable 3‘ There is a form of , You can also have no formal parameters 
                 The pain of formal parameters is to transfer the data outside the function to the function , Do you need formal parameters when defining a function , How many do you need , See whether the function of the function needs to be realized, while the external data needs several 
5) Function documentation     -    Equivalent to the instructions of the machine , Used to help others use the current function more conveniently 
6) The body of the function    -    and def One or more statements that hold an indent 
                 The function body is the code segment that realizes the function of the function ( The core )
'''''''''
'''''''''
#  Write a function , Find the sum of two numbers 
def sum2(value1,value2):
    """""""""
    ( Function area description ) Find the sum of two specified numbers 
    :param value1: The first of the numbers required and 
    :param value2: The second digit of the required sum 
    :return:( Return value description )
    """""""""
    print(value1+value2)
'''''''''



#  practice 1: Write a function to find the factorial of an integer 
# N!=1*2*3*....(N-1)*N
def factorial(n):
    sum1 = 1
    for x in range(1,n+1):
        sum1*=x
    print(f'{n}! yes :{sum1}')

#  practice 2: Write a function , Deletes all characters specified in the specified string ?
def del_char(string,char):
    new_str = ''
    for x in string:
        if x != char:
            new_str += x
    print(new_str)
#  practice 3: Write a function , Count the number of specified values in a dictionary 
def value_count(dic,value):
    count = 0
    for x in dic:
        if dic[x] == value:
            count += 1
    print(count)

# 3. Function call 
'''''''''
( important !) When defining a function, the function body is not executed , Only when the function is called will the function body execute !
1) Call function 
 grammar :
 Function name ( Argument list )
 explain :
 Function name  -    The name of the function to be used ( The function name must be the function name of the defined function )
 Argument list     -    Multiple data are well separated by ; The specific number depends on the number of formal parameters of the corresponding function 
                 Arguments are the data that is passed to the function through formal parameters when using the function 

 Be careful : The same function can be called multiple times 

2) Function call procedure 
 First step : Return to the position defined by the function 
 Second parts : The ginseng ( Assign values to formal parameters with arguments , Ensure that each parameter has a value )
 The third step : Execute function body 
 Step four : After executing the function body, return to the position of the function call, and then execute later 
'''''''''

def fun1():
    list1=[1,2,3]
    print(list1[1])
    print('======')

fun1()
factorial(5)
del_char('how are you',' ')

More secure sharing , Please pay attention to 【 Security info】 WeChat official account !

copyright notice
author[Wandering mage 12],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110607425015.html

Random recommended