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 use the Python operator *

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains how to use the Python operator *. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use the Python operator *.

1. Useful keyboard shortcuts

Press Tab for indentation code.

Press Shift + Tab to unindent the code.

To comment or uncomment a pile of code, select it and press Control + / or Command + (for Mac users).

To enclose quotation marks, select it and press Shift +'or Shift + ".

2.Zip

Zip can be used to iterate over more than two lists together.

3. Column derivation expression

The best thing about Python is that you can do a lot of work with less code. Take the column derivation expression as an example. If you want to create a list of numbers within a certain range, you can easily do so as follows:

You can also easily apply conditions to it.

Practical example:

A cool use case for list derivation is to convert a number into its single list of numbers. The trick is to convert numbers to strings, iterate through characters, convert them to int, and store them in a list. We can follow all the steps.

Or do it together.

This also reminds me of the map () function.

4. Use the * operator

The * operator can be used to repeat a string. For example,

Now you may not want to print "Python is cool" many times, but you should use it for something like this.

The * operator can also be used to extract iterations such as lists.

You can do something similar.

This operator is usually used when we have a function that does not know the number of arguments. We use it with * args and * kwargs.

The arguments we pass to the function are stored in * args. * * kwargs will store named parameters or dictionaries.

5.Partial

The other thing you can do with the function is to create the Partial function. What are these? Suppose we have a function that calculates simple interest. We can set default values for some parameters (from right to left).

However, we cannot set the default value to p in this way.

We can use some functions to implement it. In some functions, we set default values for some parameters from left to right, and then use them as functions. Let's set a default value for p.

Although local functions work from left to right, we can also use named parameters to skip the parameters between them.

6. Assertion

Test-driven development means that you write tests and then write code to pass them. You can use assert to write small tests in Python. For example, you might want to make sure that the shape of an object meets your expectations.

Writing these small tests will be very helpful in ensuring that parts of the code work as expected. It will also help you debug efficiently.

7.Generator

We can use the yield keyword in Python instead of the return keyword to create the generator. The advantage of using generators is that you can generate things and forget them in real time. This saves memory.

At this point, I believe that you have a deeper understanding of "the use of the Python operator *". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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