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 skills of Python

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

Share

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

This article mainly explains "what Python skills are there". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what Python skills are there" together!

1. list comprehension

List comprehension can fill lists instead of ugly for loops. The basic syntax for list understanding is:

expression for item in list if conditional

A very simple example of filling a list with a sequence of numbers:

i for i in range(10)

Because expressions can be used, you can also perform mathematical operations or call external functions.

Finally, you can filter the list using if. In this case, we keep only values divisible by 2:

i for i in range(10) if i%2==0

2. Check memory usage of objects

With sys.getsizeof(), you can check the memory usage of an object.

3. return multiple values

Functions in Python can return multiple variables without the need for dictionaries, lists, or classes.

For a limited number of return values, this is possible. But anything with more than 3 values should be placed in a class.

4. Using the data class

It has several advantages over conventional classes or other alternatives, such as returning multiple values or dictionaries:

Data classes require minimal code

You can compare the data class because it__eq__has been implemented for you

You can also easily print the data class for debugging, since it__repr__is also implemented

The data class requires type hints, reducing the chance of errors

5. local variable exchange

A clever trick that saves a few lines of code.

a,bb = b,a

6. Merge Dictionary (Python 3.5+)

Starting with Python 3.5, merging dictionaries has become easier:

If the keys overlap, the keys in the first dictionary will be overwritten.

7. Initial case

Capitalize() converts the first letter of a string to upper case and the rest to lower case.

"welcome".capitalize()

8. Split a string into a list

You can split strings into lists of strings. In this case, we split the space characters:

list("welcome")

9. Quickly create a Web server

You can quickly launch the Web server to serve the contents of the current directory:

python3 -m http.server

This is useful if you want to share something with colleagues or want to test out a simple HTML website.

10. multiline string

Although it is possible to include multiline strings in code using triple quotes, this is not ideal. Everything between the three quotes becomes a string, including formatting, as shown below. I prefer the second approach, which concatenates multiple lines together and formats code well. The only downside is that you need to explicitly add line breaks.

11. Links to Comparison Operators

You can link comparison operators in Python to create more readable and concise code:

12. process date

The python-dateutil module provides a powerful extension to the standard datetime module. Install by:

pip3 install python-dateutil

You can do a lot of cool things with this library. I'll limit my examples to one that I think is particularly useful: fuzzy parsing of dates in log files, etc.

Just remember: where regular Python datetime functionality ends, python-dateutil appears!

Thank you for your reading. The above is the content of "What Python skills are there?" After studying this article, I believe everyone has a deeper understanding of what Python skills are. The specific use situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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