Beginners often do not know some skills in coding, so the code will be very long, very complex, the following will introduce a few novice should know a few tips to simplify the code.
1. Invert strings
>>> a = "codementor"print"Reverseis" , a[::-1 is Rotnemedoc
2. Transpose The Matrix
>>> mat = [[1, 2, 3], [4, 5, 6]]>>> zip (*mat) [(1, 4), (2, 5), (3, 6)]
3. Store the list data separately in a separate variable
>>> a = [1, 2, 3]>>> x, y, z =>>> x1>>> y2>> ;> Z3
4. Merging the list of strings into a single new string
>>> a = ["Code","mentor","Python ","Developer"] print"" . Join (a) Code mentor Python Developer
5. One line of code swap two variables
>>> a=7>>> b=5>>> B, a =A, b>>> a5>>> b7
6. Do not cycle print "Codecodecodecode mentormentormentormentormentor"
Print " Code "*4+" +"mentor"Codecodecodecode Mentormentormentormentormentor
7. Merging the list of arrays into a single whole
Import>>>a = [[1, 2], [3, 4], [5, 6]]>>> list (itertools.chain.from_ Iterable (a)) [1, 2, 3, 4, 5, 6]
8. Speak only numeric string conversion to List
>>> result = Map (lambda x:int (x), Raw_input (). Split ())1 2 3 4>>> result[1, 2, 3, 4]
Reference: Https://www.codementor.io/python/tutorial/python-tricks-for-beginners
Python Tutorials---A few tips that beginners should know