#今天来学习一下函数, function
# when defining a function, the function will not be executed, only the function will execute if it is called.
#
# Defining functions
# # 1, def is the keyword to create a function, create a function
# # 2, Name of function
# # 3, ()
# # 4, function body
# # 5, return value
#
#
# try:
# Catch the execution of this piece of code
# except:
# If it fails, execute the statement here
#
# Else:
# If successful, execute the statement here
#
# Execute function
#
# function name ()
#
# define a function like this
#
#
# Def pick Up Courier ():
#
#
# xxxxx
# xxxxx
# xxxxx
#函数遇到return后就不会继续执行后面的语句
# def my_first_function ():
# username = ' Bob '
# _username = input (' Input name: ')
# if _username = = Username:
# return "Welcome login"
# Else:
# return "Username is Invalid"
#
# result = My_first_function ()
# Print (Result)
#如果在函数中没有定义任何一个return, Python will give a default return value of None
# def F2 ():
# print ("Hi,everybody")
# ret = F2 ()
# Print (ret)
#学习一下形参和实参, it is important to note that the formal parameter is defined when the function is defined, the argument is the function of the call function, the argument and the formal parameters should correspond, and the order is consistent, the first argument corresponds to the first parameter, the second
#实参对应第二个形参, etc.
# def send (address,content,name): #这里是形参
# print (' Send to ', address, ' success ', ' contents ', content, ' name is ', name)
# return True
# while True:
# em = input ("Input address:")
# result = Send (em, ' SB ', ' Bob ') #这里是实参
# if result = = True:
# print (' Send success ')
# Else:
# print (' Send failed ')
#函数的默认参数, if the parameter has three parameters, and one of which is the default parameter, the argument only needs to pass 2 or 3 can be, if 2, then the third parameter is displayed with the default value, if three parameters, then the third parameter
#用传递的参数代替, here's the point that if you want to set a default parameter, you have to put it on the last side of the parameter, you can have multiple default parameters, but you have to put it on the last side.
#这里是定义了三个参数, where name is the default parameter, has a default value, but the argument passes three parameters, so the default value is not used
# def send (address,content,name= ' Bob '):
# print (' Send to ', address, ' success ', ' contents ', content, ' name is ', name)
# return True
# while True:
# em = input ("Input address:")
# result = Send (em, ' SB ', ' ale ')
# if result = = True:
# print (' Send success ')
# Else:
# print (' Send failed ')
#这里是定义了三个参数, where name is the default parameter, has a default value, but the argument passes two parameters, so name uses the default parameter
# def send (address, content, name= ' Bob '):
# print (' Send to ', address, ' success ', ' contents ', content, ' name is ', name)
# return True
# while True:
# em = input ("Input address:")
# result = Send (em, ' SB ')
# if result = = True:
# print (' Send success ')
# Else:
# print (' Send failed ')
#指定参数, the front is the default argument and the parameter is in order one by one corresponding, the following can specify which parameter corresponds to which argument, do not need to follow the order one by one
# def send (address, content):
# print (' Send to ', address, ' success ', ' contents ', content)
# return True
# while True:
# em = input ("Input address:")
# result = Send (address= ' [email protected] ', Content=em)
# if result = = True:
# print (' Send success ')
# Else:
# print (' Send failed ')
# * indicates that n arguments can be accepted, and then put all the arguments into a tuple called args
# def F1 (*args):
# print (args)
# Print (type (args))
# f1 (11,22,33, ' Aaad ', ' bbbb ')
#这里也可以传一个列表进去, but take this list as an element of the tuple
# list1 = [11,22,33,44]
# list2 = [66,77,8,8]
# def F1 (*args):
# print (args)
# Print (type (args))
# f1 (LIST1,LIST2)
#如果传递的实参的前面有个 *, let's take a look at the effect, whether it's a list or str, equivalent to A for loop, and each element of the list or string as an element of the parameter
# name = ' 1234 '
# list1 = [11,22,33,44]
# def F1 (*args):
# print (args)
# f1 (List1)
# f1 (*LIST1)
# f1 (* ' ABCD ')
# f1 (*name)
#前面的形参是有一个 *, in fact, can also be a formal parameter can also have two * number, then in the call function when passing arguments to pass the actual argument, or will be error, here to pay attention to the form of transmission, the use of dict
#= No.
# def F1 (**args):
# print (args)
# Print (type (args))
#
# f1 (n1= ' CCCC ', n2= ' dddd ')
#直接传递一个dict进去, if in this way, the whole dict as an element
# Dict1 = {' name ': ' Bob ', ' age ': ' Job ': ' It '}
# def F1 (**args):
# print (args)
# Print (type (args))
#
# f1 (K=dict1)
#如果实参中有两个 *, each element in Dict is passed as an element to the parameter
# Dict1 = {' name ': ' Bob ', ' age ': ' Job ': ' It '}
# def F1 (**args):
# print (args)
# Print (type (args))
#
# f1 (**dict1)
#总结下函数传参
# 1, normal parameters, in strict accordance with the input order, the actual parameter and parameter one by one corresponding
# 2, default parameters, must be placed at the end of the formal parameter
# 3, specifying parameters, assigning actual parameters to the formal parameter
# 4, Dynamic parameters
# 4-1, * default will pass in the parameters, all into the list, if the argument of the addition of a *, the list or string will be a for loop, the argument of each element as an element passed to the parameter
# 4-2, * * Default will pass in the parameters, all into the dictionary, if the argument of the addition of a * *, will do a for loop on the dict, each element of the argument as an element passed to the parameter
# 5, the universal parameters, it must be the following, will automatically be the list or str parameters passed to the args, the dict passed to Kwargs, and in the writing parameters, must put a *args in front, **kwargs
# put it in the back #
# def F1 (*args,**kwargs):
# print (args)
# Print (Kwargs)
#
#
# f1 (11,22,33,44,k1= ' Bob ', k2= ' ale ')
#其中python中的另外一个格式化输出就用到万能传参的方法
# def format (self, *args, **kwargs): # Known special case of Str.format
# s = ' I am {0},age is {1} '. Format (' Bob ', 34)
# print (s)
#
# s = ' I am {0},age is {1} '. Format (*[' Bob ', 34])
# print (s)
# s = ' I am {name},age ' {age} '. Format (name= ' Bob ', age= ' 23 ')
# print (s)
# dict = {' name ': ' Bob ', ' Age ': 12}
# s = ' I am {Name},age is {age} '. Format (**dict)
# print (s)
Python Functions (function)