In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use one star and two stars in python. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
One star (*) and two stars (*)
Have you found that the star (*) is really a magical symbol! Think about it, without it, what is the fun of C language? Similarly, because of it, Python will be so elegant, graceful and charming! The Python function supports default and variable parameters, with one star representing an unlimited number of single-valued parameters and two stars representing an unlimited number of key-value pairs.
Let's give an example: design a function that returns the sum of multiple input values. We can list these input values to the function, but this method is far less elegant than using the variable parameters of a star:
> def multi_sum (* args): s = 0for item in args:s + = itemreturn s > multi_sum
The Python function allows all or part of the use of fixed parameters, default parameters, single-valued (one star) variable parameters, and key-value pairs (two stars) variable parameters, which must be written in the above order.
> def do_something (name, age, gender=' male', * args, * * kwds): print ('name:% s, age:% d, gender:% s% (name, age, gender) print (args) print (kwds) > do_something (' xufive', 50, male', 175,75, math=99, english=90) name: xufive, age: 50, gender: male (175,75) {'math': 99,' english': 90}
In addition, one star and two stars can also be used to unpack lists, tuples, and dictionaries, which look more like C language:
> a = (1 name':'xufive', 'age':51 3) > print (a) (1 pyrrine 2 age':51 3) > print (* a) 1 23 > b = [1 name 3] > print (b) [1 reprit 2] > print (* b) 1 23 > c = {' name':'xufive', 'age':51} > print (c) {' name':'xufive', 'age':51} > > print (* c) name age > print (' name: {name}, age: {age}'. Format (* * c)) name:xufive That's all for age:51 's article on "how python uses one and two stars". Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.