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

What are the Python functions?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the Python functions". In daily operation, I believe many people have doubts about what Python functions they have. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what Python functions are there?" Next, please follow the editor to study!

Divmod

This function is very useful in that it performs modular division% on two numbers and then returns quotient and remainder. For example:

Divmod (5,2) [Out]: (2,1)

This is just finding the number of times we can fit 2 into 5, and we don't need to split this number to get a quotient of 2 and 1 as the remainder. It is especially useful for calculating the time (in hours, minutes, and seconds) it takes to return the process to run, like this:

Start = datetime.datetime.now ()... # process code goes hereend = datetime.datetime.now () # we get the total runtime in seconds runtime = (end-start). Seconds # wewill assume 3000 how many hours are in these secs, what are the remainingsecs? Hours, remainder = divmod (runtime, 3600) # now how many minutes and seconds arein our remainder? Mins, secs = divmod (remainder,60) print ("{: 02d}: {: 02d}: {: 02d}" .format (hours, mins, secs)) [Out]: "08:00:08"

Casefold

This is a particularly interesting string method that functions like lower. But casefold tries to standardize a wider range of characters more aggressively In most cases, lower and casefold behave the same, but sometimes they are different:

"Out" .casefold () # both and σ are the Greek letter sigma [Out]: "σ"

By contrast, use lower:

"Out" .lower () # however, lower recognizes them as different [Out]: "Out": False

In this case, both sigma are already lowercase. Depending on usage, it may work as expected. However, if you plan to compare two equivalent Greek words, one uses sigma and the other uses Greek. Although the same, only casefold allows us to compare them accurately:

[Out]: False "p μ η σ". Lower () = = "ρ μ η σ" .lower () [Out]: False "ρ μ η σ" .Casefold () = "ρ μ η μ σ" .Casefold () [Out]: True

* args, * * kwargs

Sometimes you may see that the function definition contains these two parameters, such as def func (xrecery * args,** kwargs).

They are both very simple, and both allow us to pass multiple values to a function and package them into a generator. As to whether to pass the list / generator to the standard parameter, the result is as follows:

Def func (values): for x in values: print (x, end= "") func ([1 end= 2,3]) [Out]:'1 2 3'

When using * args, we should pass each value as a new parameter instead of including them all in the list.

Def func (* values): for x in values: print (x, end= "") func (1 Out 2, 3) [Out]: 1 2 3

Note that you don't need to enter * args, just enter * values. Because of a single asterisk *, it is defined as * args, regardless of the variable name used. * args simply creates a generator object based on the parameters passed to the function. * * kwargs creates a dictionary.

Therefore, you can use the name and keyword parameters as follows:

Def func (* * values): for x in values: print (f "{x}: {values [x]}") func [Out]: X: 1 y: 2 z: 3

Again, you can call variables at will, in which case, use * * values. Define it as * * kwargs by using double quotes.

List understanding

Understanding expressions is essential, which is definitely one of the most useful features of Python. The most common is list understanding, where most people have seen the following:

Vals = [1, 2, 3, 4, 5] [fori in vals 2] [Out]: [1, 4, 9, 16, 25]

But not only can you use square brackets, you can also define generator expressions with almost exactly the same syntax:

(iTune2 for i in vals) [Out]:

Of course, each element in the generator is output only when it is called, and we can use list () to do this:

List ((iTune2 for i in vals)) [Out]: [1, 4, 9, 16, 25]

You only need to make a small change to the syntax, and you can even use dictionary understanding to build a dictionary:

{I: iTunes 2 for i in vals} [Out]: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25} at this point, the study of "what are the Python functions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report