current position:Home>Collect it quickly so that you can use the 30 Python tips for taking off

Collect it quickly so that you can use the 30 Python tips for taking off

2022-02-01 15:53:39 aikutwo

Python Is one of the most popular languages , Many people use it for data science and machine learning 、Web Development 、 scripting 、 Automation and other fields .

This popularity is partly due to its simplicity and ease of learning .

In this paper , I'll introduce you to 30 A short 、 Economic Python skill . The content is more , It is suggested to collect and study , Like to like and support

1. All unique

The following method checks whether a given list has duplicate elements , It USES set() Remove duplicate elements from the list with the properties of .

image.png

2. Crossword puzzle

This method can be used to check whether two strings are riddles . A word puzzle is a word or phrase formed by rearranging the letters of different words or phrases , Usually all original letters are used only once .

image.png

3. Memory

This code snippet can be used to check the memory usage of an object .

image.png

4. Byte size

This method uses Byte Returns the length of a string in units .

image.png

5. Print a string N Time

This code snippet can be used to print strings n Time , You don't need a loop to do this .

image.png

6. title case

This code snippet uses only title() Method capitalizes the first letter of each word in the string .

image.png

7. List cut

This method divides the list into smaller lists of the specified size .

image.png

8. Delete false values

This method uses filter() Remove false values from the list (False、None、0 and “”)

image.png

9. Transpose a two-dimensional array

This code snippet can be used to transpose a two-dimensional array

image.png

10. Chain comparison

You can compare various operators multiple times in a row .

image.png

11. Comma separated

This code snippet can be used to convert a list of strings to a single string , Each element in the list is separated by commas .

image.png

12. Get vowels

This method gets the vowels in the string (‘a’、‘e’、‘i’、‘o’、‘u’)

image.png

13. conversion

This method can be used to convert the first letter of a given string to lowercase .

image.png

14. Flatten

The following method uses recursion to flatten potential deep lists .

image.png

15. differences

This method finds the difference between two iterations by retaining only the value in the first one .

image.png

16. Differentiation

After applying the given function to each element of both lists , The following method returns the difference between two lists .

image.png

17. Chain function call

You can call multiple functions in one line.

image.png

18. duplicate value

The following methods are implemented by using set() The fact that it contains only unique elements , To check if the list has duplicate values .

image.png

19. Merge two dictionaries

The following methods can be used to merge two dictionaries

image.png stay Python 3.5 And later , You can also do something like this :

image.png

20. Convert two lists into dictionaries

The following methods can be used to convert two lists into dictionaries

image.png

21. Use enumeration

This code snippet shows that you can use enumerate To get the value and index of the list .image.png

22. Time spent calculating

This code snippet can be used to calculate the time required to execute a particular code .

image.png

23. try/else

You can use else Clause as try/except Part of the block , If no exception is thrown , Will execute the block .

image.png

24. Statistics are most frequent

This method returns the most frequent element in the list .

image.png

25. Palindrome

This method checks whether the given string is a palindrome .

image.png

26. No, if-else 's calculator

The following code snippet shows how to use if-else Condition, write a simple calculator .

image.png

27.shuffle

This code snippet can be used to randomize the order of elements in the list . Please note that ,shuffle Local work , And back to None.

image.png

28. List flatten

This method is similar to JavaScript Medium [].concat(…arr) Flatten the list .

image.png

29. Exchange value

This is a very fast way to swap two variables without using additional variables .

image.png

30. Get the default value of the missing key

This code snippet shows how to get the default value if the dictionary does not contain the key you want to find .

image.png

More programming related knowledge recommended link Watch the instructional video ~

copyright notice
author[aikutwo],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011553376019.html

Random recommended