This article introduces three-way Python operations and three-way Python functions:
If 1 + 1 = 2: print (True) else: print (False) # Equivalent to: print (True if 1 + 1 = 2 else False)
Function basic syntax def XX ():
Define the function # return aa return value # or pass does not return anything # XX () call the function # The function has three different parameters: # ------ common parameter ------ def func (name ): print (name) func ('fanhaibo ') # ------ default parameter ------ def func2 (name, age = 30): print (name, age) func2 ('fanhaibo ') func2 ('Zhang San', 20) # ------ dynamic parameter ------ # * argsdef func3 (* args): print (args) func3 (1) func3 ('Hello') func3 (1, 2, 3) func3 ([1, 2, 3]) func3 ({1: 'A', 2: 'B'}) # ** kwargs: multiple elements can be input with key = valuedef func4 (** kwargs): print (kwargs) func4 (name = 'fanhaibo', age = 30) # * args ** kwargs def func5 (arg, * args, ** kwargs): # print (args) # print (kwargs) print (arg, args, kwargs) # pass 1 to arg, send 'A' B 'C' as a tuples to args, and pass name and age to kwargsfunc5 (1, 'A',' B ', 'C', name = 'fanhaibo ', age = 30)
The above is a detailed introduction to Python trielement operations and Python functions. For more information, see other related articles in the first PHP community!