1. Ellipsis in Python
Python ellipsis is a three-point sequence, usually used in natural languages. But what you may not know is that this is also a valid object in Python:
>>> ... Ellipsis
It is mainly used for matrix slicing in NumPy. However, you can use it instead of placeholders in functions that have not yet been implemented, instead of using the usual method:
def my_awesome_func(): ...
This is the valid Python code
2. Data category
Starting with version 3.7, Python comes with data classes. They have some advantages over conventional classes or other alternatives:
- Return multiple values or dictionaries;
- The data class needs the least amount of code;
- Ability to compare data categories;
- Use__ repr__ Ability to print data classes for debugging;
- Reduce the chance of errors caused by the requirements of type prompt data class.
Sample data classes in operation:
from dataclasses import dataclass @dataclass class Card: rank: str suit: str card = Card("Q", "hearts") print(card == card) # True print(card.rank) # 'Q' print(card) Card(rank='Q', suit='hearts')
3. Zen of Python
One of the earliest Python pep s was PEP-20. This is a list of 19 Python Programming papers, called "The Zen of Python". These rules date back to 2004 and are based on PEP-8.
This Easter egg has existed in Python for a long time and lists a set of rules:
Therefore, as long as you have Python REPL, you can view these rules on the screen.
4. Anonymous function
Sometimes, the naming of functions does not deserve special attention. For example, if you decide to use it only once. In this case, Python recommends using anonymous functions, also known as lambda functions.
lambda functions can be assigned to variables to create a concise way to define functions:
>>> add_one = lambda x: x + 1 >>> add_one(3) 4
It becomes more interesting when you need to use a function as an argument. In this case, it is usually used only once. As you know, map applies functions to all elements of an iteratable object. We can use lambda when calling map:
>>> numbers = [1, 2, 3, 4] >>> times_two = map(lambda x: x * 2, numbers) >>> list(times_two) [2, 4, 6, 8] >>>
This code is very common. For example, when you want to apply an operation to each element of an iterative object. In this case, the combination of map () and lambda function is both concise and effective.
5. List understanding
The overall understanding can replace the non aesthetic loop used to fill the list. The syntax is as follows:
[ expression for item in list if conditional ]
The simplest example of filling a list with a sequence of numbers:
mylist = [i for i in range(10)] print(mylist) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Since you can use expressions here, you can use math:
squares = [x**2 for x in range(10)] print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
External function call:
def some_function(a): return (a + 5) / 2 my_formula = [some_function(i) for i in range(10)] print(my_formula) # [2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0]
Finally, you can use "if" to filter the list. In this case, only those values that can be divided by 2 will be saved:
filtered = [i for i in range(20) if i%2==0] print(filtered) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Many people learn python and don't know where to start.
After learning python and mastering the basic grammar, many people don't know where to find cases.
Many people who have done cases do not know how to learn more advanced knowledge.
So for these three types of people, I will provide you with a good learning platform, free video tutorials, e-books, and the source code of the course!
QQ group: 810735403
6. Replace variables
This clever trick will save you a few lines of code:
a = 1 b = 2 a, b = b, a print (a) # 2 print (b) # 1
7. Format named string
This is not often used, but if the data is already in the dictionary, the following techniques are useful for formatting named strings:
You can even use the locals () function, but in the latest version of Python, you will have to access the f string as follows:
8. Nested list derivation
Remember the basic syntax of list derivation?
[ expression for item in list if conditional ]
If expression can be any valid Python expression, it can also be another list derivation. This is useful when you need to create a matrix:
>>> [[j for j in range(3)] for i in range(4)] [[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]
Or, if you want to "flatten" the previous matrix:
>>> [value for sublist in m for value in sublist] [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]
The first part of the loop passes through matrix m and the second part passes through the elements of each vector.
9. Required parameters
To force the use of parameters, place an asterisk in front of them and force all parameters to become Keywords:
>>> def f(*, a, b): ... print(a, b) ... >>> f(1, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() takes 0 positional arguments but 2 were given >>> f(a=1, b=2) 1 2 >>>
10. Use underline in REPL
You can use the underscore operator to get the result of the last expression in Python REPL. For example, in Python REPL, it looks like this:
>>> 3 * 3 9 >>> _ + 3 12
This technique can also be used in IPython Shell.
11. Check the required Python version
To prevent users from running scripts with incompatible versions, it's worth checking the current version of Python in your code. Do a simple test:
if not sys.version_info > (2, 7): # berate your user for running a 10 year # python version elif not sys.version_info >= (3, 5): # Kindly tell your user (s)he needs to upgrade # because you're using 3.5 features
12. Decoration function
A decorator is a wrapper around a function that somehow changes its behavior. Decorators have some use cases that you may have used when using frameworks such as Flask.
Let's create our own decorator:
def print_argument(func): def wrapper(the_number): print("Argument for", func.__name__, "is", the_number) return func(the_number) return wrapper @print_argument def add_one(x): return x + 1 print(add_one(1))
In print_ Inside argument, we define the wrapper function. It prints out the parameters and names of the called function, executes the actual function, and returns its results as if the function had been called "normally".
Via @ print_argument, we apply the decorator to the function. The decorator can also be reused for other functions.
Let's create our own decorator:
Argument for add_one is 1 2
13. Return multiple values
Functions in Python can return multiple variables without using dictionaries, lists, or classes. It's like this:
def get_user(id): # fetch user from database # .... return name, birthdate name, birthdate = get_user(4)
This is where the tuple is returned. You may have written a declaration form (name, birthday) with the same effect.
This is good for a limited number of return values. But anything with more than three values must be placed in the (data) class.
14. Merge Dictionaries
Starting with Python 3.5, merging dictionaries has become easier.
dict1 = { 'a': 1, 'b': 2 } dict2 = { 'b': 3, 'c': 4 } merged = { **dict1, **dict2 } print (merged) # {'a': 1, 'b': 3, 'c': 4} # Python >= 3.9 only merged = dict1 | dict2 print (merged) # {'a': 1, 'b': 3, 'c': 4}
If there are duplicate keys, they will be overwritten in the first dictionary.
15. Slice list
The slice syntax is as follows:
a[start:stop:step]
Start, stop and step are optional. They have default values and will be activated if you do not fill in the parameters:
- 0 start;
- The end of the stop list;
- Step 1.
Here are some examples:
# We can easily create a new list from # the first two elements of a list: first_two = [1, 2, 3, 4, 5][0:2] print(first_two) # [1, 2] # And if we use a step value of 2, # we can skip over every second number # like this: steps = [1, 2, 3, 4, 5][0:5:2] print(steps) # [1, 3, 5] # This works on strings too. In Python, # you can treat a string like a list of # letters: mystring = "abcdefdn nimt"[::2] print(mystring) # 'aced it'
16. Memory usage
Use sys Getsizeof(), you can check the memory usage of the object:
import sys mylist = range(0, 10000) print(sys.getsizeof(mylist)) # 48
The huge list is only 48 bytes, because the behavior returned by the range function is similar to that of the list. In terms of memory, ranges are more efficient than using a list of actual numbers.
import sys myreallist = [x for x in range(0, 10000)] print(sys.getsizeof(myreallist)) # 87632
17. Use * and * * decompression function parameters
Some functions require a long list of arguments. This should be avoided (for example, using data classes), although it is not always up to you. Another option is to create a dictionary using named parameters and pass it to the function. This will make your code more readable.
You can use the * * prefix to extract the dictionary:
>>> def f(a, b): ... print(a, b) ... >>> args = { "a": 1, "b": 2 } >>> f(**args) 1 2
Similarly, you can use * to decompress the array and pass its contents as parameters to the function:
>>> def f(a, b, c): ... print(a, b, c) ... >>> l = [1, 2, 3] >>> f(*l) 1 2 3
18. Line - title
If you want to get a good title quickly, do the following:
mystring = "10 awesome python tricks" print(mystring.title()) '10 Awesome Python Tricks'
19. Split strings into lists
You can split a string into a list of strings. In this case, where the character is split:
mystring = "The quick brown fox" mylist = mystring.split(' ') print(mylist) # ['The', 'quick', 'brown', 'fox']
You don't need to pass} any parameters to split with spaces - use mystring split().
Split also has a second parameter maxplit, which determines the maximum split score. By default, it is - 1 (unlimited). This is an example that limits the split to 1:
>>> mystring.split(' ', 1) ['The', 'quick brown fox']
20. Create a string from the string list
Create a string from the list and set a space between each word:
mylist = ['The', 'quick', 'brown', 'fox'] mystring = " ".join(mylist) print(mystring) # 'The quick brown fox'
It comes down to the fact that string The join () function can join not only the list, but also any iteratable object. Placing it in a string prevents the same functionality from being implemented in multiple locations.
21. Query JSON
JMESpath is a JSON query language that allows you to retrieve the required data from a JSON document or dictionary. The library can be used in Python and other languages to extend its functionality.
Here are some general code examples:
>>> import jmespath >>> persons = { ... "persons": [ ... { "name": "erik", "age": 38 }, ... { "name": "john", "age": 45 }, ... { "name": "rob", "age": 14 } ... ] ... } >>> jmespath.search('persons[*].age', persons) [38, 45, 14]
22. Reverse string and list
You can use slice symbols to reverse strings or lists. If the step value is negative, swap elements:
revstring = "abcdefg"[::-1] print(revstring) # 'gfedcba' revarray = [1, 2, 3, 4, 5][::-1] print(revarray) # [5, 4, 3, 2, 1]
23. Get unique item from list or string
By using the set() function to create a collection, you can get all unique elements from the list or object:
mylist = [1, 1, 2, 3, 4, 5, 5, 5, 6, 6] print (set(mylist)) # {1, 2, 3, 4, 5, 6} # And since a string can be treated like a # list of letters, you can also get the # unique letters from a string this way: print (set("aaabbbcccdddeeefff")) # {'a', 'b', 'c', 'd', 'e', 'f'}
24. Valid dictionary values
You can put anything in the dictionary - not just numbers or strings. You can place a list in a dictionary and access nested values:
>>> a = { 'sub_dict': { 'b': True }, 'mylist': [100, 200, 300] } >>> a['sub_dict']['b'] True >>> a['mylist'][0] 100
25. Ternary conditional assignment operator
This is another way to make the code more concise and readable:
[on_true] if [expression] else [on_false]
Here is an example:
x = "Success!" if (y == 2) else "Failed!"
26. Calculate the entries in the list
Use Counter in the collection library to get a dictionary of all unique items in the list:
from collections import Counter mylist = [1, 1, 2, 3, 4, 5, 5, 5, 6, 6] c = Counter(mylist) print(c) # Counter({1: 2, 2: 1, 3: 1, 4: 1, 5: 3, 6: 2}) # And it works on strings too: print(Counter("aaaaabbbbbccccc")) # Counter({'a': 5, 'b': 5, 'c': 5})
27. Comparison operator chain
Make your code more readable and tidy:
x = 10 # Instead of: if x > 5 and x < 15: print("Yes") # yes # You can also write: if 5 < x < 15: print("Yes") # Yes
28. Processing date
The python dateutil module provides a powerful extension to the standard DATETIME. Its installation is as follows:
pip3 install python-dateutil
This is an example of parsing dates from logs:
from dateutil.parser import parse logline = 'INFO 2020-01-01T00:00:01 Happy new year, human.' timestamp = parse(logline, fuzzy=True) print(timestamp) # 2020-01-01 00:00:01
29. Using maps ()
This is the syntax of this built-in function:
map(function, something_iterable)
The following is an example of using a list:
def upper(s): return s.upper() mylist = list(map(upper, ['sentence', 'fragment'])) print(mylist) # ['SENTENCE', 'FRAGMENT'] # Convert a string representation of # a number into a list of ints. list_of_ints = list(map(int, "1234567")) print(list_of_ints) # [1, 2, 3, 4, 5, 6, 7]
30. Vocabulary and freeze frame understanding
Dictionary requires a key and a value:
>>> {x: x**2 for x in (2, 4, 6)} {2: 4, 4: 16, 6: 36}
We define keys and values in expressions.
Set understanding syntax is not much different from list understanding. We only use curly braces instead of square brackets:
{ <expression> for item in list if <conditional> }
example:
>>> {s for s in range(1,5) if s % 2} {1, 3}