Joswlv

Python Tricks

2017-08-02

9 Neat Python Tricks Beginners Should Know

Trick #1

String Reversing

>>> a = "comdementor"
>>> print "Reverse is",a[::-1]
Reverse is rotnemedoc

Trick #2

Transposing a Matrix

>>> mat = [[1,2,3], [4,5,6]]
>>> zip(*mat)
[(1,4), (2,5), (3,6)]

Trick #3

Store all three values of the list in 3 new variables

>>> a = [1,2,3]
>>> x, y, z = a
>>> x
1
>>> y
2
>>> z
3

Trick #4

Create a single from all the elements in list above

>>> a = ["a", "b", "c", "d"]
>>> print " ".join(a)
a b c d

Trick #5

>>> list1 = ['a','b','c','d']
>>> list2 = ['p','q','r','s']
>>> for x, y in zip(list1,list2):
... 	print x, y
...
a p
b q
c r
d s

Trick #6

Swap two numbers with on line of code

>>> a=7
>>> b=5
>>> b, a = a, b
>>> a
5
>>> b
7

Trick #7

>>> print "code"*4+' '+"good"*5
codecodecodecode goodgoodgoodgoodgood

Trick #8

Convert it to a single list without using any loops.

>>> list1 = [ [1 , 2] , [ 3, 4] ]
>>> answer = sum( list1 , [ ] )
>>> print answer
1,2,3,4

Trick #9

Taking a string input

>>> wantToInt = "1 2 3 4"
>>> map(int, wantToInt.split())
[1, 2, 3, 4]

출처


Comments