× {{alert.msg}} Never ask again
Receive New Tutorials
GET IT FREE

10 Neat Python Tricks Beginners Should Know

– {{showDate(postTime)}}
10 Neat Python Tricks Beginners Should Know

Trick #1

Reversing a string in Python
>>> a =  "codementor"
>>> 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

a = [1,2,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

a = [“Code”, “mentor”, “Python”, “Developer”]

Create a single string from all the elements in list above.</h5>
```python

print “ “.join(a)
Code mentor Python Developer
```

Trick #5

list1 = [‘a’, ‘b’, ‘c’, ‘d’]
list2 = [‘p’, ‘q’, ‘r’, ‘s’]
Write a Python code to print
ap
bq
cr
ds
>>> for x, y in zip(list1,list2):
...    print x, y
...
a p
b q
c r
d s

Trick #6

Swap two numbers with one line of code.
>>> a=7
>>> b=5
>>> b, a =a, b
>>> a
5
>>> b
7

Trick #7

>>> print "code"*4+' '+"mentor"*5
codecodecodecode mentormentormentormentormentor

Trick #8

a = [[1, 2], [3, 4], [5, 6]]
Convert it to a single list without using any loops.
Output:- [1, 2, 3, 4, 5, 6]
>>> import itertools 
>>> list(itertools.chain.from_iterable(a))
[1, 2, 3, 4, 5, 6]

Trick #9

Checking if two words are anagrams
def is_anagram(word1, word2):
    """Checks whether the words are anagrams.
    word1: string
    word2: string
    returns: boolean
    """

Complete the above method to find if two words are anagrams.
python from collections import Counter def is_anagram(str1, str2): return Counter(str1) == Counter(str2) >>> is_anagram('abcd','dbca') True >>> is_anagram('abcd','dbaa') False

Trick #10.

Take a string input.
For example “1 2 3 4” and return [1, 2, 3, 4]

Remember list being returned has integers in it.
Don’t use more than one line of code.

>>> result = map(lambda x:int(x) ,raw_input().split())
1 2 3 4
>>> result
[1, 2, 3, 4]


Author
Sumit Raj
Sumit Raj
4.6
Full Stack Web Developer.Loves Python and new cutting edge technologies. Highly self-motivated.
Building something at the intersection of analytics, machine learning, and internet technologies. Specialties: Python, HTML5, CSS3, JAVA, XQuery, Web Scraping, SQL Interests :- Machine...
Hire the Author

Questions about this tutorial?  Get Live 1:1 help from Python experts!
Jessamyn Smith
Jessamyn Smith
5.0
Experienced Full-Stack Web Developer
I am an experienced full-stack software developer with over a decade in the field, primarily in back-end development for web applications. My...
Hire this Expert
Ray Phan
Ray Phan
5.0
PhD, former university instructor and software engineer with 20 years of software development experience in MATLAB, Python, Java, C, and C++. Image Processing and Machine Learning are part of my all-balanced diet. Students get a 33% discount!
*** Please note that if your language doesn't appear in my tags in my headline above, I probably don't know it! *** - Ph.D. (2013) - Electrical &...
Hire this Expert

Or Become a Codementor!

Live 1:1 help from expert developers

Codementor is your live 1:1 expert mentor helping you in real time.

comments powered by Disqus
Codementor is your live 1:1 expert helping you in real time