Python unpacking and packaging

Time:2022-7-21
"" "the difference between Python unpacking and the * sign of the packet in function parameters and arguments 1. Adding * in the definition of function parameters is the packet process, and the packet is packaged in the form of tuples by default. 2. Adding * in the call process of function parameters is the unpacking process, during which a list or tuple will be reported and split into a single element" "" subject = ["math", "Chinese" ",'english','physics','history']def print"_ subject_ one(sub):    print(*sub)def print_ subject_ two(*sub):    print(sub)def print_ subject_ three(*sub):    print(*sub)def print_ subject_ four(*sub):    print(*sub)def print_ subject_ five(*sub):    print(sub)print_ subject_ one(subject)  # math chinese english physics historyprint_ subject_ two(subject)  # (['math', 'chinese', 'english', 'physics', 'history'],)print_ subject_ three(subject)  # ['math', 'chinese', 'english', 'physics', 'history']print_ subject_ four(*subject)  # math chinese english physics historyprint_ subject_ Five (*subject) # ('math','chinese','english','physics','history') "" "Python unpacking and the * * number of the package are the difference between function parameters and arguments. 1. Adding * * when defining function parameters is the packaging process, and the package is packaged in the form of a dictionary by default. 2. Adding * * during the call process of function parameters is the unpacking process, which usually uses * * to unpack the dictionary in functions with keyword parameters." "hobby = {" Jack ":" dance ", "henry": "basketball", "jenny": "swimming", "richard": "reading"}def print_ hobby_ one(**bby):    print(bby)def print_ hobby_ two(**bby):    print(bby)def print_ hobby_ three(**bby):    if "jack" in bby:        print('jack like dance')    if "jenny" in bby:        print("jenny like swimming")print_ hobby_ one(**hobby)  # {'jack': 'dance', 'henry': 'basketball', 'jenny': 'swimming', 'richard': 'reading'}print_ hobby_ two(jack='dance', henry='basketball')  # {'jack': 'dance', 'henry': 'basketball'}print_ hobby_ Three (* *hobby) \jack like dance Jenny like swimming "" "the application of Python's unpacking and packaging * in other scenarios" "" a, *\u, c = [1, 2, 3, 4, 5, 6]  # a=1, c=6print(a)  # a=1, c=6print(c)  # a=1, c=6a, b, *_ =  [1, 2, 3, 4, 5, 6] print (a) # a=1print (b) # b=2 # one line of code completes the exchange assignment of two variables X = 10Y = 20x, y = y, xprint (x) # x = 20Print (y) # y = 10

Recommended Today

Use of String

1. Use of String String s1 = “abc”;//How literals are defined String s2 = “abc”; System.out.println(s1==s2)//true, s1, s2 point to the same address   1. String is declared final and cannot be inherited2. The String class implements the Serializable interface: indicating that strings support serialization,Implements the Comparable interface: indicates that String can be compared in […]