Pythonis a very concise language,
Python'sThe simplicity and ease of use make it easy to sigh the light of this language. In this article, we have listed 17 very useful
Python TipsThis
17 Tipsare very simple, but they are very common and can inspire different ideas.
Many people know that Python is a high-level programming language , the core idea of which is the legibility of code and the idea of allowing programmers to express ideas easily through a number of lines of code. In fact, the first reason many people choose to learn Python is the beauty of their programming , and it's natural to use it to encode and express ideas. In addition,python is written in a variety of ways, using pythonfor Data Science, web development, and machine learning. Quora, Pinterest, and Spotify use Python as the back-end development language.
Exchange variable values
"" "Pythonic The Value swapping" "A, B=5,10print (A, B) A,b=b,aprint (A, B)
Combine all the elements in a list into a string
a=["Python", "is", "Awesome"]print (" ". Join (a))
Find list Intermediate frequency the highest rate value
"" "most frequent element in a list" "" A=[1,2,3,1,2,3,2,2,4,5,1]print (Max (Set (a), key=a.count)) "" "" Using Counter from Collections "" "from collections import Countercnt=counter (a) print (Cnt.most_commin (3))
Check that two strings are not made up of the same letters in different order
From collections Import Countercounter (STR1) ==counter (STR2)
Invert string
"" "Reversing string with special case of slice step param" "" a = ' abcdefghij k lmnopqrs tuvwxyz ' Print (a[:: -1]) " "" Iterating over string contents in reverse efficiently. "" " For Char in Reversed (a): print (char) "" "Reversing an integer through type conversion and slicing. " "" num = 123456789 print (int (str (num) [:: 1]))
Reverse List
"" "Reversing list with special case of slice step param" "" a=[5,4,3,2,1 " print (A[::1]) " "" iterating over List contents in reverse efficiently. "" " For Ele in Reversed (a): print (ele)
Transpose two-dimensional arrays
"" "Transpose 2d array [[A, A,], [c,d], [e,f]], [[A,c,e], [b,d,f]]" "" original = [[' A ', ' B '], [' C ', ' d '], [' E ', ' F ']]tran sposed = Zip (*original) print (list (transposed))
Chain comparison
"" "Chained comparison with all kind of operators" "" B =6print (4< B < 7) print (1 = = B < 20)
Chained function calls
"" "calling different functions with same arguments based on condition" "" Def product (A, B): return a * bdef add (A, B): return a + BB =trueprint (product if B else add) (5, 7))
Copy List
"" "a fast-to-make a shallow copy of a list" "" B=a b[0]= "" " bothaandbwillbe[10,2,3,4,5]" "" b = a[:]b[o] = 1 0 "" "only B would change to [2, 3, 4, 5]" "" "" " copy List by typecasting method" "" A=[l,2,3,4,5]print (List ( A) "" " using the List.copy () method (Python3 only)" "" a=[1,2,3,4,5 " print (A.copy ())" "" copy nested Li STS using copy. Deepcopy "" "from copy import deepcopy l=[l,2],[3,4] L2 = deepcopy (l) print (L2)
Dictionary Get method
"" "Returning None or default value, when key was not in Dict" "" D = [' A ': 1, ' B ': 2]print (D.get (' C ', 3))
Sort dictionary elements by "keys"
"" "Sort a dictionary by it values with the built-in sorted () function and a ' key ' argument. " " D = {' Apple ': ten, ' orange ': ' Banana ': 5, ' Rotten Tomato ': 1 ' print (sorted (d. Items (), Key=lambda x:x[1])) "" "Sort using operator. Itemgetter as the sort key instead of a lambda "" "from operator import itemgetter print (sorted (d. Items (), Key=it Emgetter (1)) "" " Sort dict Keys by Value" "" print (sorted (d, Key=d.get))
For Else
"" "Else gets called when the For loop does not reach break statement" "" A=[1,2,3,4,5]for el in a: if el==0: breakelse: Print (' Do not is break out of a for Loop ')
Convert list to comma separator format
"" "converts list to Comma separated string" "" Items = [foo ', ' Bar ', ' xyz ']print (', ') '. Join (items) ' "" List of the numbers to COM Ma separated "" "numbers = [2, 3, 5, 10]print (', ') '. Join (Map (str, numbers))" "" List of Mix data "" "Data = [2, ' Hello ', 3, 3,4] Print (', '. Join (Map (str, data))
Merging dictionaries
"" "" "" Merge Dict ' "" "D1 = {' a ': 1}d2 = {' B ': 2}# python 3.5 print ({**d1, **d2}) print (Dict (D1. Items () | d2. Items ())) D1. Update (D2) print (D1)
Index of minimum and maximum values in the list
"" "Find Index of Min/max Element." "" " lst= [Max, 30]def Minindex (LST): return min (range (len), Key=lst: getitem__) def maxindex (LST), Retu RN Max (range (len (LST)), key=lst. getitem__) Print (Minindex (LST)) print (Maxindex (LST))
Remove duplicate elements from a list
"" Remove duplicate items from list. Note:does, not preserve the original list order "" "ITEMS=[2,2,3,3,1]NEWITEMS2 = List (set (items)) print (NEWITEMS2)" "Remo ve dups and, keep. Order "" "from collections Import Ordereddictitems = [" foo "," Bar "," Bar "," foo "]print (List (ordereddict.f romkeys (items). Keys ()))
The above is about 17 practical and effective small operations in Python programming