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 eight grammars of Python

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

What are the eight grammars of Python? aiming at this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

So why is Python better as a general programming language?

In recent years, Python has swept the language rankings. In the world ranking of scripting languages, Python is strong at the top; in the ranking of TIOBE programming languages, Python's status is also rising month by month, becoming more and more popular among developers. Compared with the enduring Java, C, Centrum, the rising star Python is well received.

In the ranking of TIOBE programming languages, Python is also ranked third among developers' favorite programming languages, just behind Java, C.

Also as a general programming language, it is inevitable to distinguish one, two, three or four. So who can take the lead compared to Java, C, C++ and Python? Who loses a little bit?

Although at present, the Java language is still ahead of the crowd, but through the above comparison, we can find that even compared with the three established languages, Java, C and C++, Python is easy, and even vaguely means to catch up from behind.

As a result, after entering 2018, Python has almost become an "online celebrity" in the programming language world, soaring in terms of users and popularity. In May, Python surpassed Java in the PYPL Index to win the championship for the first time.

If you lengthen the timeline of PYPL programming language popularity index, Python has almost always been a "bull market" for more than a decade.

We all know that Python is easy to start and has a wide range of uses, and we can also use Python to do some unexpected things. The purpose of this article is to help friends fully understand Python, that's all.

Next, I would like to introduce some little-known Python syntax: (just play with the code, don't write such code online, remember.)

1. Rewrite multiline functions using lambda expressions

It is well known that python's lambda expressions do not support multiple lines of code. But you can simulate the effect of multiple lines of code.

Strange as it may seem, the above function can be replaced with the following lambda expression function:

Always remember, never write code like this in a production environment:) or you will be killed.

2. Replacement method bytecode

Python prevents methods in class instances from being replaced because python assigns read-only properties to methods in class instances:

However, it can be replaced at the bytecode level:

Be careful! This affects not only the current instance, but also the entire class (the function bound to this class, to be exact). It is speculated that the author's original intention is: all classes bound to this function, to be exact, and all other instances will also be affected:

3. Ternary operator

Modern python provides a simpler syntax:

B if an else c

You can also override it in the following ways:

(an and [b] or [c]) [0] (b, c) [not a]

By the way, the following variants are wrong:

An and b or cTrue and [] or [1]-> [1], but: [] if True else [1]-> []

4. Remove duplicate elements by list derivation.

Let's convert the string x = 'tteesstt' to' test'.

1. Compare with the previous character in the original string:

'.join ([''if i and j = = x [I-1] else j for iMagazine j in enumerate (x)])

2. Save the previous character to a temporary variable:

3. Compare with the previous character in the new string:

[(not r.endswith (I) and [r for r in [rhami]], r) [- 1] for r in [''] for i in x] [- 1]

4. Through reduce function and lambda expression:

Reduce (lambda a, b: an if a.endswith (b) else a + b, x)

5. Get the Fiboracci series through the list derivation.

Save the intermediate values in the list

Save the intermediate value in the dictionary:

Through the reduce function and lambda expression:

The fastest variant:

6. Use list derivation to generate an endless loop

[a.append (b) for an in [[None]] for b in a]

7. List slicing skills

Copy list:

Remove / replace any element in the list:

Add an element at the beginning of the list:

Add an element at the end of the list:

Reverse the list:

8. Let the variable element be the default value of the function parameter

It is very dangerous to use mutable objects as the default values of function parameters, and there are a lot of thorny interview questions about this during the interview. But this is very helpful for the caching mechanism.

1. Factorial function:

two。 Fibonacci series:

Summary:

Python is becoming more and more popular because of its concise syntax, powerful functions, and efficiency advantages in artificial intelligence and big data.

Python encapsulates other languages and calls packages made by other languages. Many data scientists actually do not want to learn overly complex programming languages. It will be much more convenient to encapsulate their wisdom about data processing and call it through Python. That's why Python is so popular these years.

The answers to the eight grammar questions about Python are shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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

Network Security

Wechat

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

12
Report