Python basic study notes-functions

function

1. Custom function

To determine whether an object can be called, you can use the built-in function callable .

Define function: def statement

The string placed at the beginning of the function is called the docstring and will be stored as part of the function. Access the document string: function name._ doc _ , where **_ doc _ is called an attribute of the function **.

All functions have a return value, if you don't tell you what they return, it will return None.


2. Parameters

Positional parameters, keyword parameters, and default values

Collection parameters :

The asterisk means to collect the remaining position parameters , and the asterisk will return a tuple .

def print_params01(title,*params):
	print(title)
	print(params)
	
print_params('Params',1,2,3)		#Params:
									#(1,2,3)

But asterisk cannot collect keyword parameters. To collect keyword parameters, two asterisks can be used, and a dictionary is returned .

def print_params02(**params):
	print(params)
	
print_params02(x=1,y=2,z=3)			#{'z':3,'x':1,'y':2}

Allocation parameters :

Use asterisks when calling functions. The effect is as above, one asterisk is a tuple, and two asterisks are a dictionary.

params = (1,2)
add(*params)									#3

params = {
    
    'name':'Li','greeting':'Well met'}
hello(**params)									#Well met,Li!

Guess you like

Origin blog.csdn.net/qq_36879493/article/details/107835828