Python asterisk * And ** Usage Analysis, and python asterisk Usage Analysis
This article analyzes the usage of Python asterisks * and. We will share this with you for your reference. The details are as follows:
1. Variable names with asterisks (*) will store all unnamed variable parameters and cannot store dict. Otherwise, an error is returned.
For example:
Def multiple (arg, * args): print "arg:", arg # print the variable length parameter for value in args: print "other args :", valueif _ name _ = '_ main _': multiple (1, 'A', True)
Output:
2. Variable names with asterisks (**) are used to store all unnamed variable parameters.
Def multiple2 (** args): # print the variable length parameter for key in args: print key + ":" + bytes (args [key]) if _ name _ = '_ main _': multiple2 (name = 'amy ', age = 12, single = True)
Output
3. There are * args and ** dictargs:
Def multiple (arg, * args, ** dictargs): print "arg:", arg # print args for value in args: print "other args :", value # print the variable length parameter args for key in dictargs: print "dictargs:" + key + ":" + bytes (dictargs [key]) of the dict type if _ name _ = '_ main _': multiple (1, 'A', True, name = 'amy ', age = 12 ,)
Output:
In addition, * represents multiplication in Python mathematical operations, and ** represents exponential operations.The sample code is as follows:
>>> 2*48>>> 2**416>>>