Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to unpack python tuples and function parameters

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today Xiaobian to share with you about the python tuple and function parameters how to unpack the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you will learn something after reading this article, let's take a look at it.

Tuple unpacking # the way to create a tuple my_tuple = (1) if the tuple is not an empty tuple, it must have at least a comma my_tuple = 40 ~ _ Note that the expression on the right actually creates a tuple a = 100b = 300a, b = b, amy_tuple = 10,20,30,4. When unpacking a tuple, the number of variables must be the same as the number of elements in the tuple # use *, or you can add a * before the variable, so that the variable will get all the remaining elements in the tuple # not only the tuple There are also list and string sequences that can also deconstruct a, b, * c = my_tuplea, * b, c = my_tuple*a, b, c = my_tuplea, b, * c = [1, my_tuple 2, 3, 4, 5, 6] a, b, * c = 'hello world'# cannot have two or more * variables * a, * b, c = my_tuple # SyntaxError: two starred expressions in assignment can pack and unpack variable parameters of a function.

Function variable arguments encapsulate all parameters into a tuple instead of a list

Def halo (* a): print (a) halo (1pyr2) halo (1pint 2) halo (1pas) # variable parameters do not have to be written at the end, but note that all parameters with * must be passed in the form of keyword parameters (specify parameters by parameter name) # the first parameter is given to a, and the remaining positional parameters are given to the tuple of b C must use the keyword parameter def fn2 (a =', a) print ('b =', b) print ('c =', c) fn2 (1, def fn2 2, 3, 4, 5) fn2 # if we write a * directly at the beginning of the formal parameter, then all our parameters must be passed in the form of a keyword parameter, def fn2 (*, aQuery bline c): print ('a =') A) print ('b =', b) print ('c =', c) fn2 (Apocalypse 3, dint 4) # * parameters can only accept position parameters Can not receive keyword parameters, * * formal parameters can receive other keyword parameters, it will save these parameters in a dictionary, the dictionary key is the name of the parameter, the dictionary value is the value of the parameter # * * formal parameter can only have one And it must be written at the end of all the parameters def fn3 (brecrcec quotation (a): print ('a =', amemtypee (a)) print ('b =', b) print ('c =', c) fn3 (baure1, djin2, dongle, dongle, 3 cognac, cognac, 20) # parameter unpack (unpack) def fn4 (a ~): print ('a =', a) print ('b =', b) print ('c =' C) # when you create a tuple t = (10, 10, 20, 30) # pass the argument You can also add an asterisk before the real parameter of the sequence type In this way, he will automatically pass the elements in the sequence as parameters # where the number of elements in the sequence must be consistent with the number of formal parameters fn4 (* t) # to create a dictionary d = {'axiaxianlu 100lt lnspectmlv 200pcqlv 300} # to unpack a dictionary fn4 (* d) # is equivalent to using the parameter name to specify the argument fn4. The above are all the contents of the article "how to unpack python tuples and function parameters" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report