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 .


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 .


3. Memory

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


4. Byte size

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


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 .


6. title case

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


7. List cut

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


8. Delete false values

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


9. Transpose a two-dimensional array

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


10. Chain comparison

You can compare various operators multiple times in a row .


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 .


12. Get vowels

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


13. conversion

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


14. Flatten

The following method uses recursion to flatten potential deep lists .


15. differences

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


16. Differentiation

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


17. Chain function call

You can call multiple functions in one line.


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 .


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 :


20. Convert two lists into dictionaries

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


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 .


23. try/else

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


24. Statistics are most frequent

This method returns the most frequent element in the list .


25. Palindrome

This method checks whether the given string is a palindrome .


26. No, if-else 's calculator

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



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.


28. List flatten

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


29. Exchange value

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


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 .


More programming related knowledge recommended link Watch the instructional video ~

copyright notice
author[aikutwo],Please bring the original link to reprint, thank you.

Random recommended