What are the advanced skills in Python development
In this issue, Xiaobian will bring you advanced skills about Python development. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.
What are the advanced skills in Python development? This is the last question, I summarized some common tips here, may not be very advanced, but master these can at least make your code look Pythonic a little bit. If you are still writing according to the style of C-like language, I am afraid you will be criticized in code review.
list comprehensions
>>> chars = [ c for c in 'python' ] >>> chars ['p', 'y', 't', 'h', 'o', 'n']
dictionary derivation
>>> dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5} >>> double_dict1 = {k:v*2 for (k,v) in dict1.items()} >>> double_dict1 {'a': 2, 'b': 4, 'c': 6, 'd': 8, 'e': 10}
set derivation
>>> set1 = {1,2,3,4} >>> double_set = {i*2 for i in set1} >>> double_set {8, 2, 4, 6}
merge dictionary
>>> x = {'a':1,'b':2} >>> y = {'c':3, 'd':4} >>> z = {**x, **y} >>> z {'a': 1, 'b': 2, 'c': 3, 'd': 4}
replication list
>>> nums = [1,2,3] >>> nums[::] [1, 2, 3] >>> copy_nums = nums[::] >>> copy_nums [1, 2, 3]
inverted list
>>> reverse_nums = nums[::-1] >>> reverse_nums [3, 2, 1]
PACKING / UNPACKING
variable exchange
>>> a,b = 1, 2 >>> a ,b = b,a >>> a 2 >>> b 1
Advanced unpacking
>>> a, *b = 1,2,3 >>> a 1 >>> b [2, 3]
or
>>> a, *b, c = 1,2,3,4,5 >>> a 1 >>> b [2, 3, 4] >>> c 5
The function returns multiple values (actually automatically packing tuples) and unpacking assigns 4 variables
>>> def f(): ... return 1, 2, 3, 4 ... >>> a, b, c, d = f() >>> a 1 >>> d 4
List merging into strings
>>> " ".join(["I", "Love", "Python"]) 'I Love Python'
chain comparison
>>> if a > 2 and a
< 5: ... pass ... >>> if 2> 0 0 1 1 2 2
in place of or
>>> if x == 1 or x == 2 or x == 3: ... pass ... >>> if x in (1,2,3): ... pass
Dictionary replaces multiple if else
def fun(x): if x == 'a': return 1 elif x == 'b': return 2 else: return None def fun(x): return {"a": 1, "b": 2}.get(x)
subscripted indexed enumeration
>>> for i, e in enumerate(["a","b","c"]): ... print(i, e) ... 0 a 1 b 2 c
generator
Note the distinction between list derivations, generators are more efficient
>>> g = (i**2 for i in range(5)) >>> g >>> for i in g: ... print(i) ... 0 1 4 9 16
default dictionary
>>> d = dict() >>> d['nums'] KeyError: 'nums' >>> >>> from collections import defaultdict >>> d = defaultdict(list) >>> d["nums"] []
string formatting
>>> lang = 'python' >>> f'{lang} is most popular language in the world' 'python is most popular language in the world'
The most frequent element in a list
>>> numbers = [1,2,3,3] >> max(set(numbers), key= numbers.count) 3 or from collections import Counter >>> Counter(numbers).most_common()[0][0] 3
read and write files
>>> with open("test.txt", "w") as f: ... f.writelines("hello")
Determine object type, multiple types can be specified
>>> isinstance(a, (int, str)) True
Similar to string startswith, endswith
>>> "http://foofish.net".startswith(('http','https')) True >>> "https://foofish.net".startswith(('http','https')) True
The difference between__str__and__repr__
>>> str(datetime.now()) '2018-11-20 00:31:54.839605' >>> repr(datetime.now()) 'datetime.datetime(2018, 11, 20, 0, 32, 0, 579521)'
The former is human-friendly and more readable, while the latter is computer-friendly and supports obj == eval(repr(obj))
use decorators
def makebold(f): return lambda: "" + f() + "" def makeitalic(f): return lambda: "" + f() + "" @makebold @makeitalic def say(): return "Hello" >>> say() Hello
No decorators, very poorly readable
def say(): return "Hello" >> makebold(makeitalic(say))() Hello The above is what advanced skills in Python development are shared by Xiaobian. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.