In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the interesting uses of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the interesting uses of Python"?
Interesting usage
1.for-else usage
The else statement is executed when the loop ends normally. It is generally used to loop to find elements that meet the criteria. If found, break calls out the loop, which will not trigger else;. If it is not found (full run loop), then print not found
The use of for-else is questioned in the book "Effictive Python". The main point is that this usage can be replaced by encapsulating it into a function, which is more general and easy to understand, so the use of for-else is generally not used.
2.try-else usage
Execute else if no exception is triggered
3. Unpacking usage
Like this, a _ c _
4. Single-line if-else
A = 1b = 3 if a = = 1 else 2print ('it is one' if a = = 1 else 'no')
5. There is no need to add parentheses to the iterator input function.
# it's generally like this: a = (i for i in range (10)) sum (a) # We can do this sum ((i for i in range (10) # but we can also do this sum (i for i in range (10)) # similar to '.join (str (I) for i in range (10))
The usage of 7.or
X or y in python means that if x is true, it is the value of x, otherwise it is the value of y
We often see usage like this (for example, one of the value parameters of a function is not set to a default value, which allows it not to be assigned)
Value = value or {} # is equivalent to value = value if value else {}
The usage of 8.and
X and y in python means that if x is false, the result is the value of x, otherwise it is the value of y
When x and y and z has multiple and connections, if all the results are true, it is the last value; if there is a false value in the middle, the result is the first false value
Give me an example.
Def not_empty (a): return an and a.strip () not_empty ('a') # with a value of 'a'not_empty (None) # will not report an error (if return a.strip () will report an error) # is equivalent to def not_empty (a): if an is None: return None else: return a.strip () in dealing with None
Savor the difference between and and or carefully. Their logic is similar, but the functions they implement cannot be substituted for each other.
Or is the result. If you are not satisfied with the aftermath,
And is to check something before you do it. If you can't do it, don't let it do it.
9.if value:
# to use if value:#, do not use if value = = True:
Here's a summary of when it is True and when it is False.
False: 0 0.0'[] {} () set () None False
True:
'' anything' ['] [0] (None,)
Iterable objects without content
In addition, it should be noted that when we use if to determine whether an object is None or not, we should if an is None rather than directly if a, because if it is the latter, many cases that are not None will be judged as False, such as empty string, empty list, etc., in order to accurately specify None or the former, this is also a specification.
10. Special use of underscores
Underscore in python is a kind of special variable and symbol, which has some special uses.
11. Document string
Python has a unique way of annotating. In the first sentence of packages, modules, functions and classes, using three quotation marks such as''doc''', you can extract objects in the form of _ _ doc__.
The more standard way to write it is like this (refer to the grequests module here)
Def myfun (a, b):''add two numbers: param a: one number: param b: another number: a number' print (a + b) print (myfun.__doc__) # the result is add two numbers: param a: one number: param b: another number: returns: a number
In fact, there are other ways to write parameters, such as the numpy library. You can see here.
In addition, there is another way for function comments. The function name can directly call the comment of a parameter.
Useful function
The essence of 1.sum
Essence: sum (iterable, start=0) uses + connections to iterate objects
So sum ([[1jue 2], [3je 4]], []) returns the result as [1rect 2,3je 4].
2.range (start, stop [, step])
Can directly use for i in range (10, 0,-1) descending cycle
3.enumerate circular index
For index, item in enumerate (['await,' baked,'c']): print (index, item) output: 0 A1 b2 c
4. Pipeline operation
Func1 (func2 (func3 (a)) is written like a% >% func3% >% func2% >% func1 to clearly show the order in which the functions are executed and enhance readability.
Python does not come with such usage itself, but some libraries provide such usage, such as pandas and syntax_suga
Thank you for your reading. The above is the content of "what are the interesting uses of Python?" after the study of this article, I believe you have a deeper understanding of the interesting usage of Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.